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
// Copyright 2020 Bryant Luk
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Helpers for time.

/// A type which can return the duration since the epoch.
pub trait DurationSinceEpoch {
    /// Returns the number of seconds since the epoch.
    fn as_secs(&self) -> u64;

    /// Returns the number of milliseconds since the epoch.
    fn as_millis(&self) -> u64;
}

#[cfg(feature = "std")]
use std::convert::From;
#[cfg(feature = "std")]
use std::time::{Duration, SystemTime};

/// Duration since the epoch represented by standard library types.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg(feature = "std")]
pub struct StdDurationSinceEpoch(Duration);

#[cfg(feature = "std")]
impl StdDurationSinceEpoch {
    /// Returns the current duration since the epoch.
    pub fn now() -> Self {
        StdDurationSinceEpoch(
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .expect("SystemTime should be later than UNIX_EPOCH."),
        )
    }
}

#[cfg(feature = "std")]
impl DurationSinceEpoch for StdDurationSinceEpoch {
    fn as_secs(&self) -> u64 {
        self.0.as_secs()
    }

    fn as_millis(&self) -> u64 {
        self.0.as_secs() * 1000 + u64::from(self.0.subsec_millis())
    }
}