forked from launchbadge/hedera-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp.rs
43 lines (37 loc) · 1.19 KB
/
timestamp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use chrono::{prelude::*, DateTime, Utc};
use derive_more::From;
use pyo3::{prelude::*, types};
use try_from::TryFrom;
#[derive(From)]
pub struct PyDateTime(Py<types::PyDateTime>);
impl IntoPyObject for PyDateTime {
fn into_object(self, py: Python) -> PyObject {
self.0.into_object(py)
}
}
impl TryFrom<DateTime<Utc>> for PyDateTime {
type Err = PyErr;
fn try_from(dt: DateTime<Utc>) -> PyResult<PyDateTime> {
// note: GIL should be acquired by this point and we don't know a safer way to get a py
// instance from inside a `#[getter]`
let py = unsafe { Python::assume_gil_acquired() };
let year = dt.year();
let month = dt.month() as u8; // 1..=12
let day = dt.day() as u8; // 1..=31
let hour = dt.hour() as u8; // 0..=23
let minute = dt.minute() as u8; // 0..=59
let second = dt.second() as u8; // 0..=59
let microsecond = (dt.nanosecond() % 1_000_000_000) / 1_000;
Ok(PyDateTime(types::PyDateTime::new(
py,
year,
month,
day,
hour,
minute,
second,
microsecond,
None,
)?))
}
}