Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use datetime.fold to distinguish ambiguous datetimes when converting #4791

Merged
merged 2 commits into from
Dec 16, 2024
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/4791.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use `datetime.fold` to distinguish ambiguous datetimes when converting to and from `chrono::DateTime<Tz>`
44 changes: 34 additions & 10 deletions src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ use crate::sync::GILOnceCell;
use crate::types::any::PyAnyMethods;
#[cfg(not(Py_LIMITED_API))]
use crate::types::datetime::timezone_from_offset;
#[cfg(Py_LIMITED_API)]
use crate::types::IntoPyDict;
use crate::types::PyNone;
#[cfg(not(Py_LIMITED_API))]
use crate::types::{
Expand All @@ -61,7 +63,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)]
Expand Down Expand Up @@ -465,14 +468,21 @@ where
truncated_leap_second,
} = (&self.naive_local().time()).into();

let fold = matches!(
self.timezone().offset_from_local_datetime(&self.naive_local()),
LocalResult::Ambiguous(_, latest) if self.offset().fix() == latest.fix()
);

#[cfg(not(Py_LIMITED_API))]
let datetime = PyDateTime::new(py, year, month, day, hour, min, sec, micro, Some(tz))?;
let datetime =
PyDateTime::new_with_fold(py, year, month, day, hour, min, sec, micro, Some(tz), fold)?;

#[cfg(Py_LIMITED_API)]
let datetime = DatetimeTypes::try_get(py).and_then(|dt| {
dt.datetime
.bind(py)
.call1((year, month, day, hour, min, sec, micro, tz))
dt.datetime.bind(py).call(
(year, month, day, hour, min, sec, micro, tz),
Some(&[("fold", fold as u8)].into_py_dict(py)?),
)
})?;

if truncated_leap_second {
Expand Down Expand Up @@ -503,12 +513,26 @@ impl<Tz: TimeZone + for<'py> FromPyObject<'py>> FromPyObject<'_> for DateTime<Tz
));
};
let naive_dt = NaiveDateTime::new(py_date_to_naive_date(dt)?, py_time_to_naive_time(dt)?);
naive_dt.and_local_timezone(tz).single().ok_or_else(|| {
PyValueError::new_err(format!(
"The datetime {:?} contains an incompatible or ambiguous timezone",
match naive_dt.and_local_timezone(tz) {
LocalResult::Single(value) => 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::<usize>()? > 0;

if fold {
Ok(latest)
} else {
Ok(earliest)
}
}
LocalResult::None => Err(PyValueError::new_err(format!(
"The datetime {:?} contains an incompatible timezone",
dt
))
})
))),
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions src/conversions/chrono_tz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl FromPyObject<'_> for Tz {
#[cfg(all(test, not(windows)))] // Troubles loading timezones on Windows
mod tests {
use super::*;
use crate::prelude::PyAnyMethods;
use crate::Python;
use chrono::{DateTime, Utc};
use chrono_tz::Tz;

#[test]
fn test_frompyobject() {
Expand All @@ -114,6 +118,54 @@ mod tests {
});
}

#[test]
fn test_ambiguous_datetime_to_pyobject() {
let dates = [
DateTime::<Utc>::from_str("2020-10-24 23:00:00 UTC").unwrap(),
DateTime::<Utc>::from_str("2020-10-25 00:00:00 UTC").unwrap(),
DateTime::<Utc>::from_str("2020-10-25 01:00:00 UTC").unwrap(),
];

let dates = dates.map(|dt| dt.with_timezone(&Tz::Europe__London));

assert_eq!(
dates.map(|dt| dt.to_string()),
[
"2020-10-25 00:00:00 BST",
"2020-10-25 01:00:00 BST",
"2020-10-25 01:00:00 GMT"
]
);

let dates = Python::with_gil(|py| {
let pydates = dates.map(|dt| dt.into_pyobject(py).unwrap());
assert_eq!(
pydates
.clone()
.map(|dt| dt.getattr("hour").unwrap().extract::<usize>().unwrap()),
[0, 1, 1]
);

assert_eq!(
pydates
.clone()
.map(|dt| dt.getattr("fold").unwrap().extract::<usize>().unwrap() > 0),
[false, false, true]
);

pydates.map(|dt| dt.extract::<DateTime<Tz>>().unwrap())
});

assert_eq!(
dates.map(|dt| dt.to_string()),
[
"2020-10-25 00:00:00 BST",
"2020-10-25 01:00:00 BST",
"2020-10-25 01:00:00 GMT"
]
);
}

#[test]
#[cfg(not(Py_GIL_DISABLED))] // https://github.com/python/cpython/issues/116738#issuecomment-2404360445
fn test_into_pyobject() {
Expand Down
Loading