use chrono::{DateTime, Duration, Utc};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Window<M> {
pub start: DateTime<Utc>,
pub end: DateTime<Utc>,
pub meta: M,
}
impl<M> Window<M> {
pub fn new(start: DateTime<Utc>, end: DateTime<Utc>, meta: M) -> Option<Self> {
(start < end).then_some(Self { start, end, meta })
}
#[inline]
pub fn is_upcoming(&self, now: DateTime<Utc>) -> bool {
now < self.start
}
#[inline]
pub fn is_active(&self, now: DateTime<Utc>) -> bool {
self.start <= now && now < self.end
}
#[inline]
pub fn is_expired(&self, now: DateTime<Utc>) -> bool {
now >= self.end
}
#[inline]
pub fn duration(&self) -> Duration {
self.end - self.start
}
pub fn elapsed_at(&self, now: DateTime<Utc>) -> Option<Duration> {
if self.is_active(now) {
Some(now - self.start)
} else {
None
}
}
pub fn remaining_at(&self, now: DateTime<Utc>) -> Option<Duration> {
if self.is_active(now) {
Some(self.end - now)
} else {
None
}
}
}