compact_time/
lib.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! 64-bit time value with nanosecond precision.
//!
//! Time values range from 1970-01-01T00:00:00.000000000Z to 2554-07-21T23:34:33.709551615Z.
mod source;

use std::{fmt, time::{SystemTime, UNIX_EPOCH, Duration}, ops::{Add, Sub}};

pub use source::TimeSource;

const NANO: u64 = 1_000_000_000;

/// A time value with nanosecond precision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode), cbor(transparent))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
pub struct Time(#[cfg_attr(feature = "minicbor", n(0))] u64);

impl Time {
    /// The minimum time value (1970-01-01T00:00:00.000000000Z).
    pub const MIN: Self = Time(0);

    /// The maximum time value (2554-07-21T23:34:33.709551615Z).
    pub const MAX: Self = Time(u64::MAX);

    /// Get the current system time.
    ///
    /// # Panics
    ///
    /// If the reported system time is less than [`Time::MIN`] or greater than [`Time::MAX`].
    pub fn now() -> Self {
        Self::try_now().expect("SystemTime in range")
    }

    /// Get the current system time.
    ///
    /// Fails if the reported system time is less than [`Time::MIN`] or greater than [`Time::MAX`].
    pub fn try_now() -> Result<Self, OutOfRange> {
        let t = SystemTime::now();
        let Ok(d) = t.duration_since(UNIX_EPOCH) else {
            return Err(OutOfRange(()))
        };
        Self::try_from_sn(d.as_secs(), d.subsec_nanos())
    }

    /// Get the current system time using `CLOCK_REALTIME_COARSE`.
    ///
    /// This is faster but less precise than [`Time::now`].
    ///
    /// # Panics
    ///
    /// If the reported system time is less than [`Time::MIN`] or greater than [`Time::MAX`].
    #[cfg(feature = "coarse")]
    pub fn coarse() -> Self {
        Self::try_coarse().expect("valid time")
    }

    /// Get the current system time using `CLOCK_REALTIME_COARSE`.
    ///
    /// This is faster but less precise than [`Time::now`].
    ///
    /// Fails if the reported system time is less than [`Time::MIN`] or greater than [`Time::MAX`].
    #[cfg(feature = "coarse")]
    pub fn try_coarse() -> Result<Self, OutOfRange> {
        use rustix::time::{clock_gettime, ClockId};
        let t = clock_gettime(ClockId::RealtimeCoarse);
        if t.tv_sec < 0 || t.tv_nsec < 0 {
            return Err(OutOfRange(()))
        }
        Self::try_from_sn(t.tv_sec as u64, t.tv_nsec as u64)
    }

    /// Construct a time value from the given seconds.
    ///
    /// # Panics
    ///
    /// If the given seconds are greater than [`Time::MAX`] seconds.
    pub fn from_seconds(s: u64) -> Self {
        Self::try_from_seconds(s).expect("seconds in range")
    }

    /// Construct a time value from the given seconds.
    ///
    /// Fails if the given seconds are greater than [`Time::MAX`] seconds.
    pub fn try_from_seconds(s: u64) -> Result<Self, OutOfRange> {
        Self::try_from_sn(s, 0u64)
    }

    /// Construct a time value from the given nanoseconds.
    pub const fn from_nanos(n: u64) -> Self {
        Self(n)
    }

    /// Seconds since the Unix epoch.
    pub const fn seconds(self) -> u64 {
        self.0 / NANO
    }

    /// Nanoseconds since the Unix epoch.
    pub const fn nanos(self) -> u64 {
        self.0
    }

    /// Subsecond nanoseconds.
    pub const fn second_nanos(self) -> u64 {
        self.nanos() - self.seconds() * NANO
    }

    /// Try to add the given time value.
    ///
    /// Fails if the result would be greater than [`Time::MAX`].
    pub fn try_add(self, t: Time) -> Result<Self, OutOfRange> {
        let n = self.0.checked_add(t.0).ok_or(OutOfRange(()))?;
        Ok(Self(n))
    }

    /// Try to substract the given time value.
    ///
    /// Fails if the result would be less than [`Time::MIN`].
    pub fn try_sub(self, t: Time) -> Result<Self, OutOfRange> {
        let n = self.0.checked_sub(t.0).ok_or(OutOfRange(()))?;
        Ok(Self(n))
    }

    /// Add the given number of nanoseconds.
    ///
    /// # Panics
    ///
    /// If the result would be greater than [`Time::MAX`]. See
    /// [`Time::add_nanos_checked`] for an alternative that does
    /// not panic.
    pub fn add_nanos(self, n: u64) -> Self {
        self.add_nanos_checked(n).expect("no overflow")
    }

    /// Add the given number of seconds.
    ///
    /// # Panics
    ///
    /// If the result would be greater than [`Time::MAX`]. See
    /// [`Time::add_seconds_checked`] for an alternative that does
    /// not panic.
    pub fn add_seconds(self, s: u64) -> Self {
        self.add_seconds_checked(s).expect("no overflow")
    }

    /// Add the given number of minutes.
    ///
    /// # Panics
    ///
    /// If the result would be greater than [`Time::MAX`]. See
    /// [`Time::add_minutes_checked`] for an alternative that does
    /// not panic.
    pub fn add_minutes(self, m: u64) -> Self {
        self.add_minutes_checked(m).expect("no overflow")
    }

    /// Add the given number of hours.
    ///
    /// # Panics
    ///
    /// If the result would be greater than [`Time::MAX`]. See
    /// [`Time::add_hours_checked`] for an alternative that does
    /// not panic.
    pub fn add_hours(self, h: u64) -> Self {
        self.add_hours_checked(h).expect("no overflow")
    }

    /// Add the given number of days.
    ///
    /// # Panics
    ///
    /// If the result would be greater than [`Time::MAX`]. See
    /// [`Time::add_days_checked`] for an alternative that does
    /// not panic.
    pub fn add_days(self, d: u64) -> Self {
        self.add_days_checked(d).expect("no overflow")
    }

    /// Add the given number of nanoseconds.
    ///
    /// Fails if the result would be greater than [`Time::MAX`].
    pub fn add_nanos_checked(self, n: u64) -> Result<Self, OutOfRange> {
        let n = self.0.checked_add(n).ok_or(OutOfRange::new())?;
        Ok(Self(n))
    }

    /// Add the given number of seconds.
    ///
    /// Fails if the result would be greater than [`Time::MAX`].
    pub fn add_seconds_checked(self, s: u64) -> Result<Self, OutOfRange> {
        self.add_nanos_checked(s.checked_mul(NANO).ok_or(OutOfRange::new())?)
    }

    /// Add the given number of minutes.
    ///
    /// Fails if the result would be greater than [`Time::MAX`].
    pub fn add_minutes_checked(self, m: u64) -> Result<Self, OutOfRange> {
        self.add_seconds_checked(m.checked_mul(60).ok_or(OutOfRange::new())?)
    }

    /// Add the given number of hours.
    ///
    /// Fails if the result would be greater than [`Time::MAX`].
    pub fn add_hours_checked(self, h: u64) -> Result<Self, OutOfRange> {
        self.add_seconds_checked(h.checked_mul(3600).ok_or(OutOfRange::new())?)
    }

    /// Add the given number of days.
    ///
    /// Fails if the result would be greater than [`Time::MAX`].
    pub fn add_days_checked(self, d: u64) -> Result<Self, OutOfRange> {
        self.add_seconds_checked(d.checked_mul(24 * 3600).ok_or(OutOfRange::new())?)
    }

    /// Substract the given number of nanoseconds.
    ///
    /// # Panics
    ///
    /// If the result would be less than [`Time::MIN`]. See
    /// [`Time::sub_nanos_checked`] for an alternative that does
    /// not panic.
    pub fn sub_nanos(self, n: u64) -> Self {
        self.sub_nanos_checked(n).expect("no underflow")
    }

    /// Substract the given number of seconds.
    ///
    /// # Panics
    ///
    /// If the result would be less than [`Time::MIN`]. See
    /// [`Time::sub_seconds_checked`] for an alternative that does
    /// not panic.
    pub fn sub_seconds(self, s: u64) -> Self {
        self.sub_seconds_checked(s).expect("no underflow")
    }

    /// Substract the given number of minutes.
    ///
    /// # Panics
    ///
    /// If the result would be less than [`Time::MIN`]. See
    /// [`Time::sub_minutes_checked`] for an alternative that does
    /// not panic.
    pub fn sub_minutes(self, m: u64) -> Self {
        self.sub_minutes_checked(m).expect("no underflow")
    }

    /// Substract the given number of hours.
    ///
    /// # Panics
    ///
    /// If the result would be less than [`Time::MIN`]. See
    /// [`Time::sub_hours_checked`] for an alternative that does
    /// not panic.
    pub fn sub_hours(self, h: u64) -> Self {
        self.sub_hours_checked(h).expect("no underflow")
    }

    /// Substract the given number of days.
    ///
    /// # Panics
    ///
    /// If the result would be less than [`Time::MIN`]. See
    /// [`Time::sub_days_checked`] for an alternative that does
    /// not panic.
    pub fn sub_days(self, d: u64) -> Self {
        self.sub_days_checked(d).expect("no underflow")
    }

    /// Substract the given number of nanoseconds.
    ///
    /// Fails if the result would be less than [`Time::MIN`].
    pub fn sub_nanos_checked(self, n: u64) -> Result<Self, OutOfRange> {
        let n = self.0.checked_sub(n).ok_or(OutOfRange::new())?;
        Ok(Self(n))
    }

    /// Substract the given number of seconds.
    ///
    /// Fails if the result would be less than [`Time::MIN`].
    pub fn sub_seconds_checked(self, s: u64) -> Result<Self, OutOfRange> {
        self.sub_nanos_checked(s.checked_mul(NANO).ok_or(OutOfRange::new())?)
    }

    /// Substract the given number of minutes.
    ///
    /// Fails if the result would be less than [`Time::MIN`].
    pub fn sub_minutes_checked(self, m: u64) -> Result<Self, OutOfRange> {
        self.sub_seconds_checked(m.checked_mul(60).ok_or(OutOfRange::new())?)
    }

    /// Substract the given number of hours.
    ///
    /// Fails if the result would be less than [`Time::MIN`].
    pub fn sub_hours_checked(self, h: u64) -> Result<Self, OutOfRange> {
        self.sub_seconds_checked(h.checked_mul(3600).ok_or(OutOfRange::new())?)
    }

    /// Substract the given number of days.
    ///
    /// Fails if the result would be less than [`Time::MIN`].
    pub fn sub_days_checked(self, d: u64) -> Result<Self, OutOfRange> {
        self.sub_seconds_checked(d.checked_mul(24 * 3600).ok_or(OutOfRange::new())?)
    }

    /// Convert this time value to the [`std::time::Duration`] since the Unix epoch.
    pub const fn to_duration(self) -> Duration {
        let s = self.seconds();
        let n = self.second_nanos();
        Duration::new(s, n as u32)
    }

    /// Convert the delta between this time and the given one into a [`std::time::Duration`].
    ///
    /// Returns `None` if the argument is greater that `self`.
    pub fn duration_since(self, earlier: Self) -> Option<Duration> {
        self.to_duration().checked_sub(earlier.to_duration())
    }

    /// Return this time value as a plain integer.
    pub fn as_u64(self) -> u64 {
        self.0
    }

    /// Format this time value as `YYYY-MM-DDThh:mm:ss.nnnnnnnnnZ`.
    ///
    /// *Requires feature `"utc"`*.
    #[cfg(feature = "utc")]
    pub fn to_utc_string(self) -> Option<String> {
        let s = self.seconds();
        let n = self.second_nanos();
        if let Ok(utc) = tz::UtcDateTime::from_timespec(s as i64, n as u32) {
            Some(utc.to_string())
        } else {
            None
        }
    }

    /// Try to construct a time value from seconds and nanoseconds.
    fn try_from_sn<S: Into<u64>, N: Into<u64>>(s: S, n: N) -> Result<Self, OutOfRange> {
        let n = s.into()
            .checked_mul(NANO)
            .and_then(|x| x.checked_add(n.into()))
            .ok_or(OutOfRange::new())?;
        Ok(Self(n))
    }
}

impl From<Time> for u64 {
    fn from(value: Time) -> Self {
        value.0
    }
}

impl From<u64> for Time {
    fn from(n: u64) -> Self {
        Self(n)
    }
}

/// Error if values go out of range.
#[derive(Debug)]
pub struct OutOfRange(());

impl OutOfRange {
    fn new() -> Self {
        Self(())
    }
}

impl fmt::Display for OutOfRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("out of range")
    }
}

