Skip to main content

rbdc/types/
timestamp.rs

1use crate::{DateTime, Error};
2use rbs::Value;
3use serde::Deserializer;
4use std::fmt::{Debug, Display, Formatter};
5use std::str::FromStr;
6
7/// Timestamp(timestamp_millis:u64)
8#[derive(serde::Serialize, Clone, Eq, PartialEq, Hash)]
9#[serde(rename = "Timestamp")]
10pub struct Timestamp(pub i64);
11
12impl Timestamp {
13    #[deprecated(note = "please use utc()")]
14    pub fn now() -> Self {
15        Self(fastdate::DateTime::utc().unix_timestamp_millis())
16    }
17    /// utc time
18    pub fn utc() -> Self {
19        Self(fastdate::DateTime::utc().unix_timestamp_millis())
20    }
21}
22
23impl<'de> serde::Deserialize<'de> for Timestamp {
24    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
25    where
26        D: Deserializer<'de>,
27    {
28        use serde::de::Error;
29        match Value::deserialize(deserializer)?.as_i64() {
30            None => Err(Error::custom("warn type decode Json")),
31            Some(v) => Ok(Self(v)),
32        }
33    }
34}
35
36impl Display for Timestamp {
37    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
38        write!(f, "{}", self.0)
39    }
40}
41
42impl Debug for Timestamp {
43    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44        write!(f, "Timestamp({})", self.0)
45    }
46}
47
48impl From<Timestamp> for Value {
49    fn from(arg: Timestamp) -> Self {
50        Value::Ext("Timestamp", Box::new(Value::I64(arg.0)))
51    }
52}
53
54impl FromStr for Timestamp {
55    type Err = Error;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        Ok(Timestamp(i64::from_str(s)?))
59    }
60}
61
62impl From<Timestamp> for fastdate::DateTime {
63    fn from(value: Timestamp) -> Self {
64        fastdate::DateTime::from_timestamp_millis(value.0 as i64)
65    }
66}
67
68impl Default for Timestamp {
69    fn default() -> Self {
70        Timestamp(0)
71    }
72}
73
74impl From<DateTime> for Timestamp {
75    fn from(value: DateTime) -> Self {
76        Self(value.unix_timestamp_millis())
77    }
78}
79
80impl Into<DateTime> for Timestamp {
81    fn into(self) -> DateTime {
82        DateTime::from_timestamp_millis(self.0)
83    }
84}
85
86#[cfg(test)]
87mod test {
88    use crate::DateTime;
89    use crate::timestamp::Timestamp;
90    use rbs::Value;
91
92    #[test]
93    fn test_from_timestamp() {
94        let v = Timestamp::utc();
95        let dt: DateTime = v.into();
96        println!("{}", dt);
97    }
98
99    #[test]
100    fn test_ser_de() {
101        let dt = Timestamp::utc();
102        let v = serde_json::to_value(&dt).unwrap();
103        let new_dt: Timestamp = serde_json::from_value(v).unwrap();
104        assert_eq!(new_dt, dt);
105    }
106
107    #[test]
108    fn test_decode_timestamp_u64() {
109        assert_eq!(Timestamp(1), rbs::from_value(Value::U64(1)).unwrap());
110    }
111
112    #[test]
113    fn test_decode_timestamp_ext() {
114        assert_eq!(
115            Timestamp(1),
116            rbs::from_value(Value::Ext("Timestamp", Box::new(Value::U64(1)))).unwrap()
117        );
118    }
119}