diff --git a/src/time.rs b/src/time.rs index d6da605..13f3b78 100644 --- a/src/time.rs +++ b/src/time.rs @@ -2,7 +2,10 @@ // // SPDX-License-Identifier: MPL-2.0 -use std::marker::PhantomData; +use std::{ + marker::PhantomData, + time::{SystemTime, UNIX_EPOCH}, +}; use rustix::time::{ClockId, clock_gettime}; @@ -64,3 +67,17 @@ impl ClockSample { (self.time_s as i64 % 86_400 * 1000) + self.time_ns as i64 / 1_000_000 } } + +impl From for ClockSample { + fn from(time: SystemTime) -> Self { + let duration = time + .duration_since(UNIX_EPOCH) + .expect("realtime timestamp predates Unix epoch"); + + Self { + time_s: duration.as_secs(), + time_ns: u64::from(duration.subsec_nanos()), + _clock: PhantomData, + } + } +}