impl std::error::Error for OutOfRange {}

impl Add<Duration> for Time {
    type Output = Self;

    /// Adds the given duration.
    ///
    /// # Panics
    ///
    /// If the result would be greater than [`Time::MAX`].
    fn add(self, d: Duration) -> Self::Output {
        self.add_seconds(d.as_secs())
            .add_nanos(d.subsec_nanos().into())
    }
}

impl Add<Time> for Time {
    type Output = Self;

    /// Adds the given time.
    ///
    /// # Panics
    ///
    /// If the result would be greater than [`Time::MAX`].
    fn add(self, t: Time) -> Self::Output {
        self.add_nanos(t.nanos())
    }
}

impl Sub<Duration> for Time {
    type Output = Self;

    /// Subtracts the given duration.
    ///
    /// # Panics
    ///
    /// If the result would be less than [`Time::MIN`].
    fn sub(self, d: Duration) -> Self::Output {
        self.sub_seconds(d.as_secs())
            .sub_nanos(d.subsec_nanos().into())
    }
}

impl Sub<Time> for Time {
    type Output = Self;

    /// Subtracts the given time.
    ///
    /// # Panics
    ///
    /// If the result would be less than [`Time::MIN`].
    fn sub(self, t: Time) -> Self::Output {
        self.sub_nanos(t.nanos())
    }
}

impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = self.seconds();
        let n = self.second_nanos();
        write!(f, "{s}.{n}")
    }
}

#[cfg(test)]
mod tests {
    use quickcheck::{Arbitrary, Gen, quickcheck, TestResult};
    use super::{NANO, Time};

    impl Arbitrary for Time {
        fn arbitrary(g: &mut Gen) -> Self {
            Time::from(u64::arbitrary(g))
        }
    }

    quickcheck! {
        fn prop_sec_nano_id(s: u64, n: u64) -> TestResult {
            let n = n % NANO; // subsec nanos
            if s.checked_mul(NANO).and_then(|s| s.checked_add(n)).is_none() {
                return TestResult::discard()
            }
            let t = Time::try_from_sn(s, n).unwrap();
            let b = t.seconds() == s && t.second_nanos() == n;
            TestResult::from_bool(b)
        }

        fn prop_add_sub_nanos_id(t: Time, n: u64) -> TestResult {
            let Ok(u) = t.add_nanos_checked(n) else {
                return TestResult::discard()
            };
            TestResult::from_bool(u.sub_nanos(n) == t)
        }

        fn prop_add_sub_secs_id(t: Time, s: u64) -> TestResult {
            let Ok(u) = t.add_seconds_checked(s) else {
                return TestResult::discard()
            };
            TestResult::from_bool(u.sub_seconds(s) == t)
        }

        fn prop_add_sub_mins_id(t: Time, m: u64) -> TestResult {
            let Ok(u) = t.add_minutes_checked(m) else {
                return TestResult::discard()
            };
            TestResult::from_bool(u.sub_minutes(m) == t)
        }

        fn prop_add_sub_hours_id(t: Time, h: u64) -> TestResult {
            let Ok(u) = t.add_hours_checked(h) else {
                return TestResult::discard()
            };
            TestResult::from_bool(u.sub_hours(h) == t)
        }

        fn prop_add_sub_days_id(t: Time, d: u64) -> TestResult {
            let Ok(u) = t.add_days_checked(d) else {
                return TestResult::discard()
            };
            TestResult::from_bool(u.sub_days(d) == t)
        }

        fn prop_second_nanos(t: Time) -> bool {
            t.nanos() - t.seconds() * NANO == t.second_nanos()
        }

        fn prop_add_1s_as_nanos(t: Time) -> TestResult {
            if Time::MAX.try_sub(t).map(|d| d < NANO.into()).unwrap_or(true) {
                return TestResult::discard()
            }
            let u = t.add_nanos(NANO);
            let b = u.seconds() - t.seconds() == 1 && u.second_nanos() - t.second_nanos() == 0;
            TestResult::from_bool(b)
        }
    }

    #[test]
    fn max_time() {
        assert!(Time::MAX.add_nanos_checked(1).is_err());
        assert!(Time::MAX.add_seconds_checked(1).is_err());
        assert!(Time::MAX.add_minutes_checked(1).is_err());
        assert!(Time::MAX.add_hours_checked(1).is_err());
        assert!(Time::MAX.add_days_checked(1).is_err());
    }

    #[test]
    fn min_time() {
        assert!(Time::MIN.sub_nanos_checked(1).is_err());
        assert!(Time::MIN.sub_seconds_checked(1).is_err());
        assert!(Time::MIN.sub_minutes_checked(1).is_err());
        assert!(Time::MIN.sub_hours_checked(1).is_err());
        assert!(Time::MIN.sub_days_checked(1).is_err());
    }
}