aboutsummaryrefslogtreecommitdiff
path: root/apps/cassiopeia/src/time.rs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/cassiopeia/src/time.rs')
-rw-r--r--apps/cassiopeia/src/time.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/apps/cassiopeia/src/time.rs b/apps/cassiopeia/src/time.rs
index 56804713033b..4ac4ce7db900 100644
--- a/apps/cassiopeia/src/time.rs
+++ b/apps/cassiopeia/src/time.rs
@@ -1,14 +1,32 @@
+use crate::Date;
use chrono::{
- DateTime, FixedOffset as Offset, Local, NaiveDateTime, NaiveTime, TimeZone, Timelike, Utc,
+ DateTime, Duration, FixedOffset as Offset, Local, NaiveDateTime, NaiveTime, TimeZone, Timelike,
+ Utc,
};
+use std::{cmp::Ordering, ops::Sub};
/// A convenience wrapper around [DateTime][t] with fixed timezone
///
/// [t]: chrono::DateTime
+#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Time {
inner: DateTime<Offset>,
}
+impl From<DateTime<Offset>> for Time {
+ fn from(inner: DateTime<Offset>) -> Self {
+ Self { inner }
+ }
+}
+
+impl<'t> Sub for &'t Time {
+ type Output = Duration;
+
+ fn sub(self, o: &'t Time) -> Self::Output {
+ self.inner - o.inner
+ }
+}
+
impl Time {
/// Get the current local time and pin it to a fixed Tz offset
pub fn now() -> Self {
@@ -18,6 +36,15 @@ impl Time {
}
}
+ pub(crate) fn date(&self) -> chrono::Date<Offset> {
+ self.inner.date()
+ }
+
+ /// Check if a time stamp happened _after_ a date
+ pub fn after(&self, date: &Date) -> bool {
+ &Date::from(self.date()) > date
+ }
+
#[cfg(test)]
pub(crate) fn fixed(hour: u32, min: u32, sec: u32) -> Self {
Self {