Commit 4c9dbaf2 authored by xuwencheng's avatar xuwencheng

time

parent 606f9b71
...@@ -9,6 +9,7 @@ socket = ["rmp", "rmp-serde", "bytes", "flate2"] ...@@ -9,6 +9,7 @@ socket = ["rmp", "rmp-serde", "bytes", "flate2"]
paidui = [] paidui = []
login = [] login = []
client = ["socket"] client = ["socket"]
time_tool = ["time", "compact_str"]
[dependencies] [dependencies]
anyhow = { version = "1", features = ["std"], default-features = false } anyhow = { version = "1", features = ["std"], default-features = false }
...@@ -27,3 +28,11 @@ rmp = { version = "0.8", default-features = false, optional = true } ...@@ -27,3 +28,11 @@ rmp = { version = "0.8", default-features = false, optional = true }
rmp-serde = { version = "1", default-features = false, optional = true } rmp-serde = { version = "1", default-features = false, optional = true }
flate2 = { version = "1", features = ["rust_backend"], default-features = false, optional = true } flate2 = { version = "1", features = ["rust_backend"], default-features = false, optional = true }
compact_str = { version = "0.7", features = [
"serde",
], default-features = false, optional = true }
time = { version = "0.3", features = [
"formatting",
"macros",
], default-features = false, optional = true }
pub use tool::id;
#[cfg(feature = "client")]
pub mod client;
pub mod event; pub mod event;
pub mod id;
#[cfg(feature = "login")] #[cfg(feature = "login")]
pub mod login; pub mod login;
#[cfg(feature = "paidui")] #[cfg(feature = "paidui")]
...@@ -7,8 +10,7 @@ pub mod paidui; ...@@ -7,8 +10,7 @@ pub mod paidui;
pub mod serde; pub mod serde;
#[cfg(feature = "socket")] #[cfg(feature = "socket")]
pub mod socket; pub mod socket;
#[cfg(feature = "client")] pub mod tool;
pub mod client;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
......
...@@ -16,7 +16,7 @@ pub struct LoginInfo { ...@@ -16,7 +16,7 @@ pub struct LoginInfo {
impl LoginInfo { impl LoginInfo {
pub fn is_dinner(&self) -> bool { pub fn is_dinner(&self) -> bool {
self.brand_type == 70 self.brand_type.is_dinner()
} }
} }
...@@ -34,7 +34,7 @@ pub struct StoreInfo { ...@@ -34,7 +34,7 @@ pub struct StoreInfo {
impl StoreInfo { impl StoreInfo {
pub fn is_dinner(&self) -> bool { pub fn is_dinner(&self) -> bool {
self.brand_type == 70 self.brand_type.is_dinner()
} }
pub fn login_info(&self, token: impl AsRef<str>) -> LoginInfo { pub fn login_info(&self, token: impl AsRef<str>) -> LoginInfo {
...@@ -47,4 +47,14 @@ impl StoreInfo { ...@@ -47,4 +47,14 @@ impl StoreInfo {
token: token.as_ref().to_string(), token: token.as_ref().to_string(),
} }
} }
}
pub trait BrandType {
fn is_dinner(&self) -> bool;
}
impl BrandType for i32 {
fn is_dinner(&self) -> bool {
70.eq(self)
}
} }
\ No newline at end of file
pub mod id;
#[cfg(feature = "time_tool")]
pub mod time;
use anyhow::Result;
use compact_str::CompactString;
use time::{
error::ComponentRange, format_description::FormatItem, macros::format_description, Duration,
OffsetDateTime, UtcOffset,
};
const OFFSET: Result<UtcOffset, ComponentRange> = UtcOffset::from_hms(8, 0, 0);
pub fn datetime() -> Result<CompactString> {
#[rustfmt::skip]
const FORMAT: &[FormatItem<'static>] = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]:[subsecond]");
Ok(OffsetDateTime::now_utc()
.to_offset(OFFSET?)
.format(FORMAT)
.map(CompactString::from)?)
}
pub fn date() -> Result<CompactString> {
const FORMAT: &[FormatItem<'static>] = format_description!("[year]-[month]-[day]");
Ok(OffsetDateTime::now_utc()
.to_offset(OFFSET?)
.format(FORMAT)
.map(CompactString::from)?)
}
pub fn date_by_offset(days: i64) -> Result<CompactString> {
const FORMAT: &[FormatItem<'static>] = format_description!("[year]-[month]-[day]");
Ok(OffsetDateTime::now_utc()
.to_offset(OFFSET?)
.checked_add(Duration::days(days))
.ok_or_else(|| anyhow::anyhow!("Date offset failed"))?
.format(FORMAT)
.map(CompactString::from)?)
}
// 计算指定天数前的毫秒时间戳
pub fn date_millisecond(days: i64) -> Result<u64> {
milliseconds(Duration::days(days))
}
// 计算指定小时前的毫秒时间戳
pub fn hour_millisecond(hours: i64) -> Result<u64> {
milliseconds(Duration::hours(hours))
}
pub fn milliseconds(duration: Duration) -> Result<u64> {
let offset_date_time = OffsetDateTime::now_utc()
.to_offset(OFFSET?)
.checked_add(duration)
.ok_or_else(|| anyhow::anyhow!("Date offset failed"))?;
let millis = offset_date_time.unix_timestamp() * 1000 + offset_date_time.millisecond() as i64;
Ok(millis as u64)
}
pub fn slice_to_array_4<T>(slice: &[T]) -> Result<&[T; 4]> {
if slice.len() == 4 {
let ptr = slice.as_ptr() as *const [T; 4];
unsafe { Ok(&*ptr) }
} else {
Err(anyhow::anyhow!(
"Data length is not consistent with the expected length"
))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment