use serde::{Serialize, Serializer}; use std::{ fmt::{Display, Formatter, Result as FmtResult}, ops::Sub, time::{SystemTime, UNIX_EPOCH}, }; #[derive(PartialEq, PartialOrd, Copy, Clone, Debug)] pub struct EpochTimestamp(SystemTime); impl EpochTimestamp { pub fn now() -> Self { EpochTimestamp(SystemTime::now()) } } impl Sub for EpochTimestamp { type Output = u64; fn sub(self, other: EpochTimestamp) -> u64 { self.0.duration_since(other.0).unwrap_or_default().as_secs() } } impl Display for EpochTimestamp { fn fmt(&self, f: &mut Formatter) -> FmtResult { write!( f, "{}", self.0 .duration_since(UNIX_EPOCH) .unwrap_or_default() .as_secs() ) } } impl Serialize for EpochTimestamp { fn serialize(&self, s: S) -> Result where S: Serializer, { s.serialize_u64( self.0 .duration_since(SystemTime::from(UNIX_EPOCH)) .unwrap_or_default() .as_secs(), ) } }