Skip to content

Commit

Permalink
fix: fix time issue for lazi onenote 😤😤
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 10, 2021
1 parent 7a0ace9 commit 07fbefa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
21 changes: 17 additions & 4 deletions quake_core/src/helper/quake_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use regex::Regex;
const DATETIME_ZONE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S %z";
const DATETIME_NANO_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S.%f";
const UTC_FORMAT: &'static str = "%Y-%m-%dT%H:%M:%SZ";
const UTC_MS_FORMAT: &'static str = "%Y-%m-%dT%H:%M:%S.%fZ";
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";
Expand All @@ -15,9 +16,11 @@ lazy_static! {
static ref ISO8601_DATE_TIME_REGEX: Regex =
Regex::new(r"(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})").unwrap();
static ref RFC3339_NANO_REGEX: Regex =
Regex::new(r"(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2,8})").unwrap();
Regex::new(r"(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{1,8})").unwrap();
static ref UTC_TIME_REGEX: Regex =
Regex::new(r"(?P<time>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)").unwrap();
static ref UTC_TIME_MS_REGEX: Regex =
Regex::new(r"(?P<time>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,8}Z)").unwrap();
static ref ISO8601_DATE_TIME_ZONE_REGEX: Regex =
Regex::new(r"(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\+\d{2}:\d{2})").unwrap();
}
Expand All @@ -42,7 +45,14 @@ pub fn date_now() -> String {
/// long time should be in first
pub fn replace_to_unix(text: &str) -> String {
let mut result = text.to_string();
// first for: 2021-08-20T06:32:28Z
// for: 2021-08-20T06:32:28.214Z
for caps in UTC_TIME_MS_REGEX.captures_iter(text) {
let time = &caps["time"];
let naive_date_time = NaiveDateTime::parse_from_str(time, UTC_MS_FORMAT).unwrap();
result = result.replace(time, naive_date_time.timestamp().to_string().as_str());
}

// for: 2021-08-20T06:32:28Z
for caps in UTC_TIME_REGEX.captures_iter(text) {
let time = &caps["time"];
let naive_date_time = NaiveDateTime::parse_from_str(time, UTC_FORMAT).unwrap();
Expand Down Expand Up @@ -111,8 +121,11 @@ mod tests {
let filter5 = "created_date > 2021-08-20 06:32:28.537346";
assert_eq!(replace_to_unix(filter5), "created_date > 1629441148");

let filter5 = "created_date > 2021-11-08T07:25:26Z";
assert_eq!(replace_to_unix(filter5), "created_date > 1636356326");
let filter6 = "created_date > 2021-11-08T07:25:26Z";
assert_eq!(replace_to_unix(filter6), "created_date > 1636356326");

let filter7 = "created_date > 2021-11-08T07:25:26.125Z";
assert_eq!(replace_to_unix(filter7), "created_date > 1636356326");
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions quake_core/src/usecases/entrysets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,11 @@ mod tests {
#[cfg(not(windows))]
assert_eq!(json, "[{\"title\":\"time support\",\"author\":\"\",\"content\":\"\\n\\nahaha\\n\",\"created_date\":1637781250,\"updated_date\":1637781250,\"id\":1}]");
}

#[ignore]
#[test]
fn jsonify_todo_with_date_test() {
let buf = PathBuf::from("..").join("examples").join("onenote");
let _json = Entrysets::jsonify_with_format_date(&buf, todo_define()).unwrap();
}
}

0 comments on commit 07fbefa

Please sign in to comment.