From 00fb128805a8a5effa4212486ade9656717d39ed Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Wed, 30 Sep 2026 15:34:54 +0700 Subject: [PATCH] refactor(time): implement ClockSample from SystemTime --- src/time.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/time.rs b/src/time.rs index 7b6830b..65bd2a0 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, + } + } +}