Merge pull request #3679 from davidhewitt/datetime2

implement datetime traits for `Bound`
This commit is contained in:
Adam Reichold 2023-12-23 14:54:30 +00:00 committed by GitHub
commit e99058a442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 126 additions and 11 deletions

View File

@ -0,0 +1 @@
Add lifetime parameter to `PyTzInfoAccess` trait and change the return type of `PyTzInfoAccess::get_tzinfo` to `Option<Bound<PyTzInfo>>`. For the deprecated gil-ref API, the trait is now implemented for `&'py PyTime` and `&'py PyDateTime` instead of `PyTime` and `PyDate`.

View File

@ -160,12 +160,12 @@ fn datetime_from_timestamp<'p>(
}
#[pyfunction]
fn get_datetime_tzinfo(dt: &PyDateTime) -> Option<&PyTzInfo> {
fn get_datetime_tzinfo(dt: &PyDateTime) -> Option<Bound<'_, PyTzInfo>> {
dt.get_tzinfo()
}
#[pyfunction]
fn get_time_tzinfo(dt: &PyTime) -> Option<&PyTzInfo> {
fn get_time_tzinfo(dt: &PyTime) -> Option<Bound<'_, PyTzInfo>> {
dt.get_tzinfo()
}

View File

@ -45,6 +45,8 @@ use crate::exceptions::{PyTypeError, PyUserWarning, PyValueError};
#[cfg(Py_LIMITED_API)]
use crate::sync::GILOnceCell;
#[cfg(not(Py_LIMITED_API))]
use crate::types::any::PyAnyMethods;
#[cfg(not(Py_LIMITED_API))]
use crate::types::datetime::timezone_from_offset;
#[cfg(not(Py_LIMITED_API))]
use crate::types::{

View File

@ -19,9 +19,11 @@ use crate::ffi::{
PyDateTime_TIME_GET_FOLD, PyDateTime_TIME_GET_HOUR, PyDateTime_TIME_GET_MICROSECOND,
PyDateTime_TIME_GET_MINUTE, PyDateTime_TIME_GET_SECOND,
};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::PyNativeType;
use crate::types::any::PyAnyMethods;
use crate::types::PyTuple;
use crate::{AsPyPointer, IntoPy, Py, PyAny, Python};
use crate::{Bound, IntoPy, Py, PyAny, Python};
use std::os::raw::c_int;
#[cfg(feature = "chrono")]
use std::ptr;
@ -160,13 +162,13 @@ pub trait PyTimeAccess {
}
/// Trait for accessing the components of a struct containing a tzinfo.
pub trait PyTzInfoAccess {
pub trait PyTzInfoAccess<'py> {
/// Returns the tzinfo (which may be None).
///
/// Implementations should conform to the upstream documentation:
/// <https://docs.python.org/3/c-api/datetime.html#c.PyDateTime_DATE_GET_TZINFO>
/// <https://docs.python.org/3/c-api/datetime.html#c.PyDateTime_TIME_GET_TZINFO>
fn get_tzinfo(&self) -> Option<&PyTzInfo>;
fn get_tzinfo(&self) -> Option<Bound<'py, PyTzInfo>>;
}
/// Bindings around `datetime.date`
@ -211,6 +213,20 @@ impl PyDate {
}
impl PyDateAccess for PyDate {
fn get_year(&self) -> i32 {
Bound::borrowed_from_gil_ref(&self).get_year()
}
fn get_month(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_month()
}
fn get_day(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_day()
}
}
impl PyDateAccess for Bound<'_, PyDate> {
fn get_year(&self) -> i32 {
unsafe { PyDateTime_GET_YEAR(self.as_ptr()) }
}
@ -325,6 +341,20 @@ impl PyDateTime {
}
impl PyDateAccess for PyDateTime {
fn get_year(&self) -> i32 {
Bound::borrowed_from_gil_ref(&self).get_year()
}
fn get_month(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_month()
}
fn get_day(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_day()
}
}
impl PyDateAccess for Bound<'_, PyDateTime> {
fn get_year(&self) -> i32 {
unsafe { PyDateTime_GET_YEAR(self.as_ptr()) }
}
@ -339,6 +369,28 @@ impl PyDateAccess for PyDateTime {
}
impl PyTimeAccess for PyDateTime {
fn get_hour(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_hour()
}
fn get_minute(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_minute()
}
fn get_second(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_second()
}
fn get_microsecond(&self) -> u32 {
Bound::borrowed_from_gil_ref(&self).get_microsecond()
}
fn get_fold(&self) -> bool {
Bound::borrowed_from_gil_ref(&self).get_fold()
}
}
impl PyTimeAccess for Bound<'_, PyDateTime> {
fn get_hour(&self) -> u8 {
unsafe { PyDateTime_DATE_GET_HOUR(self.as_ptr()) as u8 }
}
@ -360,12 +412,24 @@ impl PyTimeAccess for PyDateTime {
}
}
impl PyTzInfoAccess for PyDateTime {
fn get_tzinfo(&self) -> Option<&PyTzInfo> {
impl<'py> PyTzInfoAccess<'py> for &'py PyDateTime {
fn get_tzinfo(&self) -> Option<Bound<'py, PyTzInfo>> {
Bound::borrowed_from_gil_ref(self).get_tzinfo()
}
}
impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyDateTime> {
fn get_tzinfo(&self) -> Option<Bound<'py, PyTzInfo>> {
let ptr = self.as_ptr() as *mut ffi::PyDateTime_DateTime;
unsafe {
if (*ptr).hastzinfo != 0 {
Some(self.py().from_borrowed_ptr((*ptr).tzinfo))
Some(
(*ptr)
.tzinfo
.assume_borrowed(self.py())
.to_owned()
.downcast_into_unchecked(),
)
} else {
None
}
@ -435,6 +499,28 @@ impl PyTime {
}
impl PyTimeAccess for PyTime {
fn get_hour(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_hour()
}
fn get_minute(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_minute()
}
fn get_second(&self) -> u8 {
Bound::borrowed_from_gil_ref(&self).get_second()
}
fn get_microsecond(&self) -> u32 {
Bound::borrowed_from_gil_ref(&self).get_microsecond()
}
fn get_fold(&self) -> bool {
Bound::borrowed_from_gil_ref(&self).get_fold()
}
}
impl PyTimeAccess for Bound<'_, PyTime> {
fn get_hour(&self) -> u8 {
unsafe { PyDateTime_TIME_GET_HOUR(self.as_ptr()) as u8 }
}
@ -456,12 +542,24 @@ impl PyTimeAccess for PyTime {
}
}
impl PyTzInfoAccess for PyTime {
fn get_tzinfo(&self) -> Option<&PyTzInfo> {
impl<'py> PyTzInfoAccess<'py> for &'py PyTime {
fn get_tzinfo(&self) -> Option<Bound<'py, PyTzInfo>> {
Bound::borrowed_from_gil_ref(self).get_tzinfo()
}
}
impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyTime> {
fn get_tzinfo(&self) -> Option<Bound<'py, PyTzInfo>> {
let ptr = self.as_ptr() as *mut ffi::PyDateTime_Time;
unsafe {
if (*ptr).hastzinfo != 0 {
Some(self.py().from_borrowed_ptr((*ptr).tzinfo))
Some(
(*ptr)
.tzinfo
.assume_borrowed(self.py())
.to_owned()
.downcast_into_unchecked(),
)
} else {
None
}
@ -536,6 +634,20 @@ impl PyDelta {
}
impl PyDeltaAccess for PyDelta {
fn get_days(&self) -> i32 {
Bound::borrowed_from_gil_ref(&self).get_days()
}
fn get_seconds(&self) -> i32 {
Bound::borrowed_from_gil_ref(&self).get_seconds()
}
fn get_microseconds(&self) -> i32 {
Bound::borrowed_from_gil_ref(&self).get_microseconds()
}
}
impl PyDeltaAccess for Bound<'_, PyDelta> {
fn get_days(&self) -> i32 {
unsafe { PyDateTime_DELTA_GET_DAYS(self.as_ptr()) }
}