-
Notifications
You must be signed in to change notification settings - Fork 778
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
Conversion between chrono_tz::Tz and zoneinfo.ZoneInfo #3730
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
`chrono-tz` feature allowing conversion between `chrono_tz::Tz` and `zoneinfo.ZoneInfo` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#![cfg(all(Py_3_9, feature = "chrono-tz"))] | ||
|
||
//! Conversions to and from [chrono-tz](https://docs.rs/chrono-tz/)’s `Tz`. | ||
//! | ||
//! This feature requires at least Python 3.9. | ||
//! | ||
//! # Setup | ||
//! | ||
//! To use this feature, add this to your **`Cargo.toml`**: | ||
//! | ||
//! ```toml | ||
//! [dependencies] | ||
//! chrono-tz = "0.8" | ||
#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"chrono-tz\"] }")] | ||
//! ``` | ||
//! | ||
//! Note that you must use compatible versions of chrono, chrono-tz and PyO3. | ||
//! The required chrono version may vary based on the version of PyO3. | ||
//! | ||
//! # Example: Convert a `zoneinfo.ZoneInfo` to chrono-tz's `Tz` | ||
//! | ||
//! ```rust,no_run | ||
//! use chrono_tz::Tz; | ||
//! use pyo3::{Python, ToPyObject}; | ||
//! | ||
//! fn main() { | ||
//! pyo3::prepare_freethreaded_python(); | ||
//! Python::with_gil(|py| { | ||
//! // Convert to Python | ||
//! let py_tzinfo = Tz::Europe__Paris.to_object(py); | ||
//! // Convert back to Rust | ||
//! assert_eq!(py_tzinfo.extract::<Tz>(py).unwrap(), Tz::Europe__Paris); | ||
//! }); | ||
//! } | ||
//! ``` | ||
use crate::exceptions::PyValueError; | ||
use crate::sync::GILOnceCell; | ||
use crate::types::PyType; | ||
use crate::{intern, FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, Python, ToPyObject}; | ||
use chrono_tz::Tz; | ||
use std::str::FromStr; | ||
|
||
impl ToPyObject for Tz { | ||
fn to_object(&self, py: Python<'_>) -> PyObject { | ||
static ZONE_INFO: GILOnceCell<Py<PyType>> = GILOnceCell::new(); | ||
ZONE_INFO | ||
.get_or_try_init_type_ref(py, "zoneinfo", "ZoneInfo") | ||
.unwrap() | ||
.call1((self.name(),)) | ||
.unwrap() | ||
.into() | ||
} | ||
} | ||
|
||
impl IntoPy<PyObject> for Tz { | ||
fn into_py(self, py: Python<'_>) -> PyObject { | ||
self.to_object(py) | ||
} | ||
} | ||
|
||
impl FromPyObject<'_> for Tz { | ||
fn extract(ob: &PyAny) -> PyResult<Tz> { | ||
Tz::from_str(ob.getattr(intern!(ob.py(), "key"))?.extract()?) | ||
.map_err(|e| PyValueError::new_err(e.to_string())) | ||
} | ||
} | ||
|
||
#[cfg(all(test, not(windows)))] // Troubles loading timezones on Windows | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_frompyobject() { | ||
Python::with_gil(|py| { | ||
assert_eq!( | ||
new_zoneinfo(py, "Europe/Paris").extract::<Tz>().unwrap(), | ||
Tz::Europe__Paris | ||
); | ||
assert_eq!(new_zoneinfo(py, "UTC").extract::<Tz>().unwrap(), Tz::UTC); | ||
assert_eq!( | ||
new_zoneinfo(py, "Etc/GMT-5").extract::<Tz>().unwrap(), | ||
Tz::Etc__GMTMinus5 | ||
); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_topyobject() { | ||
Python::with_gil(|py| { | ||
let assert_eq = |l: PyObject, r: &PyAny| { | ||
assert!(l.as_ref(py).eq(r).unwrap()); | ||
}; | ||
|
||
assert_eq( | ||
Tz::Europe__Paris.to_object(py), | ||
new_zoneinfo(py, "Europe/Paris"), | ||
); | ||
assert_eq(Tz::UTC.to_object(py), new_zoneinfo(py, "UTC")); | ||
assert_eq( | ||
Tz::Etc__GMTMinus5.to_object(py), | ||
new_zoneinfo(py, "Etc/GMT-5"), | ||
); | ||
}); | ||
} | ||
|
||
fn new_zoneinfo<'a>(py: Python<'a>, name: &str) -> &'a PyAny { | ||
zoneinfo_class(py).call1((name,)).unwrap() | ||
} | ||
|
||
fn zoneinfo_class(py: Python<'_>) -> &PyAny { | ||
py.import("zoneinfo").unwrap().getattr("ZoneInfo").unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
pub mod anyhow; | ||
pub mod chrono; | ||
pub mod chrono_tz; | ||
pub mod either; | ||
pub mod eyre; | ||
pub mod hashbrown; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to make sure we are compatible with MSRV