relay_common/
time.rs

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! Utilities to deal with date-time types. (DateTime, Instant, SystemTime, etc)

use std::fmt;
use std::time::{Duration, Instant, SystemTime};

use chrono::{DateTime, TimeZone, Utc};
use serde::{Deserialize, Serialize};

/// Converts an `Instant` into a `SystemTime`.
pub fn instant_to_system_time(instant: Instant) -> SystemTime {
    SystemTime::now() - instant.elapsed()
}

/// Returns the number of milliseconds contained by this `Duration` as `f64`.
///
/// The returned value does include the fractional (nanosecond) part of the duration.
///
/// # Example
///
/// ```
/// use std::time::Duration;
///
/// let duration = Duration::from_nanos(2_125_000);
/// let millis = relay_common::time::duration_to_millis(duration);
/// assert_eq!(millis, 2.125);
/// ```
pub fn duration_to_millis(duration: Duration) -> f64 {
    (duration.as_secs_f64() * 1_000_000_000f64).round() / 1_000_000f64
}

/// Returns the positive number of milliseconds contained by this `Duration` as `f64`.
///
/// The returned value does include the fractional (nanosecond) part of the duration. If the
/// duration is negative, this returns `0.0`;
///
/// # Example
///
/// ```
/// use chrono::Duration;
///
/// let duration = Duration::nanoseconds(2_125_000);
/// let millis = relay_common::time::chrono_to_positive_millis(duration);
/// assert_eq!(millis, 2.125);
/// ```
///
/// Negative durations are clamped to `0.0`:
///
/// ```
/// use chrono::Duration;
///
/// let duration = Duration::nanoseconds(-2_125_000);
/// let millis = relay_common::time::chrono_to_positive_millis(duration);
/// assert_eq!(millis, 0.0);
/// ```
pub fn chrono_to_positive_millis(duration: chrono::Duration) -> f64 {
    duration_to_millis(duration.to_std().unwrap_or_default())
}

/// A unix timestamp (full seconds elapsed since 1970-01-01 00:00 UTC).
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UnixTimestamp(u64);

impl UnixTimestamp {
    /// Creates a unix timestamp from the given number of seconds.
    pub const fn from_secs(secs: u64) -> Self {
        Self(secs)
    }

    /// Creates a unix timestamp from the given system time.
    pub fn from_system(time: SystemTime) -> Self {
        let duration = time
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self(duration)
    }

    /// Creates a unix timestamp from the given chrono `DateTime`.
    ///
    /// Returns `Some` if this is a valid date time starting with 1970-01-01 00:00 UTC. If the date
    /// lies before the UNIX epoch, this function returns `None`.
    pub fn from_datetime(date_time: DateTime<impl TimeZone>) -> Option<Self> {
        let timestamp = date_time.timestamp();
        if timestamp >= 0 {
            Some(UnixTimestamp::from_secs(timestamp as u64))
        } else {
            None
        }
    }

    /// Converts the given `Instant` into a UNIX timestamp.
    ///
    /// This is done by comparing the `Instant` with the current system time. Note that the system
    /// time is subject to skew, so subsequent calls to `from_instant` may return different values.
    #[inline]
    pub fn from_instant(instant: Instant) -> Self {
        Self::from_system(instant_to_system_time(instant))
    }

    /// Returns the current timestamp.
    #[inline]
    pub fn now() -> Self {
        Self::from_system(SystemTime::now())
    }

    /// Returns the number of seconds since the UNIX epoch start.
    pub fn as_secs(self) -> u64 {
        self.0
    }

    /// Returns the timestamp as chrono datetime.
    pub fn as_datetime(self) -> Option<DateTime<Utc>> {
        DateTime::from_timestamp(self.0 as i64, 0)
    }
}

impl fmt::Debug for UnixTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "UnixTimestamp({})", self.as_secs())
    }
}

impl fmt::Display for UnixTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_secs().fmt(f)
    }
}

/// Adds _whole_ seconds of the given duration to the timestamp.
impl std::ops::Add<Duration> for UnixTimestamp {
    type Output = Self;

    fn add(self, rhs: Duration) -> Self::Output {
        Self(self.0.saturating_add(rhs.as_secs()))
    }
}

impl std::ops::Sub for UnixTimestamp {
    type Output = Duration;

    fn sub(self, rhs: Self) -> Self::Output {
        Duration::from_secs(self.0 - rhs.0)
    }
}

#[derive(Debug)]
/// An error returned from parsing [`UnixTimestamp`].
pub struct ParseUnixTimestampError(());

impl std::str::FromStr for UnixTimestamp {
    type Err = ParseUnixTimestampError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(datetime) = s.parse::<DateTime<Utc>>() {
            let timestamp = datetime.timestamp();
            if timestamp >= 0 {
                return Ok(UnixTimestamp(timestamp as u64));
            }
        }
        let ts = s.parse().or(Err(ParseUnixTimestampError(())))?;
        Ok(Self(ts))
    }
}

impl Serialize for UnixTimestamp {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u64(self.as_secs())
    }
}

struct UnixTimestampVisitor;

impl serde::de::Visitor<'_> for UnixTimestampVisitor {
    type Value = UnixTimestamp;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a non-negative timestamp or datetime string")
    }

    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(UnixTimestamp::from_secs(v))
    }

    fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        if v < 0.0 || v > u64::MAX as f64 {
            return Err(E::custom("timestamp out-of-range"));
        }

        Ok(UnixTimestamp::from_secs(v.trunc() as u64))
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        let datetime = v.parse::<DateTime<Utc>>().map_err(E::custom)?;
        let timestamp = datetime.timestamp();

        if timestamp >= 0 {
            Ok(UnixTimestamp(timestamp as u64))
        } else {
            Err(E::custom("timestamp out-of-range"))
        }
    }
}

impl<'de> Deserialize<'de> for UnixTimestamp {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(UnixTimestampVisitor)
    }
}

#[cfg(test)]
mod tests {
    use serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};

    use super::*;

    #[test]
    fn test_parse_timestamp_int() {
        assert_tokens(&UnixTimestamp::from_secs(123), &[Token::U64(123)]);
    }

    #[test]
    fn test_parse_timestamp_neg_int() {
        assert_de_tokens_error::<UnixTimestamp>(
            &[Token::I64(-1)],
            "invalid type: integer `-1`, expected a non-negative timestamp or datetime string",
        );
    }

    #[test]
    fn test_parse_timestamp_float() {
        assert_de_tokens(&UnixTimestamp::from_secs(123), &[Token::F64(123.4)]);
    }

    #[test]
    fn test_parse_timestamp_large_float() {
        assert_de_tokens_error::<UnixTimestamp>(
            &[Token::F64(2.0 * (u64::MAX as f64))],
            "timestamp out-of-range",
        );
    }

    #[test]
    fn test_parse_timestamp_neg_float() {
        assert_de_tokens_error::<UnixTimestamp>(&[Token::F64(-1.0)], "timestamp out-of-range");
    }

    #[test]
    fn test_parse_timestamp_str() {
        assert_de_tokens(
            &UnixTimestamp::from_secs(123),
            &[Token::Str("1970-01-01T00:02:03Z")],
        );
    }

    #[test]
    fn test_parse_timestamp_other() {
        assert_de_tokens_error::<UnixTimestamp>(
            &[Token::Bool(true)],
            "invalid type: boolean `true`, expected a non-negative timestamp or datetime string",
        );
    }

    #[test]
    fn test_parse_datetime_bogus() {
        assert_de_tokens_error::<UnixTimestamp>(
            &[Token::Str("adf3rt546")],
            "input contains invalid characters",
        );
    }
}