From fcf8d45077f3da1e397efa2ca80bc5771216542d Mon Sep 17 00:00:00 2001 From: Ravencentric <78981416+Ravencentric@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:12:56 +0530 Subject: [PATCH 1/3] Add a regression test for Jiff fold round trips --- src/conversions/jiff.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/conversions/jiff.rs b/src/conversions/jiff.rs index f4ec9577df5..37d8115c88f 100644 --- a/src/conversions/jiff.rs +++ b/src/conversions/jiff.rs @@ -892,6 +892,24 @@ mod tests { ); } + #[test] + #[cfg(not(windows))] + fn test_zoned_datetime_roundtrip_during_fold() { + let original: ZonedDateTime = "2024-11-03T01:30:00-05:00[America/New_York]" + .parse() + .unwrap(); + + Python::attach(|py| { + let datetime = (&original).into_pyobject(py).unwrap(); + let roundtripped: ZonedDateTime = datetime.extract().unwrap(); + + assert_eq!( + roundtripped.timestamp().as_second(), + original.timestamp().as_second(), + ); + }); + } + #[test] #[cfg(not(windows))] fn test_nonexistent_datetime_frompyobject_matches_python_timestamp() { From c2e5a9c32aae3628e619cf1c527945e489430c3a Mon Sep 17 00:00:00 2001 From: Ravencentric <78981416+Ravencentric@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:18:22 +0530 Subject: [PATCH 2/3] fix: jiff round trips through Python datetime --- newsfragments/6606.fixed.md | 1 + src/conversions/jiff.rs | 23 +++++++---------------- 2 files changed, 8 insertions(+), 16 deletions(-) create mode 100644 newsfragments/6606.fixed.md diff --git a/newsfragments/6606.fixed.md b/newsfragments/6606.fixed.md new file mode 100644 index 00000000000..d374384aa5b --- /dev/null +++ b/newsfragments/6606.fixed.md @@ -0,0 +1 @@ +Fix round trips from Jiff `ZonedDateTime` to Python `datetime` and back. diff --git a/src/conversions/jiff.rs b/src/conversions/jiff.rs index 37d8115c88f..23f3dba5413 100644 --- a/src/conversions/jiff.rs +++ b/src/conversions/jiff.rs @@ -299,23 +299,14 @@ impl<'py> IntoPyObject<'py> for &ZonedDateTime { const OUTPUT_TYPE: PyStaticExpr = PyDateTime::TYPE_HINT; fn into_pyobject(self, py: Python<'py>) -> Result { - fn fold(zoned: &ZonedDateTime) -> Option { - let prev = zoned.time_zone().preceding(zoned.timestamp()).next()?; - let next = zoned.time_zone().following(prev.timestamp()).next()?; - let start_of_current_offset = if next.timestamp() == zoned.timestamp() { - next.timestamp() - } else { - prev.timestamp() - }; - Some(zoned.timestamp() + (zoned.offset() - prev.offset()) <= start_of_current_offset) - } + let datetime = self.datetime(); + let time_zone = self.time_zone(); + let fold = matches!( + time_zone.to_ambiguous_timestamp(datetime).offset(), + AmbiguousOffset::Fold { after, .. } if self.offset() == after + ); - datetime_to_pydatetime( - py, - self.datetime(), - fold(self).unwrap_or(false), - Some(self.time_zone()), - ) + datetime_to_pydatetime(py, datetime, fold, Some(time_zone)) } } From 8ed89d96f878a6e2d71548de72211c3a326e437c Mon Sep 17 00:00:00 2001 From: Ravencentric <78981416+Ravencentric@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:06:47 +0530 Subject: [PATCH 3/3] expand minute range in test_zoned_datetime_roundtrip_around_timezone_transition to catch the same bug --- src/conversions/jiff.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/conversions/jiff.rs b/src/conversions/jiff.rs index 23f3dba5413..00cfe7eb5a7 100644 --- a/src/conversions/jiff.rs +++ b/src/conversions/jiff.rs @@ -892,6 +892,7 @@ mod tests { Python::attach(|py| { let datetime = (&original).into_pyobject(py).unwrap(); + assert!(datetime.get_fold()); let roundtripped: ZonedDateTime = datetime.extract().unwrap(); assert_eq!( @@ -1285,7 +1286,7 @@ mod tests { Just(&TimeZone::get("Australia/Sydney").unwrap()), ].prop_flat_map(|tz| (Just(tz), timezone_transitions(tz))), hour in -2i32..=2i32, - min in 0u32..=59u32, + min in -59i32..=59i32, ) { Python::attach(|py| { let transition_moment = transition.timestamp();