From e00555cd967aba0e84500bb3425e37b406534f7c Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Sun, 25 May 2025 20:51:30 +0200 Subject: [PATCH 1/3] src/lib: Add end-to-end timezone test Also make the offset test a little bit smarter. --- src/lib.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9462a75..8674238 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -212,6 +212,7 @@ mod tests { #[cfg(test)] mod offsets { + use chrono::FixedOffset; use chrono::{Local, NaiveDate}; use crate::parse_datetime; @@ -258,12 +259,25 @@ mod tests { #[test] fn test_datetime_with_offset() { - let actual = parse_datetime("1997-01-19 08:17:48 +0").unwrap(); + let actual = parse_datetime("1997-01-19 08:17:48 +2").unwrap(); let expected = NaiveDate::from_ymd_opt(1997, 1, 19) .unwrap() .and_hms_opt(8, 17, 48) .unwrap() - .and_utc(); + .and_local_timezone(FixedOffset::east_opt(2 * 3600).unwrap()) + .unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn test_datetime_with_timezone() { + let actual = parse_datetime("1997-01-19 08:17:48 BRT").unwrap(); + let expected = NaiveDate::from_ymd_opt(1997, 1, 19) + .unwrap() + .and_hms_opt(8, 17, 48) + .unwrap() + .and_local_timezone(FixedOffset::west_opt(3 * 3600).unwrap()) + .unwrap(); assert_eq!(actual, expected); } } From da0af495e83e0c7b988036a2efa2679578456d97 Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Sun, 25 May 2025 21:14:39 +0200 Subject: [PATCH 2/3] test: Replace %+ format with explicit one %+ is an extension that jiff doesn't support (if we still want to convert parse_datetime in the future, and since these are tests, it's probably better to use an explicit format. --- src/items/mod.rs | 2 +- src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/items/mod.rs b/src/items/mod.rs index 51fe78a..2ce2810 100644 --- a/src/items/mod.rs +++ b/src/items/mod.rs @@ -518,7 +518,7 @@ mod tests { // https://github.com/uutils/coreutils/issues/5177 assert_eq!( "2023-07-27T13:53:54+00:00", - test_eq_fmt("%+", "@1690466034") + test_eq_fmt("%Y-%m-%dT%H:%M:%S%:z", "@1690466034") ); // https://github.com/uutils/coreutils/issues/6398 diff --git a/src/lib.rs b/src/lib.rs index 8674238..4b8a03d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -666,7 +666,7 @@ mod tests { assert_eq!( parse_datetime("28 feb 2023 + 1 day") .unwrap() - .format("%+") + .format("%Y-%m-%dT%H:%M:%S%:z") .to_string(), "2023-03-01T00:00:00+00:00" ); @@ -678,7 +678,7 @@ mod tests { assert_eq!( parse_datetime("2024-01-31 + 1 month") .unwrap() - .format("%+") + .format("%Y-%m-%dT%H:%M:%S%:z") .to_string(), "2024-03-02T00:00:00+00:00", ); @@ -686,7 +686,7 @@ mod tests { assert_eq!( parse_datetime("2024-02-29 + 1 month") .unwrap() - .format("%+") + .format("%Y-%m-%dT%H:%M:%S%:z") .to_string(), "2024-03-29T00:00:00+00:00", ); From a9676626bef0f99ab46437bbb037fffd521bbfaf Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Sun, 25 May 2025 21:15:53 +0200 Subject: [PATCH 3/3] src/items: Fix timezone tests The current tests look wrong ("%Z" shouldn't print an offset, but this accidentally works due to the way the parsing logic operates), fix that. --- src/items/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/items/mod.rs b/src/items/mod.rs index 2ce2810..2e12085 100644 --- a/src/items/mod.rs +++ b/src/items/mod.rs @@ -527,12 +527,12 @@ mod tests { assert_eq!( "2024-07-17 06:14:49 +00:00", - test_eq_fmt("%Y-%m-%d %H:%M:%S %Z", "Jul 17 06:14:49 2024 GMT"), + test_eq_fmt("%Y-%m-%d %H:%M:%S %:z", "Jul 17 06:14:49 2024 GMT"), ); assert_eq!( "2024-07-17 06:14:49 -03:00", - test_eq_fmt("%Y-%m-%d %H:%M:%S %Z", "Jul 17 06:14:49 2024 BRT"), + test_eq_fmt("%Y-%m-%d %H:%M:%S %:z", "Jul 17 06:14:49 2024 BRT"), ); } }