pyo3::conversions

Module jiff

Source
Available on crate feature jiff-01 only.
Expand description

Conversions to and from jiff’s Span, SignedDuration, TimeZone, Offset, Date, Time, DateTime, Zoned, and Timestamp.

§Setup

To use this feature, add this to your Cargo.toml:

[dependencies]
jiff = "0.1"
pyo3 = { version = "0.23.3", 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

use jiff::{Zoned, SignedDuration, 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 = SignedDuration::try_from(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(())
    })
}

Functions§

datetime_to_pydatetime 🔒 Non-Py_LIMITED_API
pytime_to_time 🔒 Non-Py_LIMITED_API
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here