#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Monotonic(pub(crate) Duration);
use std::time::Duration;
#[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 Monotonic {
#[inline]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
#[cfg(target_family = "unix")]
pub fn now() -> Self {
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))
}
#[inline]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
#[cfg(target_family = "unix")]
pub fn now_rounded() -> Self {
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() -> Self {
STARTED_AT.elapsed().into()
}
#[cfg(not(target_family = "unix"))]
#[inline]
pub fn now_rounded() -> Self {
Monotonic::from_secs(STARTED_AT.elapsed().as_secs())
}
#[inline]
pub fn elapsed(&self) -> Duration {
Self::now().0 - self.0
}
#[inline]
pub fn duration_since(&self, earlier: Self) -> Duration {
self.0 - earlier.0
}
}