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
44
45
46
47
48
49
50
51
use crate::{DateTime, Time};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::ops::Deref;
impl<'de> Deserialize<'de> for DateTime {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let time = Time::deserialize(deserializer)?;
let secs = time.epoch_time.trunc() as i64;
let msecs = time.epoch_time.fract().abs() as u32;
let naive = chrono::NaiveDateTime::from_timestamp(secs, msecs * 1_000_000);
let dt = chrono::DateTime::<chrono::Utc>::from_utc(naive, chrono::Utc);
Ok(DateTime(dt))
}
}
impl Serialize for DateTime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let reql_type = String::from("TIME");
let epoch_time = {
let t = format!(
"{}.{}",
self.0.timestamp(),
self.0.timestamp_subsec_millis()
);
t.parse().unwrap()
};
let timezone = String::from("+00:00");
let time = Time {
reql_type,
epoch_time,
timezone,
};
time.serialize(serializer)
}
}
impl Deref for DateTime {
type Target = chrono::DateTime<chrono::Utc>;
fn deref(&self) -> &Self::Target {
&self.0
}
}