Add time_with_fold

This commit is contained in:
Paul Ganssle 2018-04-09 08:00:36 -04:00
parent c49bbe4549
commit f5e0785654
No known key found for this signature in database
GPG key ID: CD54FCE3D964BEFB
3 changed files with 47 additions and 6 deletions

View file

@ -93,6 +93,25 @@ impl PyTime {
Py::from_owned_ptr_or_err(py, ptr)
}
}
#[cfg(Py_3_6)]
pub fn new_with_fold(py: Python, hour: u32, minute: u32, second: u32,
microsecond: u32, tzinfo: &PyObject,
fold: bool) -> PyResult<Py<PyTime>> {
let h = hour as c_int;
let m = minute as c_int;
let s = second as c_int;
let u = microsecond as c_int;
let f = fold as c_int;
unsafe {
let ptr = PyDateTimeAPI.Time_FromTimeAndFold.unwrap()
(h, m, s, u, tzinfo.as_ptr(), f, PyDateTimeAPI.TimeType);
Py::from_owned_ptr_or_err(py, ptr)
}
}
}
// datetime.tzinfo bindings

View file

@ -8,10 +8,17 @@ use pyo3::prelude::{PyTuple, PyDict};
use pyo3::prelude::{PyDate, PyTime, PyDateTime, PyDelta, PyTzInfo};
macro_rules! to_pyobject {
($py:expr, $o:ident) => (match $o {
Some(t) => t.to_object($py),
None => $py.None()
})
}
#[py::modinit(datetime)]
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "make_date")]
fn make_date(py: Python, year: u32, month: u32, day: u32) -> PyResult<Py<PyDate>> {
PyDate::new(py, year, month, day)
@ -27,11 +34,7 @@ fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "make_time")]
fn make_time(py: Python, hour: u32, minute: u32, second: u32,
microsecond: u32, tzinfo: Option<&PyTzInfo>) -> PyResult<Py<PyTime>> {
let tzi: PyObject = match tzinfo {
Some(t) => t.to_object(py),
None => py.None(),
};
let tzi: PyObject = to_pyobject!(py, tzinfo);
PyTime::new(py, hour, minute, second, microsecond, &tzi)
}
@ -65,5 +68,15 @@ fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
PyDateTime::from_timestamp(py, &args.to_object(py), &kwargs.to_object(py))
}
#[cfg(Py_3_6)]
#[pyfn(m, "time_with_fold")]
fn time_with_fold(py: Python, hour: u32, minute: u32, second: u32,
microsecond: u32, tzinfo: Option<&PyTzInfo>,
fold: bool) -> PyResult<Py<PyTime>> {
let tzi = to_pyobject!(py, tzinfo);
PyTime::new_with_fold(py, hour, minute, second, microsecond, &tzi, fold)
}
Ok(())
}

View file

@ -16,6 +16,8 @@ MIN_SECONDS = int(pdt.timedelta.min.total_seconds())
MAX_MICROSECONDS = int(pdt.timedelta.max.total_seconds() * 1e6)
MIN_MICROSECONDS = int(pdt.timedelta.min.total_seconds() * 1e6)
HAS_FOLD = getattr(pdt.datetime, 'fold', False)
# Helper functions
def get_timestamp(dt):
@ -51,6 +53,13 @@ def test_time(args, kwargs):
assert act.tzinfo is exp.tzinfo
@pytest.mark.skipif(not HAS_FOLD, reason="Feature not available before 3.6")
@pytest.mark.parametrize('fold', [False, True])
def test_time_fold(fold):
t = rdt.time_with_fold(0, 0, 0, 0, None, fold)
assert t.fold == fold
@pytest.mark.xfail
@pytest.mark.parametrize('args', [
(-1, 0, 0, 0),