use serde::{Deserialize, Serialize}; use std::time::SystemTime; #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)] #[serde(transparent)] pub struct Timestamp(u32); impl Timestamp { // This will work for the next 20 years #[allow(clippy::cast_possible_truncation)] pub fn now() -> Self { Timestamp( SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .expect("SystemTime::duration_since failed") .as_secs() as u32, ) } pub fn plus(self, seconds: u32) -> Self { Self(self.0.checked_add(seconds).unwrap_or(std::u32::MAX)) } } impl From for Timestamp { fn from(item: u32) -> Self { Self(item) } } impl From for u32 { fn from(item: Timestamp) -> Self { item.0 } } impl From for u64 { fn from(item: Timestamp) -> Self { u64::from(item.0) } } impl From for i64 { fn from(item: Timestamp) -> Self { i64::from(item.0) } }