#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct UnixNanos(u64);
impl UnixNanos {
pub const EPOCH: Self = Self(0);
#[must_use]
pub const fn from_nanos(nanos: u64) -> Self {
Self(nanos)
}
#[must_use]
pub const fn as_nanos(self) -> u64 {
self.0
}
#[must_use]
pub const fn as_millis(self) -> u64 {
self.0 / 1_000_000
}
}
#[cfg(test)]
mod tests {
use super::UnixNanos;
#[test]
fn as_millis_truncates_toward_zero() {
let reading = UnixNanos::from_nanos(1_753_500_000_999_999);
assert_eq!(reading.as_millis(), 1_753_500_000);
}
#[test]
fn epoch_round_trips_through_raw_nanos() {
assert_eq!(UnixNanos::EPOCH.as_nanos(), 0);
assert_eq!(UnixNanos::from_nanos(0), UnixNanos::EPOCH);
}
}