diff --git a/newsfragments/5949.added.md b/newsfragments/5949.added.md new file mode 100644 index 00000000000..2e92eccb849 --- /dev/null +++ b/newsfragments/5949.added.md @@ -0,0 +1,8 @@ +Added the following functions: + +* PyDate_FromDate +* PyDateTime_FromDateAndTime +* PyDateTime_FromDateAndTimeAndFold +* PyTime_FromTime +* PyTime_FromTimeAndFold +* PyDelta_FromDSU diff --git a/pyo3-ffi/src/datetime.rs b/pyo3-ffi/src/datetime.rs index aa786f7f5e4..aecdbad9822 100644 --- a/pyo3-ffi/src/datetime.rs +++ b/pyo3-ffi/src/datetime.rs @@ -8,7 +8,7 @@ use crate::PyCapsule_Import; #[cfg(GraalPy)] use crate::{PyLong_AsLong, PyLong_Check, PyObject_GetAttrString, Py_DecRef}; -use crate::{PyObject, PyObject_TypeCheck, PyTypeObject, Py_TYPE}; +use crate::{PyObject, PyObject_TypeCheck, PyTypeObject, Py_None, Py_TYPE}; use std::ffi::c_char; use std::ffi::c_int; use std::ptr; @@ -33,7 +33,12 @@ pub struct PyDateTime_Delta { pub microseconds: c_int, } -// skipped non-limited PyDateTime_TZInfo +#[repr(C)] +#[derive(Debug)] +pub struct PyDateTime_TZInfo { + pub ob_base: PyObject, +} + // skipped non-limited _PyDateTime_BaseTZInfo #[cfg(not(any(PyPy, GraalPy)))] @@ -697,12 +702,103 @@ pub unsafe fn PyTZInfo_CheckExact(op: *mut PyObject) -> c_int { (Py_TYPE(op) == (*PyDateTimeAPI()).TZInfoType) as c_int } -// skipped non-limited PyDate_FromDate -// skipped non-limited PyDateTime_FromDateAndTime -// skipped non-limited PyDateTime_FromDateAndTimeAndFold -// skipped non-limited PyTime_FromTime -// skipped non-limited PyTime_FromTimeAndFold -// skipped non-limited PyDelta_FromDSU +pub unsafe fn PyDate_FromDate(year: c_int, month: c_int, day: c_int) -> *mut PyObject { + ((*PyDateTimeAPI()).Date_FromDate)(year, month, day, (*PyDateTimeAPI()).DateType) +} + +#[allow(clippy::too_many_arguments)] +/// See +pub unsafe fn PyDateTime_FromDateAndTime( + year: c_int, + month: c_int, + day: c_int, + hour: c_int, + minute: c_int, + second: c_int, + microsecond: c_int, +) -> *mut PyObject { + ((*PyDateTimeAPI()).DateTime_FromDateAndTime)( + year, + month, + day, + hour, + minute, + second, + microsecond, + Py_None(), + (*PyDateTimeAPI()).DateTimeType, + ) +} + +#[allow(clippy::too_many_arguments)] +/// See +pub unsafe fn PyDateTime_FromDateAndTimeAndFold( + year: c_int, + month: c_int, + day: c_int, + hour: c_int, + minute: c_int, + second: c_int, + microsecond: c_int, + fold: c_int, +) -> *mut PyObject { + ((*PyDateTimeAPI()).DateTime_FromDateAndTimeAndFold)( + year, + month, + day, + hour, + minute, + second, + microsecond, + Py_None(), + fold, + (*PyDateTimeAPI()).DateTimeType, + ) +} + +pub unsafe fn PyTime_FromTime( + hour: c_int, + minute: c_int, + second: c_int, + microsecond: c_int, +) -> *mut PyObject { + ((*PyDateTimeAPI()).Time_FromTime)( + hour, + minute, + second, + microsecond, + Py_None(), + (*PyDateTimeAPI()).TimeType, + ) +} + +pub unsafe fn PyTime_FromTimeAndFold( + hour: c_int, + minute: c_int, + second: c_int, + microsecond: c_int, + fold: c_int, +) -> *mut PyObject { + ((*PyDateTimeAPI()).Time_FromTimeAndFold)( + hour, + minute, + second, + microsecond, + Py_None(), + fold, + (*PyDateTimeAPI()).TimeType, + ) +} + +pub unsafe fn PyDelta_FromDSU(days: c_int, seconds: c_int, microseconds: c_int) -> *mut PyObject { + ((*PyDateTimeAPI()).Delta_FromDelta)( + days, + seconds, + microseconds, + 1, + (*PyDateTimeAPI()).DeltaType, + ) +} pub unsafe fn PyTimeZone_FromOffset(offset: *mut PyObject) -> *mut PyObject { ((*PyDateTimeAPI()).TimeZone_FromTimeZone)(offset, std::ptr::null_mut())