Skip to content

Commit

Permalink
Add support for some myqsl and cassandra date formats.
Browse files Browse the repository at this point in the history
Fixes #3.
  • Loading branch information
jrduncans committed Jan 25, 2021
1 parent 7536324 commit 2af6c75
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "timeturner"
version = "1.5.0"
version = "1.6.0"
authors = ["Stephen Duncan <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
29 changes: 27 additions & 2 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ fn parse_from_rfc3339(input: &str) -> Option<ParsedInput> {
})
}

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<ParsedInput> {
Expand All @@ -77,7 +78,7 @@ fn parse_from_format_unzoned(input: &str, format: &str) -> Option<ParsedInput> {
})
}

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<ParsedInput> {
CUSTOM_ZONED_FORMATS
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2af6c75

Please sign in to comment.