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
/*
    Appellation: time <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
//! # Time
//!
//! The `time` module provides a set of utilities for working with time and timestamps.
#[allow(unused_imports)]
#[doc(inline)]
pub use self::{epoch::Epoch, timestamp::Timestamp, utils::*};

#[doc(hidden)]
pub mod datetime;
pub mod epoch;
pub mod timestamp;

pub(crate) mod prelude {
    pub use super::epoch::Epoch;
    pub use super::timestamp::Timestamp;
    #[allow(unused_imports)]
    pub use super::utils::*;
    pub use super::Now;
}

///
pub trait Now {
    type Output;

    fn now() -> Self::Output;
}

pub(crate) mod utils {
    /// [systime] is a utilitarian function that returns the current system time in milliseconds.
    #[cfg(feature = "std")]
    #[inline]
    pub fn systime() -> core::time::Duration {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
    }
    /// [systime] is a utilitarian function that returns the current system time in milliseconds.
    #[cfg(feature = "std")]
    #[inline]
    pub fn std_time() -> u128 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis()
    }
}

#[allow(unused)]
#[cfg(test)]
mod tests {

    use super::*;
    use core::time::Duration;

    fn absdiff<A, B, C>(a: A, b: B) -> C
    where
        A: PartialOrd<B> + core::ops::Sub<B, Output = C>,
        B: core::ops::Sub<A, Output = C>,
    {
        if a > b {
            a - b
        } else {
            b - a
        }
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_timestamp() {
        let now = systime();
        let ts = Timestamp::<u128>::now();

        let tsd = Duration::from_millis(ts.0 as u64);
        let diff = absdiff(tsd, now).as_millis();
        assert!(diff < 1);
    }
}