From 2af6c7574df0d150ac356930a69c485dc442ce2e Mon Sep 17 00:00:00 2001 From: Stephen Duncan Date: Sun, 24 Jan 2021 17:20:23 -0800 Subject: [PATCH] Add support for some myqsl and cassandra date formats. Fixes #3. --- Cargo.lock | 22 +++++++++++----------- Cargo.toml | 2 +- src/parsing.rs | 29 +++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f790d66..ebbb2c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,18 +92,18 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.17" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" dependencies = [ "libc", ] [[package]] name = "humantime" -version = "2.0.1" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "itoa" @@ -222,18 +222,18 @@ checksum = "088c5d71572124929ea7549a8ce98e1a6fd33d0a38367b09027b382e67c033db" [[package]] name = "serde" -version = "1.0.118" +version = "1.0.122" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +checksum = "974ef1bd2ad8a507599b336595454081ff68a9599b4890af7643c0c0ed73a62c" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.118" +version = "1.0.122" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" +checksum = "8dee1f300f838c8ac340ecb0112b3ac472464fa67e87292bdb3dfc9c49128e17" dependencies = [ "proc-macro2", "quote", @@ -283,9 +283,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.58" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" dependencies = [ "proc-macro2", "quote", @@ -314,7 +314,7 @@ dependencies = [ [[package]] name = "timeturner" -version = "1.5.0" +version = "1.6.0" dependencies = [ "chrono", "chrono-english", diff --git a/Cargo.toml b/Cargo.toml index 51381c8..9fc0910 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "timeturner" -version = "1.5.0" +version = "1.6.0" authors = ["Stephen Duncan "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/src/parsing.rs b/src/parsing.rs index 243d8f4..41f0dba 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -53,12 +53,13 @@ fn parse_from_rfc3339(input: &str) -> Option { }) } -const CUSTOM_UNZONED_FORMATS: [&str; 5] = [ +const CUSTOM_UNZONED_FORMATS: [&str; 6] = [ "%F %T,%3f", "%d %b %Y %H:%M:%S%.3f", "%d %b %Y %H:%M:%S,%3f", "%F %T%.3f UTC", "%T%.3f UTC %F", + "%F %T%.6f", ]; fn parse_custom_unzoned_format(input: &str) -> Option { @@ -77,7 +78,7 @@ fn parse_from_format_unzoned(input: &str, format: &str) -> Option { }) } -const CUSTOM_ZONED_FORMATS: [&str; 0] = []; +const CUSTOM_ZONED_FORMATS: [&str; 2] = ["%F %T%z", "%F %T%.3f%z"]; fn parse_custom_zoned_format(input: &str) -> Option { CUSTOM_ZONED_FORMATS @@ -254,6 +255,30 @@ mod tests { assert_eq!(result.value, Utc.timestamp_millis(1581912639000)); } + #[test] + fn test_casssandra_zoned_no_millis() { + let result = parse_input(&Some(String::from("2015-03-07 00:59:56+0100"))).unwrap(); + assert_eq!(result.input_format, DateTimeFormat::CustomZoned); + assert_eq!(result.input_zone, Some(FixedOffset::east(3600))); + assert_eq!(result.value, Utc.timestamp_millis(1425686396000)); + } + + #[test] + fn test_casssandra_zoned_millis() { + let result = parse_input(&Some(String::from("2015-03-07 00:59:56.001+0100"))).unwrap(); + assert_eq!(result.input_format, DateTimeFormat::CustomZoned); + assert_eq!(result.input_zone, Some(FixedOffset::east(3600))); + assert_eq!(result.value, Utc.timestamp_millis(1425686396001)); + } + + #[test] + fn test_mysql_datetime() { + let result = parse_input(&Some(String::from("2021-01-20 18:13:37.842000"))).unwrap(); + assert_eq!(result.input_format, DateTimeFormat::CustomUnzoned); + assert_eq!(result.input_zone, None); + assert_eq!(result.value, Utc.timestamp_millis(1611166417842)); + } + #[test] fn english_input() { let result = parse_input(&Some(String::from("May 23, 2020 12:00"))).unwrap();