Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/6606.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix round trips from Jiff `ZonedDateTime` to Python `datetime` and back.
44 changes: 27 additions & 17 deletions src/conversions/jiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::Output, Self::Error> {
fn fold(zoned: &ZonedDateTime) -> Option<bool> {
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))
}
}

Expand Down Expand Up @@ -892,6 +883,25 @@ 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();
assert!(datetime.get_fold());
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() {
Expand Down Expand Up @@ -1276,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();
Expand Down
Loading