Skip to content
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
8 changes: 8 additions & 0 deletions newsfragments/5949.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Added the following functions:

* PyDate_FromDate
* PyDateTime_FromDateAndTime
* PyDateTime_FromDateAndTimeAndFold
* PyTime_FromTime
* PyTime_FromTimeAndFold
* PyDelta_FromDSU
112 changes: 104 additions & 8 deletions pyo3-ffi/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)))]
Expand Down Expand Up @@ -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 <https://github.com/python/cpython/blob/3.10/Include/datetime.h#L226-L228>
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 <https://github.com/python/cpython/blob/3.10/Include/datetime.h#L230-L232>
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())
Expand Down
Loading