From 098ac045545d4149b0062bbb365bf7467eb2bff3 Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Tue, 19 May 2020 11:29:25 +0200 Subject: Add support for ical's BYDAY recursion rule --- src/date.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/date.rs') diff --git a/src/date.rs b/src/date.rs index ab1c2c8..98e052a 100644 --- a/src/date.rs +++ b/src/date.rs @@ -1,10 +1,10 @@ use std::cmp::{Ordering, Ord}; -use std::ops::Add; +use std::ops::{Add, Sub}; use errors::EventError; use chrono; -use chrono::{TimeZone, Duration, Datelike, Local}; +use chrono::{TimeZone, Duration, Datelike, Local, Weekday}; use chrono::offset::Utc; use chrono_tz::{Tz, UTC}; @@ -79,6 +79,13 @@ impl Date { } } + pub fn weekday(&self) -> Weekday { + match *self { + Date::Time(t) => t.weekday(), + Date::AllDay(d) => d.weekday(), + } + } + pub fn month(&self) -> u32 { match *self { Date::Time(t) => t.month(), @@ -144,6 +151,27 @@ impl Add for Date { } } +impl Sub for Date { + type Output = Duration; + + fn sub(self, other: Self) -> Duration { + match self { + Date::Time(t1) => { + match other { + Date::Time(t2) => t1 - t2, + Date::AllDay(d) => t1.date() - d, + } + } + Date::AllDay(d1) => { + match other { + Date::Time(t) => d1 - t.date(), + Date::AllDay(d2) => d1 - d2, + } + } + } + } +} + fn cmp_date_time(date: &chrono::Date, time: &chrono::DateTime) -> Ordering { let d2 = time.date(); if date.eq(&d2) { -- cgit v1.2.3