1pub use chrono::*;
2pub const LOG_DATE_FORMAT: &'static str = "[%Y-%m-%d][%H:%M:%S]";
4pub const LOG_FILE_DATE_FORMAT: &'static str = "%Y-%m-%d_%H-%M-%S";
6pub const STANDARD_DATETIME_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
8
9pub fn now() -> DateTime<Utc> {
11 Utc::now()
12}
13pub fn parse_str(content: &str, fmt: &str) -> Option<DateTime<Utc>> {
15 Some(NaiveDateTime::parse_from_str(content, fmt).ok()?.and_utc())
16}
17pub 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}
21pub 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}
25pub fn parse_datetime_offset(datetime: NaiveDateTime, fixed_offset: FixedOffset) -> Option<DateTime<FixedOffset>> {
27 Some(DateTime::from_naive_utc_and_offset(datetime, fixed_offset))
28}
29pub fn china_now() -> Option<DateTime<FixedOffset>> {
31 let shanghai_offset = FixedOffset::east_opt(8 * 3600)?; Some(DateTime::from_naive_utc_and_offset(Utc::now().naive_utc(), shanghai_offset))
34}