#[cfg(feature = "std")]
use core::time::Duration;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WallTime(u64);
impl WallTime {
pub const UNIX_EPOCH: Self = Self(0);
#[must_use]
pub const fn from_unix_nanos(ns: u64) -> Self {
Self(ns)
}
#[must_use]
pub const fn unix_nanos(&self) -> u64 {
self.0
}
#[cfg(feature = "std")]
#[must_use]
pub fn system(&self) -> std::time::SystemTime {
std::time::SystemTime::UNIX_EPOCH
.checked_add(Duration::from_nanos(self.0))
.unwrap_or(std::time::SystemTime::UNIX_EPOCH)
}
#[cfg(feature = "std")]
#[must_use]
pub fn from_system_now() -> Self {
Self(now_wall_nanos())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Timestamp {
pub wall: WallTime,
pub mono: u64,
}
impl Timestamp {
#[must_use]
pub const fn from_parts(wall: WallTime, mono: u64) -> Self {
Self { wall, mono }
}
#[must_use]
pub fn mono_from_wall_lossy(&self) -> u64 {
self.wall.unix_nanos()
}
}
#[cfg(feature = "std")]
fn now_wall_nanos() -> u64 {
use std::time::{SystemTime, UNIX_EPOCH};
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(d) => u64::try_from(d.as_nanos()).unwrap_or(u64::MAX),
Err(_) => 0,
}
}