diff options
author | Ruben Pollan | 2018-08-28 16:16:17 +0200 |
---|---|---|
committer | Ruben Pollan | 2018-08-28 16:16:17 +0200 |
commit | 5eb5bb17c0f7e93aacc340ce9a28ee352cc91c59 (patch) | |
tree | 7c20e616108e5b5927f28781c76fb71df95b9785 | |
parent | 3aaab82ab066c71486bcf323d08aafe6794cf051 (diff) |
Add +, now and is_empty to Date
Diffstat (limited to '')
-rw-r--r-- | src/date.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/date.rs b/src/date.rs index dced057..b703766 100644 --- a/src/date.rs +++ b/src/date.rs @@ -1,9 +1,11 @@ use std::cmp::{Ordering, Ord, PartialEq, PartialOrd}; +use std::ops::Add; use errors::EventError; use chrono; -use chrono::TimeZone; +use chrono::{TimeZone, Duration}; +use chrono::offset::Utc; use chrono_tz::{Tz, UTC}; @@ -19,6 +21,15 @@ impl Date { Date::Time(UTC.timestamp(0, 0)) } + + pub fn now() -> Date { + Date::Time(UTC.from_utc_datetime(&Utc::now().naive_utc())) + } + + pub fn is_empty(&self) -> bool { + *self == Date::empty() + } + pub fn parse(date_str: &str, time_zone: &str) -> Result<Self, EventError> { let absolute_time = date_str.chars().rev().next().unwrap() == 'Z'; let tz: Tz = if absolute_time { @@ -86,6 +97,17 @@ impl PartialEq for Date { } } +impl Add<Duration> for Date { + type Output = Date; + + fn add(self, other: Duration) -> Date { + match self { + Date::Time(d) => Date::Time(d + other), + Date::AllDay(d) => Date::AllDay(d + other), + } + } +} + fn cmp_date_time<T: TimeZone>(date: &chrono::Date<T>, time: &chrono::DateTime<T>) -> Ordering { let d2 = time.date(); if date.eq(&d2) { |