e_utils/system/
chrono.rs

1pub use chrono::*;
2/// 日志日期格式
3pub const LOG_DATE_FORMAT: &'static str = "[%Y-%m-%d][%H:%M:%S]";
4/// 日志日期文件格式
5pub const LOG_FILE_DATE_FORMAT: &'static str = "%Y-%m-%d_%H-%M-%S";
6/// 系统日期时间
7pub const STANDARD_DATETIME_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
8
9/// Get Current time
10pub fn now() -> DateTime<Utc> {
11  Utc::now()
12}
13/// 解析字符串
14pub fn parse_str(content: &str, fmt: &str) -> Option<DateTime<Utc>> {
15  Some(NaiveDateTime::parse_from_str(content, fmt).ok()?.and_utc())
16}
17/// 解析字符串
18pub fn parse_china_str(content: &str, fmt: &str) -> Option<DateTime<FixedOffset>> {
19  parse_datetime_offset(NaiveDateTime::parse_from_str(content, fmt).ok()?, FixedOffset::east_opt(8 * 3600)?)
20}
21/// 解析时间戳
22pub fn parse_timestamp(timestamp: i64) -> Option<DateTime<FixedOffset>> {
23  parse_datetime_offset(DateTime::from_timestamp(timestamp, 0)?.naive_utc(), FixedOffset::east_opt(8 * 3600)?)
24}
25/// 解析
26pub fn parse_datetime_offset(datetime: NaiveDateTime, fixed_offset: FixedOffset) -> Option<DateTime<FixedOffset>> {
27  Some(DateTime::from_naive_utc_and_offset(datetime, fixed_offset))
28}
29/// Get Now From China
30pub fn china_now() -> Option<DateTime<FixedOffset>> {
31  // 定义上海时区
32  let shanghai_offset = FixedOffset::east_opt(8 * 3600)?; // UTC+8
33  Some(DateTime::from_naive_utc_and_offset(Utc::now().naive_utc(), shanghai_offset))
34}