Skip to content

Commit

Permalink
Add jiff doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Jan 13, 2025
1 parent 8935b87 commit 0dba0f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
44 changes: 43 additions & 1 deletion src/conversions/jiff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
#![cfg(feature = "jiff-01")]

//! Conversions to and from [jiff](https://docs.rs/jiff/)’s `Span`, `SignedDuration`, `TimeZone`,
//! `Offset`, `Date`, `Time`, `DateTime`, `Zoned`, and `Timestamp`.
//!
//! # Setup
//!
//! To use this feature, add this to your **`Cargo.toml`**:
//!
//! ```toml
//! [dependencies]
//! jiff = "0.1"
#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"jiff-01\"] }")]
//! ```
//!
//! Note that you must use compatible versions of jiff and PyO3.
//! The required jiff version may vary based on the version of PyO3. Jiff also requires a MSRV
//! of 1.70.
//!
//! # Example: Convert a `datetime.datetime` to jiff `Zoned`
//!
//! ```rust
//! # use jiff_01 as jiff;
//! use jiff::{Zoned, ToSpan};
//! use pyo3::{Python, PyResult, IntoPyObject, types::PyAnyMethods};
//!
//! fn main() -> PyResult<()> {
//! pyo3::prepare_freethreaded_python();
//! Python::with_gil(|py| {
//! // Build some jiff values
//! let jiff_zoned = Zoned::now();
//! let jiff_span = 1.second();
//! // Convert them to Python
//! let py_datetime = jiff_zoned.into_pyobject(py)?;
//! let py_timedelta = jiff_span.into_pyobject(py)?;
//! // Do an operation in Python
//! let py_sum = py_datetime.call_method1("__add__", (py_timedelta,))?;
//! // Convert back to Rust
//! let jiff_sum: Zoned = py_sum.extract()?;
//! println!("Zoned: {}", jiff_sum);
//! Ok(())
//! })
//! }
//! ```
use crate::exceptions::{PyTypeError, PyValueError};
use crate::pybacked::PyBackedStr;
use crate::sync::GILOnceCell;
Expand Down Expand Up @@ -69,7 +111,7 @@ fn datetime_to_pydatetime<'py>(
}

#[cfg(not(Py_LIMITED_API))]
fn pytime_to_time(time: &dyn PyTimeAccess) -> PyResult<Time> {
fn pytime_to_time(time: &impl PyTimeAccess) -> PyResult<Time> {
Ok(Time::new(
time.get_hour().try_into()?,
time.get_minute().try_into()?,
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod either;
pub mod eyre;
pub mod hashbrown;
pub mod indexmap;
mod jiff;
pub mod jiff;
pub mod num_bigint;
pub mod num_complex;
pub mod num_rational;
Expand Down

0 comments on commit 0dba0f5

Please sign in to comment.