use alloc::sync::Arc;
use core::fmt;
use core::time::Duration;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone)]
pub struct Clock(Arc<dyn Fn() -> SystemTime + Send + Sync>);
impl Clock {
pub fn system() -> Self {
Self(Arc::new(SystemTime::now))
}
pub fn from_fn(now: impl Fn() -> SystemTime + Send + Sync + 'static) -> Self {
Self(Arc::new(now))
}
pub fn now(&self) -> SystemTime {
(self.0)()
}
}
impl Default for Clock {
fn default() -> Self {
Self::system()
}
}
impl fmt::Debug for Clock {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("Clock")
}
}
pub(crate) fn unix_seconds(time: SystemTime) -> u64 {
time.duration_since(UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.as_secs())
}
pub(crate) fn from_unix_seconds(seconds: u64) -> SystemTime {
UNIX_EPOCH
.checked_add(Duration::from_secs(seconds))
.unwrap_or_else(|| UNIX_EPOCH + Duration::from_secs(u64::from(u32::MAX)))
}
pub(crate) fn later(time: SystemTime, by: Duration) -> SystemTime {
[
by,
Duration::from_secs(100 * 365 * 86_400),
Duration::from_secs(365 * 86_400),
]
.into_iter()
.filter(|step| *step <= by)
.find_map(|step| time.checked_add(step))
.unwrap_or(time)
}