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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use serde::de::{self, Visitor};
use std::fmt;
use time::OffsetDateTime;

#[derive(Debug, Clone)]
pub struct StrEpochMs {
    dt: OffsetDateTime,
}

impl From<OffsetDateTime> for StrEpochMs {
    fn from(dt: OffsetDateTime) -> Self {
        Self { dt }
    }
}

impl From<StrEpochMs> for OffsetDateTime {
    fn from(value: StrEpochMs) -> Self {
        value.dt
    }
}

struct StrEpochMsVisitor;

impl<'de> Visitor<'de> for StrEpochMsVisitor {
    type Value = StrEpochMs;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a string containing count of miliseconds since UNIX epoch")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        let unix_ts_ms: i128 = value.parse().map_err(|e| E::custom(format!("{e:?}")))?;

        let off_dt = OffsetDateTime::from_unix_timestamp_nanos(unix_ts_ms * 1000000)
            .map_err(|e| E::custom(format!("{e:?}")))?;

        Ok(off_dt.into())
    }
}

impl<'de> de::Deserialize<'de> for StrEpochMs {
    fn deserialize<D>(deserializer: D) -> Result<StrEpochMs, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        deserializer.deserialize_str(StrEpochMsVisitor)
    }
}

#[derive(Debug, Clone)]
pub struct StrEpochSec {
    dt: OffsetDateTime,
}

impl From<OffsetDateTime> for StrEpochSec {
    fn from(dt: OffsetDateTime) -> Self {
        Self { dt }
    }
}

impl From<StrEpochSec> for OffsetDateTime {
    fn from(value: StrEpochSec) -> Self {
        value.dt
    }
}

struct StrEpochSecVisitor;

impl<'de> Visitor<'de> for StrEpochSecVisitor {
    type Value = StrEpochSec;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a string containing count of seconds since UNIX epoch")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        let unix_ts: i64 = value.parse().map_err(|e| E::custom(format!("{e:?}")))?;

        let off_dt = OffsetDateTime::from_unix_timestamp(unix_ts)
            .map_err(|e| E::custom(format!("{e:?}")))?;

        Ok(off_dt.into())
    }
}

impl<'de> de::Deserialize<'de> for StrEpochSec {
    fn deserialize<D>(deserializer: D) -> Result<StrEpochSec, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        deserializer.deserialize_str(StrEpochSecVisitor)
    }
}

#[derive(Debug, Clone)]
pub struct I128EpochMs {
    dt: OffsetDateTime,
}

impl From<OffsetDateTime> for I128EpochMs {
    fn from(dt: OffsetDateTime) -> Self {
        Self { dt }
    }
}

impl From<I128EpochMs> for OffsetDateTime {
    fn from(value: I128EpochMs) -> Self {
        value.dt
    }
}

struct I128EpochMsVisitor;

impl<'de> Visitor<'de> for I128EpochMsVisitor {
    type Value = I128EpochMs;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("an integer containing count of miliseconds since UNIX epoch")
    }

    fn visit_i128<E>(self, value: i128) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        let off_dt = OffsetDateTime::from_unix_timestamp_nanos(value * 1000000)
            .map_err(|e| E::custom(format!("{e:?}")))?;

        Ok(off_dt.into())
    }
}

impl<'de> de::Deserialize<'de> for I128EpochMs {
    fn deserialize<D>(deserializer: D) -> Result<I128EpochMs, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        deserializer.deserialize_i128(I128EpochMsVisitor)
    }
}