diff --git a/Cargo.toml b/Cargo.toml index c6cd5141..f2167f7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ spin = "0.4.9" num-traits = "0.2.5" pyo3cls = { path = "pyo3cls", version = "=0.4.1" } mashup = "0.1.5" +lazy_static = "1.0" [dev-dependencies] docmatic = "0.1.2" diff --git a/src/lib.rs b/src/lib.rs index 01074536..842ab0e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,6 +129,8 @@ extern crate spin; #[doc(hidden)] pub extern crate mashup; +#[macro_use] extern crate lazy_static; + #[cfg(not(Py_3))] mod ffi2; diff --git a/src/objects/datetime.rs b/src/objects/datetime.rs new file mode 100644 index 00000000..ec0e166c --- /dev/null +++ b/src/objects/datetime.rs @@ -0,0 +1,27 @@ +use object::PyObject; +use std::os::raw::c_int; +use ffi::{PyDateTime_CAPI, PyDateTime_IMPORT}; +use python::{Python, ToPyPointer}; +use instance::Py; + +pub struct PyDate(PyObject); +pyobject_convert!(PyDate); +pyobject_nativetype!(PyDate, PyDateTime_Date, PyDate_Check); + +lazy_static! { + static ref PyDateTimeAPI: PyDateTime_CAPI = unsafe { PyDateTime_IMPORT() }; +} + +impl PyDate { + pub fn new(py: Python, year: u32, month: u32, day: u32) -> Py { + let y = year as c_int; + let m = month as c_int; + let d = day as c_int; + + unsafe { + let ptr = PyDateTimeAPI.Date_FromDate.unwrap()(y, m, d, PyDateTimeAPI.DateType); + Py::from_owned_ptr_or_panic(ptr) + } + } +} + diff --git a/src/objects/mod.rs b/src/objects/mod.rs index 2a4a1a13..62b098d6 100644 --- a/src/objects/mod.rs +++ b/src/objects/mod.rs @@ -5,6 +5,7 @@ mod exc_impl; pub use self::boolobject::PyBool; pub use self::bytearray::PyByteArray; +pub use self::datetime::{PyDate}; pub use self::dict::PyDict; pub use self::floatob::PyFloat; pub use self::iterator::PyIterator; @@ -185,6 +186,7 @@ pyobject_native_type_convert!(PyObjectRef, ffi::PyBaseObject_Type, ffi::PyObject mod boolobject; mod bytearray; +mod datetime; mod dict; pub mod exc; mod floatob;