use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone, Debug, Copy, PartialEq, PartialOrd, Eq)]
pub struct UnixEpochTs(pub u64);
impl UnixEpochTs {
#[must_use]
pub fn now() -> Self {
Self(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("UNIX_EPOCH")
.as_secs(),
)
}
#[must_use]
pub fn diff_str(time: Self, now: Self) -> String {
let diff = i64::try_from(now.0).expect("u64 from i64")
- i64::try_from(time.0).expect("u64 from i64");
if diff < 60 {
format!("{diff} seconds ago")
} else if diff < 60 * 60 {
format!("{} minutes ago", diff / 60)
} else if diff < 60 * 60 * 60 {
format!("{} hours ago", diff / 60 / 60)
} else {
format!("{} days ago", diff / 60 / 60 / 24)
}
}
}