From 5eb5bb17c0f7e93aacc340ce9a28ee352cc91c59 Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Tue, 28 Aug 2018 16:16:17 +0200 Subject: Add +, now and is_empty to Date --- src/date.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 { 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 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(date: &chrono::Date, time: &chrono::DateTime) -> Ordering { let d2 = time.date(); if date.eq(&d2) { -- cgit v1.2.3