serde_datetime/chrono/
ts_microseconds.rs1use 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 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 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 std::error::Error;
61
62 use chrono::{DateTime, NaiveDate, Utc};
63 use serde::{Deserialize, Serialize};
64 use serde_json::json;
65
66 use crate::chrono::ts_microseconds;
67
68 #[test]
69 fn test_ts_microseconds() -> Result<(), Box<dyn Error>> {
70 #[derive(Deserialize, Serialize, Debug)]
71 struct S {
72 #[serde(with = "ts_microseconds")]
73 time: DateTime<Utc>,
74 }
75
76 let s: S = serde_json::from_str(r#"{ "time": 1609459200999999 }"#)?;
78 assert_eq!(
79 s.time,
80 DateTime::<Utc>::from_utc(
81 NaiveDate::from_ymd_opt(2021, 1, 1)
82 .expect("")
83 .and_hms_micro_opt(0, 0, 0, 999999)
84 .expect(""),
85 Utc,
86 )
87 );
88
89 let s = S {
91 time: DateTime::from_utc(
92 NaiveDate::from_ymd_opt(2021, 1, 1)
93 .expect("")
94 .and_hms_micro_opt(0, 0, 0, 999999)
95 .expect(""),
96 Utc,
97 ),
98 };
99 assert_eq!(
100 serde_json::to_value(&s)?,
101 json!({ "time": 1609459200999999_u64 })
102 );
103
104 Ok(())
105 }
106}