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
use crate::Timestamp;

#[cfg(not(target_family = "unix"))]
use std::time::Instant;

#[cfg(not(target_family = "unix"))]
static STARTED_AT: once_cell::sync::Lazy<Instant> = once_cell::sync::Lazy::new(|| Instant::now());

impl Timestamp {
    /// On non-UNIX platforms returns time since the first access
    ///
    /// # Panics
    ///
    /// On UNIX platforms will panic if the system monotonic clock is not available
    #[inline]
    #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
    #[cfg(target_family = "unix")]
    pub fn now_monotonic() -> Self {
        use std::time::Duration;

        let t = nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC).unwrap();
        Self(Duration::new(t.tv_sec() as u64, t.tv_nsec() as u32))
    }
    /// # Panics
    ///
    /// On UNIX platforms will panic if the system monotonic clock is not available
    #[inline]
    #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
    #[cfg(target_family = "unix")]
    pub fn now_monotonic_rounded() -> Self {
        use std::time::Duration;

        let t = nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC).unwrap();
        Self(Duration::new(t.tv_sec() as u64, 0))
    }
    #[cfg(not(target_family = "unix"))]
    #[inline]
    pub fn now_monotonic() -> Self {
        STARTED_AT.elapsed().into()
    }
    #[cfg(not(target_family = "unix"))]
    #[inline]
    pub fn now_monotonic_rounded() -> Self {
        Timestamp::from_secs(STARTED_AT.elapsed().as_secs())
    }
}