Skip to content

Commit

Permalink
feat(core): add simple date time
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 9, 2021
1 parent 04be50e commit 21b7f45
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions quake_core/src/helper/quake_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use regex::Regex;
const DATETIME_ZONE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S %z";
const DATETIME_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
const DATE_FORMAT: &'static str = "%Y-%m-%d";
const SIMPLE_DATE_FORMAT: &'static str = "%Y.%m.%d";

lazy_static! {
static ref SIMPLE_DATE_REGEX: Regex =
Regex::new(r"(?P<y>\d{4}).(?P<m>\d{2}).(?P<d>\d{2})").unwrap();
static ref ISO8601_DATE_REGEX: Regex =
Regex::new(r"(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})").unwrap();
static ref ISO8601_DATE_TIME_REGEX: Regex =
Expand Down Expand Up @@ -51,6 +54,12 @@ pub fn text_date_to_unix(text: &str) -> i64 {
return naive_datetime.timestamp();
};

if let Some(_caps) = SIMPLE_DATE_REGEX.captures(text) {
let naive_date = NaiveDate::parse_from_str(text, SIMPLE_DATE_FORMAT).unwrap();
let naive_datetime = naive_date.and_hms(0, 0, 0);
return naive_datetime.timestamp();
};

timestamp
}

Expand All @@ -63,5 +72,6 @@ mod tests {
assert_eq!(text_date_to_unix("2020-04-12 22:10:57 +08:00"), 1586700657);
assert_eq!(text_date_to_unix("2020-04-12 22:10:57"), 1586729457);
assert_eq!(text_date_to_unix("2021-12-09"), 1639008000);
assert_eq!(text_date_to_unix("2021.12.09"), 1639008000);
}
}

0 comments on commit 21b7f45

Please sign in to comment.