1use core::fmt;
2
3use chrono::{DateTime, Datelike as _, NaiveDate, TimeZone as _, Timelike as _, Utc};
4use serde::{de, ser};
5
6use super::lib_copy::serde_from;
7
8pub(crate) struct FloatMicroSecondsTimestampVisitor;
9
10pub fn serialize<S>(dt: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
11where
12 S: ser::Serializer,
13{
14 let f64 = dt.timestamp() as f64 + f64::from(dt.timestamp_subsec_micros()) / 1_000_000_f64;
15 serializer.serialize_f64(f64)
16}
17
18pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
19where
20 D: de::Deserializer<'de>,
21{
22 d.deserialize_f64(FloatMicroSecondsTimestampVisitor)
23 .map(|dt| dt.with_timezone(&Utc))
24}
25
26impl<'de> de::Visitor<'de> for FloatMicroSecondsTimestampVisitor {
27 type Value = DateTime<Utc>;
28
29 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
30 formatter.write_str("a unix timestamp in float microseconds")
31 }
32
33 fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>
35 where
36 E: de::Error,
37 {
38 serde_from(Utc.timestamp_opt(value, 0), &value)
39 }
40
41 fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E>
43 where
44 E: de::Error,
45 {
46 serde_from(Utc.timestamp_opt(value as i64, 0), &value)
47 }
48
49 fn visit_f64<E>(self, value: f64) -> Result<DateTime<Utc>, E>
51 where
52 E: de::Error,
53 {
54 serde_from(
55 Utc.timestamp_opt(
56 value as i64,
57 ((value * 1_000_000_f64) as u64 % 1_000_000) as u32,
58 ),
59 &value,
60 )
61 .map(|dt| {
62 DateTime::from_utc(
63 NaiveDate::from_ymd_opt(dt.year(), dt.month(), dt.day())
64 .expect("")
65 .and_hms_micro_opt(dt.hour(), dt.minute(), dt.second(), dt.nanosecond())
66 .expect(""),
67 Utc,
68 )
69 })
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use std::error::Error;
76
77 use chrono::{DateTime, NaiveDate, Utc};
78 use serde::{Deserialize, Serialize};
79 use serde_json::json;
80
81 use crate::chrono::ts_float_microseconds;
82
83 #[test]
84 fn test_ts_float_microseconds() -> Result<(), Box<dyn Error>> {
85 #[derive(Deserialize, Serialize, Debug)]
86 struct S {
87 #[serde(with = "ts_float_microseconds")]
88 time: DateTime<Utc>,
89 }
90
91 let s: S = serde_json::from_str(r#"{ "time": 1609459200.999999 }"#)?;
93 assert_eq!(
94 s.time,
95 DateTime::<Utc>::from_utc(
96 NaiveDate::from_ymd_opt(2021, 1, 1)
97 .expect("")
98 .and_hms_micro_opt(0, 0, 0, 999999)
99 .expect(""),
100 Utc
101 )
102 );
103
104 let s: S = serde_json::from_str(r#"{ "time": 1609459200 }"#)?;
105 assert_eq!(
106 s.time,
107 DateTime::<Utc>::from_utc(
108 NaiveDate::from_ymd_opt(2021, 1, 1)
109 .expect("")
110 .and_hms_micro_opt(0, 0, 0, 0)
111 .expect(""),
112 Utc
113 )
114 );
115
116 let s: S = serde_json::from_str(r#"{ "time": 1609459200.000001 }"#)?;
117 assert_eq!(
118 s.time,
119 DateTime::<Utc>::from_utc(
120 NaiveDate::from_ymd_opt(2021, 1, 1)
121 .expect("")
122 .and_hms_micro_opt(0, 0, 0, 1)
123 .expect(""),
124 Utc
125 )
126 );
127
128 let s = S {
130 time: DateTime::from_utc(
131 NaiveDate::from_ymd_opt(2021, 1, 1)
132 .expect("")
133 .and_hms_micro_opt(0, 0, 0, 999999)
134 .expect(""),
135 Utc,
136 ),
137 };
138 assert_eq!(
139 serde_json::to_value(&s)?,
140 json!({ "time": 1609459200.999999 })
141 );
142
143 let s = S {
144 time: DateTime::from_utc(
145 NaiveDate::from_ymd_opt(2021, 1, 1)
146 .expect("")
147 .and_hms_micro_opt(0, 0, 0, 0)
148 .expect(""),
149 Utc,
150 ),
151 };
152 assert_eq!(
153 serde_json::to_value(&s)?,
154 json!({ "time": 1609459200.000000 })
155 );
156
157 let s = S {
158 time: DateTime::from_utc(
159 NaiveDate::from_ymd_opt(2021, 1, 1)
160 .expect("")
161 .and_hms_micro_opt(0, 0, 0, 1)
162 .expect(""),
163 Utc,
164 ),
165 };
166 assert_eq!(
167 serde_json::to_value(&s)?,
168 json!({ "time": 1609459200.000001 })
169 );
170
171 Ok(())
172 }
173}