Skip to main content

serde_datetime/chrono/
ts_microseconds.rs

1use core::fmt;
2
3use chrono::{DateTime, TimeZone as _, Utc};
4use serde::{de, ser};
5
6use super::lib_copy::serde_from;
7
8pub(crate) struct MicroSecondsTimestampVisitor;
9
10pub fn serialize<S>(dt: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
11where
12    S: ser::Serializer,
13{
14    serializer.serialize_i64(dt.timestamp_micros())
15}
16
17pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
18where
19    D: de::Deserializer<'de>,
20{
21    d.deserialize_i64(MicroSecondsTimestampVisitor)
22        .map(|dt| dt.with_timezone(&Utc))
23}
24
25impl<'de> de::Visitor<'de> for MicroSecondsTimestampVisitor {
26    type Value = DateTime<Utc>;
27
28    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
29        formatter.write_str("a unix timestamp in microseconds")
30    }
31
32    /// Deserialize a timestamp in microseconds since the epoch
33    fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>
34    where
35        E: de::Error,
36    {
37        serde_from(
38            Utc.timestamp_opt(value / 1_000_000, ((value % 1_000_000) * 1_000) as u32),
39            &value,
40        )
41    }
42
43    /// Deserialize a timestamp in microseconds since the epoch
44    fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E>
45    where
46        E: de::Error,
47    {
48        serde_from(
49            Utc.timestamp_opt(
50                (value / 1_000_000) as i64,
51                ((value % 1_000_000) * 1_000) as u32,
52            ),
53            &value,
54        )
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use chrono::{DateTime, NaiveDate, Utc};
61    use serde::{Deserialize, Serialize};
62    use serde_json::json;
63
64    use crate::chrono::ts_microseconds;
65
66    #[test]
67    fn test_ts_microseconds() -> Result<(), serde_json::Error> {
68        #[derive(Deserialize, Serialize, Debug)]
69        struct S {
70            #[serde(with = "ts_microseconds")]
71            time: DateTime<Utc>,
72        }
73
74        //
75        let s: S = serde_json::from_str(r#"{ "time": 1609459200999999 }"#)?;
76        assert_eq!(
77            s.time,
78            DateTime::<Utc>::from_naive_utc_and_offset(
79                NaiveDate::from_ymd_opt(2021, 1, 1)
80                    .expect("")
81                    .and_hms_micro_opt(0, 0, 0, 999999)
82                    .expect(""),
83                Utc,
84            )
85        );
86
87        //
88        let s = S {
89            time: DateTime::from_naive_utc_and_offset(
90                NaiveDate::from_ymd_opt(2021, 1, 1)
91                    .expect("")
92                    .and_hms_micro_opt(0, 0, 0, 999999)
93                    .expect(""),
94                Utc,
95            ),
96        };
97        assert_eq!(
98            serde_json::to_value(s)?,
99            json!({ "time": 1609459200999999_u64 })
100        );
101
102        Ok(())
103    }
104}