commit 0b1c6e3dffae463e4b5ac77ffa5e4f5f467b23cf Author: Adrian Malacoda Date: Fri Feb 23 02:30:46 2018 -0600 initial commit diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..03344fd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "daterade" +version = "0.0.1" +authors = ["Adrian Malacoda "] + +[dependencies] +chrono = "0.4.0" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c3b2792 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,84 @@ +extern crate chrono; +use chrono::{Datelike, Timelike, DateTime, Weekday, TimeZone}; + +pub enum Frequency { + Yearly, + Weekly, + Monthly, + Daily, + Hourly, + Minutely, + Secondly +} + +pub struct ByDay { + pub num: i32, + pub weekday: Weekday +} + +pub struct Recurrence { + pub frequency: Frequency, + pub count: Option, + pub interval: Option, + + pub byminute: Option>, + pub byhour: Option>, + pub byday: Option>, + pub byweekno: Option>, + pub bymonth: Option>, + pub bymonthday: Option>, + + pub until: Option> +} + +impl Recurrence { + pub fn iter (&self, start: DateTime) -> DateTimeIterator { + DateTimeIterator { + recurrence: &self, + start: start, + next_datetimes: vec![] + } + } +} + +pub struct DateTimeIterator<'a, Tz: TimeZone + 'a> { + recurrence: &'a Recurrence, + start: DateTime, + + next_datetimes: Vec> +} + +impl<'a, Tz: TimeZone + 'a> DateTimeIterator<'a, Tz> { + fn generate_next_datetimes (&mut self) { + let interval = self.recurrence.interval.unwrap_or(1); + self.start = match self.recurrence.frequency { + Frequency::Yearly => self.start.with_year(self.start.year() + interval).unwrap(), + Frequency::Monthly => self.start.with_month(self.start.month() + interval as u32).unwrap(), + Frequency::Weekly => self.start.with_day(self.start.day() + (7 * interval as u32)).unwrap(), + Frequency::Daily => self.start.with_day(self.start.day() + interval as u32).unwrap(), + Frequency::Hourly => self.start.with_hour(self.start.hour() + interval as u32).unwrap(), + Frequency::Minutely => self.start.with_minute(self.start.minute() + interval as u32).unwrap(), + Frequency::Secondly => self.start.with_second(self.start.second() + interval as u32).unwrap() + } + } +} + +impl<'a, Tz: TimeZone + 'a> Iterator for DateTimeIterator<'a, Tz> { + type Item = DateTime; + + fn next (&mut self) -> Option> { + if self.next_datetimes.is_empty() { + self.generate_next_datetimes(); + } + + self.next_datetimes.pop() + } +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +}