From a87d9ae134cf87fb6ada0e9894be7ea70ad55c4e Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Wed, 11 Dec 2024 14:14:17 +0100 Subject: [PATCH] use `datetime.fold` to distinguish ambiguous datetimes when converting --- src/conversions/chrono.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/conversions/chrono.rs b/src/conversions/chrono.rs index 90f9c69761d..95f44a2c034 100644 --- a/src/conversions/chrono.rs +++ b/src/conversions/chrono.rs @@ -61,7 +61,8 @@ use crate::{intern, DowncastError}; use crate::{IntoPy, ToPyObject}; use chrono::offset::{FixedOffset, Utc}; use chrono::{ - DateTime, Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike, + DateTime, Datelike, Duration, LocalResult, NaiveDate, NaiveDateTime, NaiveTime, Offset, + TimeZone, Timelike, }; #[allow(deprecated)] @@ -493,12 +494,26 @@ impl FromPyObject<'py>> FromPyObject<'_> for DateTime Ok(value), + LocalResult::Ambiguous(earliest, latest) => { + #[cfg(not(Py_LIMITED_API))] + let fold = dt.get_fold(); + + #[cfg(Py_LIMITED_API)] + let fold = dt.getattr(intern!(dt.py(), "fold"))?.extract::()? > 0; + + if fold { + Ok(latest) + } else { + Ok(earliest) + } + } + LocalResult::None => Err(PyValueError::new_err(format!( + "The datetime {:?} contains an incompatible timezone", dt - )) - }) + ))), + } } }