diff options
author | Mx Kookie <kookie@spacekookie.de> | 2020-12-13 12:08:55 +0000 |
---|---|---|
committer | Mx Kookie <kookie@spacekookie.de> | 2020-12-21 05:19:49 +0100 |
commit | 236cf191b90a428325c8c179d595d4b1cd36f776 (patch) | |
tree | 5d6c67cb0b7ef980aad47ee35e264b4dfdb5422d /apps/cassiopeia/src/time.rs | |
parent | 4c97f3208a0ba185264a169e01d0b0d922266ea6 (diff) |
cassiopeia: changing parser output to more generic IR structure
This allows a few things: a, it's a persistant format that we can
mirror to disk again, AND can adapt because all type information is
known, and it allows for new entries to be added to the IR more
easily, without having to worry about exact formatting, or
re-inferring order from the TimeFile abstraction.
Diffstat (limited to '')
-rw-r--r-- | apps/cassiopeia/src/time.rs | 29 |
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 { |