e_utils/system/
chrono.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pub use chrono::*;
/// 日志日期格式
pub const LOG_DATE_FORMAT: &'static str = "[%Y-%m-%d][%H:%M:%S]";
/// 日志日期文件格式
pub const LOG_FILE_DATE_FORMAT: &'static str = "%Y-%m-%d_%H-%M-%S";
/// 系统日期时间
pub const STANDARD_DATETIME_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";

/// Get Current time
pub fn now() -> DateTime<Utc> {
  Utc::now()
}
/// 解析字符串
pub fn parse_str(content: &str, fmt: &str) -> Option<DateTime<Utc>> {
  Some(NaiveDateTime::parse_from_str(content, fmt).ok()?.and_utc())
}
/// 解析字符串
pub fn parse_china_str(content: &str, fmt: &str) -> Option<DateTime<FixedOffset>> {
  let shanghai_offset = FixedOffset::east_opt(8 * 3600)?; // UTC+8
  NaiveDateTime::parse_from_str(content, fmt)
    .ok()?
    .and_local_timezone(shanghai_offset)
    .single()
}
/// Get Now From China
pub fn china_now() -> Option<DateTime<FixedOffset>> {
  // 定义上海时区
  let shanghai_offset = FixedOffset::east_opt(8 * 3600)?; // UTC+8
  Some(DateTime::from_naive_utc_and_offset(
    Utc::now().naive_utc(),
    shanghai_offset,
  ))
}