use std::time::{SystemTime, UNIX_EPOCH}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Timestamp { secs: u64, // seconds since UNIX epoch } unsafe impl Send for Timestamp {} impl Timestamp { // Create a Timestamp from current system time pub fn now() -> Self { let dur = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("system time before UNIX_EPOCH"); Self { secs: dur.as_secs(), } } // Create from explicit fields (optional helper) pub fn from_secs(secs: u64) -> Self { Self { secs } } // Return underlying seconds pub fn as_secs(&self) -> u64 { self.secs } // Return elapsed seconds between `self` and `other`. // Panics if `other` is in the future relative to `self`. // If you call `Timestamp::now().diff(past)`, it returns seconds since `past`. pub fn diff(&self, earlier: &Timestamp) -> u64 { assert!(earlier.secs <= self.secs, "given time is in the future"); self.secs - earlier.secs } pub fn to_string(&self) -> String { let secs_of_day = self.secs % 86_400; let hh = secs_of_day / 3600; let mm = (secs_of_day % 3600) / 60; let ss = secs_of_day % 60; format!("{:02}:{:02}:{:02}", hh, mm, ss) } }