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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#![cfg_attr(not(feature = "with_protocol"), allow(unused))]
use chrono::{DateTime, TimeZone, Utc};
pub fn datetime_to_timestamp(dt: &DateTime<Utc>) -> f64 {
if dt.timestamp_subsec_nanos() == 0 {
dt.timestamp() as f64
} else {
(dt.timestamp() as f64) + ((dt.timestamp_subsec_micros() as f64) / 1_000_000f64)
}
}
pub fn timestamp_to_datetime(ts: f64) -> DateTime<Utc> {
let secs = ts as i64;
let micros = (ts.fract() * 1_000_000f64) as u32;
Utc.timestamp_opt(secs, micros * 1000).unwrap()
}
pub mod ts_seconds_float {
use chrono::{DateTime, TimeZone, Utc};
use serde::{de, ser};
use std::fmt;
use super::timestamp_to_datetime;
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_any(SecondsTimestampVisitor)
.map(|dt| dt.with_timezone(&Utc))?)
}
pub fn serialize<S>(dt: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
if dt.timestamp_subsec_nanos() == 0 {
serializer.serialize_i64(dt.timestamp())
} else {
serializer.serialize_f64(
(dt.timestamp() as f64) + ((dt.timestamp_subsec_micros() as f64) / 1_000_000f64),
)
}
}
struct SecondsTimestampVisitor;
impl<'de> de::Visitor<'de> for SecondsTimestampVisitor {
type Value = DateTime<Utc>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a unix timestamp")
}
fn visit_f64<E>(self, value: f64) -> Result<DateTime<Utc>, E>
where
E: de::Error,
{
Ok(timestamp_to_datetime(value))
}
fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>
where
E: de::Error,
{
Ok(Utc.timestamp_opt(value, 0).unwrap())
}
fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E>
where
E: de::Error,
{
Ok(Utc.timestamp_opt(value as i64, 0).unwrap())
}
fn visit_str<E>(self, value: &str) -> Result<DateTime<Utc>, E>
where
E: de::Error,
{
value.parse().map_err(|e| E::custom(format!("{}", e)))
}
}
}