From e5ea51ee26a0c37c5152a248dcd89f605cbd83c6 Mon Sep 17 00:00:00 2001 From: Ian Roddis Date: Fri, 3 Dec 2021 15:18:43 -0400 Subject: [PATCH] Fixing issue with date range --- Cargo.lock | 24 ++++++++++++------------ Cargo.toml | 2 +- src/calendar.rs | 22 +++++++++++++++++++--- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc2d474..e33175a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,6 +478,18 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "cronkin" +version = "0.1.0" +dependencies = [ + "actix-web", + "chrono", + "crossbeam", + "pam", + "serde", + "serde_json", +] + [[package]] name = "crossbeam" version = "0.8.1" @@ -601,18 +613,6 @@ dependencies = [ "syn", ] -[[package]] -name = "ezsched" -version = "0.1.0" -dependencies = [ - "actix-web", - "chrono", - "crossbeam", - "pam", - "serde", - "serde_json", -] - [[package]] name = "flate2" version = "1.0.22" diff --git a/Cargo.toml b/Cargo.toml index 0202ab9..35af363 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ezsched" +name = "cronkin" version = "0.1.0" edition = "2021" diff --git a/src/calendar.rs b/src/calendar.rs index 7c41bbe..a8e3481 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -155,6 +155,8 @@ impl Calendar { ) -> Option { let mut actual = date.clone(); + println!("Adjusting {:?}", date); + let is_blocked = |x: NaiveDate| -> bool { (!self.dow.contains(&x.weekday())) || holidays.contains(&x) }; @@ -215,11 +217,12 @@ impl Calendar { /// Returns the set of valid calendar dates within the specified range pub fn date_range(&self, from: NaiveDate, to: NaiveDate) -> Vec { let mut result = Vec::new(); - let mut cur = from; + let mut cur = from.pred(); let year = from.year(); let mut holidays = self.get_holidays(cur); - while cur < to { + while cur <= to { + cur = cur.succ(); if cur.year() != year { holidays = self.get_holidays(cur); } @@ -227,7 +230,6 @@ impl Calendar { continue; } result.push(cur); - cur = cur.succ(); } result } @@ -302,8 +304,22 @@ mod tests { since: None, until: None, }, + DateSpec::MonthDay { + month: January, + day: 1u32, + observed: AdjustmentPolicy::Next, + description: "New Years Day".to_owned(), + since: None, + until: None, + }, ], inherits: vec![], }; + + let myrange = cal.date_range( + NaiveDate::from_ymd(2021, 12, 15), + NaiveDate::from_ymd(2022, 01, 15), + ); + assert_eq!(myrange.len(), 20); } }