use std::time::{Duration, SystemTime, SystemTimeError, UNIX_EPOCH};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd)]
pub struct NotBotTime(pub SystemTime);
pub const NOTBOT_EPOCH: NotBotTime = NotBotTime(UNIX_EPOCH);
impl NotBotTime {
#[must_use]
pub fn now() -> Self {
Self(SystemTime::now())
}
pub fn duration_since(&self, earlier: Self) -> Result<Duration, SystemTimeError> {
self.0.duration_since(earlier.0)
}
}
impl From<Vec<u8>> for NotBotTime {
fn from(v: Vec<u8>) -> Self {
let boxed_v: Box<[u8; 8]> = v.try_into().unwrap_or_else(|_| Box::new([0u8; 8]));
let d_secs: u64 = u64::from_le_bytes(*boxed_v);
let d: Duration = Duration::from_secs(d_secs);
let st: Self = NOTBOT_EPOCH.0.checked_add(d).map_or_else(Self::now, Self);
st
}
}
impl From<NotBotTime> for Vec<u8> {
fn from(s: NotBotTime) -> Self {
let d: Duration = s
.duration_since(NOTBOT_EPOCH)
.unwrap_or_else(|_| Duration::from_secs(0));
d.as_secs().to_le_bytes().to_vec()
}
}