diff --git a/Cargo.toml b/Cargo.toml index eb32b35..241f3ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,6 @@ license = "MIT" name = "cron" [dependencies] -chrono = "~0.3" +chrono = "~0.4" nom = "~2.1" error-chain="~0.10.0" diff --git a/src/schedule.rs b/src/schedule.rs index 2339707..0c546ce 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -2,7 +2,7 @@ use nom::*; use std::str::{self, FromStr}; use std::collections::BTreeSet; use std::collections::Bound::{Included, Unbounded}; -use chrono::{UTC, DateTime, Duration, Datelike, Timelike}; +use chrono::{Utc, DateTime, Duration, Datelike, Timelike}; use chrono::offset::TimeZone; use std::iter::{self, Iterator}; use error::{Error, ErrorKind}; @@ -25,7 +25,7 @@ impl Schedule { if number_of_fields != 6 && number_of_fields != 7 { bail!(ErrorKind::Expression(format!("Expression has {} fields. Valid cron \ expressions have 6 or 7.", - number_of_fields))); + number_of_fields))); } let mut iter = fields.into_iter(); @@ -36,7 +36,9 @@ impl Schedule { let days_of_month = DaysOfMonth::from_field(iter.next().unwrap())?; let months = Months::from_field(iter.next().unwrap())?; let days_of_week = DaysOfWeek::from_field(iter.next().unwrap())?; - let years: Years = iter.next().map(Years::from_field).unwrap_or_else(|| Ok(Years::all()))?; + let years: Years = iter.next() + .map(Years::from_field) + .unwrap_or_else(|| Ok(Years::all()))?; Ok(Schedule::from(seconds, minutes, @@ -84,9 +86,9 @@ impl Schedule { // println!("Looking for next schedule time after {}", after.to_rfc3339()); for year in self.years - .ordinals() - .range((Included(datetime.year() as u32), Unbounded)) - .cloned() { + .ordinals() + .range((Included(datetime.year() as u32), Unbounded)) + .cloned() { let month_start = month_starts.next().unwrap(); let month_end = Months::inclusive_max(); @@ -99,9 +101,9 @@ impl Schedule { let day_of_month_range = (Included(day_of_month_start), Included(day_of_month_end)); 'day_loop: for day_of_month in self.days_of_month - .ordinals() - .range(day_of_month_range) - .cloned() { + .ordinals() + .range(day_of_month_range) + .cloned() { let hour_start = hour_starts.next().unwrap(); let hour_end = Hours::inclusive_max(); @@ -121,11 +123,12 @@ impl Schedule { for second in self.seconds.ordinals().range(second_range).cloned() { let timezone = datetime.timezone(); - let candidate = timezone.ymd(year as i32, month, day_of_month) + let candidate = timezone + .ymd(year as i32, month, day_of_month) .and_hms(hour, minute, second); if !self.days_of_week - .ordinals() - .contains(&candidate.weekday().number_from_sunday()) { + .ordinals() + .contains(&candidate.weekday().number_from_sunday()) { continue 'day_loop; } return Some(candidate); @@ -150,10 +153,10 @@ impl Schedule { None } - pub fn upcoming(& self, timezone: Z) -> ScheduleIterator + pub fn upcoming(&self, timezone: Z) -> ScheduleIterator where Z: TimeZone { - self.after(&timezone.from_utc_datetime(&UTC::now().naive_utc())) + self.after(&timezone.from_utc_datetime(&Utc::now().naive_utc())) } pub fn after<'a, Z>(&'a self, after: &DateTime) -> ScheduleIterator<'a, Z> @@ -502,7 +505,7 @@ fn test_next_after() { let schedule = schedule(expression.as_bytes()); assert!(schedule.is_done()); let schedule = schedule.unwrap().1; - let next = schedule.next_after(&UTC::now()); + let next = schedule.next_after(&Utc::now()); println!("NEXT AFTER for {} {:?}", expression, next); assert!(next.is_some()); } @@ -513,7 +516,7 @@ fn test_upcoming_utc() { let schedule = schedule(expression.as_bytes()); assert!(schedule.is_done()); let schedule = schedule.unwrap().1; - let mut upcoming = schedule.upcoming(UTC); + let mut upcoming = schedule.upcoming(Utc); let next1 = upcoming.next(); assert!(next1.is_some()); let next2 = upcoming.next(); diff --git a/tests/lib.rs b/tests/lib.rs index 7162832..33121c0 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -12,7 +12,7 @@ mod tests { let expression = "0 30 9,12,15 1,15 May-Aug Mon,Wed,Fri 2018/2"; let schedule = Schedule::from_str(expression).unwrap(); println!("README: Upcoming fire times for '{}':", expression); - for datetime in schedule.upcoming(UTC).take(10) { + for datetime in schedule.upcoming(Utc).take(10) { println!("README: -> {}", datetime); } } @@ -22,7 +22,7 @@ mod tests { let expression = "* * * * * * *"; let schedule = Schedule::from_str(expression).unwrap(); println!("All stars: Upcoming fire times for '{}':", expression); - for datetime in schedule.upcoming(UTC).take(10) { + for datetime in schedule.upcoming(Utc).take(10) { println!("All stars: -> {}", datetime); } } @@ -43,7 +43,7 @@ mod tests { fn test_parse_with_lists() { let expression = "1 2,17,51 1-3,6,9-11 4,29 2,3,7 Tues"; let schedule = Schedule::from_str(expression).unwrap(); - let mut date = UTC::now(); + let mut date = Utc::now(); println!("Fire times for {}:", expression); for _ in 0..20 { date = schedule.after(&date).next().expect("No further dates!"); @@ -57,7 +57,7 @@ mod tests { let expression = "0 2,17,51 1-3,6,9-11 4,29 2,3,7 Wed"; let schedule = Schedule::from_str(expression).unwrap(); println!("Upcoming fire times for '{}':", expression); - for datetime in schedule.upcoming(UTC).take(12) { + for datetime in schedule.upcoming(Utc).take(12) { println!("-> {}", datetime); } assert!(true); @@ -85,7 +85,10 @@ mod tests { fn test_next_utc() { let expression = "1 2 3 4 10 Fri"; let schedule = Schedule::from_str(expression).unwrap(); - let next = schedule.upcoming(UTC).next().expect("There was no upcoming fire time."); + let next = schedule + .upcoming(Utc) + .next() + .expect("There was no upcoming fire time."); println!("Next fire time: {}", next.to_rfc3339()); } @@ -93,62 +96,62 @@ mod tests { fn test_yearly() { let expression = "@yearly"; let schedule = Schedule::from_str(expression).expect("Failed to parse @yearly."); - let starting_date = UTC.ymd(2017, 6, 15).and_hms(14, 29, 36); + let starting_date = Utc.ymd(2017, 6, 15).and_hms(14, 29, 36); let mut events = schedule.after(&starting_date); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2019, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2020, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2019, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2020, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); } #[test] fn test_monthly() { let expression = "@monthly"; let schedule = Schedule::from_str(expression).expect("Failed to parse @monthly."); - let starting_date = UTC.ymd(2017, 10, 15).and_hms(14, 29, 36); + let starting_date = Utc.ymd(2017, 10, 15).and_hms(14, 29, 36); let mut events = schedule.after(&starting_date); - assert_eq!(UTC.ymd(2017, 11, 1).and_hms(0, 0, 0), + assert_eq!(Utc.ymd(2017, 11, 1).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2017, 12, 1).and_hms(0, 0, 0), + assert_eq!(Utc.ymd(2017, 12, 1).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); } #[test] fn test_weekly() { let expression = "@weekly"; let schedule = Schedule::from_str(expression).expect("Failed to parse @weekly."); - let starting_date = UTC.ymd(2016, 12, 23).and_hms(14, 29, 36); + let starting_date = Utc.ymd(2016, 12, 23).and_hms(14, 29, 36); let mut events = schedule.after(&starting_date); - assert_eq!(UTC.ymd(2016, 12, 25).and_hms(0, 0, 0), + assert_eq!(Utc.ymd(2016, 12, 25).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2017, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2017, 1, 8).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2017, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2017, 1, 8).and_hms(0, 0, 0), events.next().unwrap()); } #[test] fn test_daily() { let expression = "@daily"; let schedule = Schedule::from_str(expression).expect("Failed to parse @daily."); - let starting_date = UTC.ymd(2016, 12, 29).and_hms(14, 29, 36); + let starting_date = Utc.ymd(2016, 12, 29).and_hms(14, 29, 36); let mut events = schedule.after(&starting_date); - assert_eq!(UTC.ymd(2016, 12, 30).and_hms(0, 0, 0), + assert_eq!(Utc.ymd(2016, 12, 30).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2016, 12, 31).and_hms(0, 0, 0), + assert_eq!(Utc.ymd(2016, 12, 31).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2017, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2017, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); } #[test] fn test_hourly() { let expression = "@hourly"; let schedule = Schedule::from_str(expression).expect("Failed to parse @hourly."); - let starting_date = UTC.ymd(2017, 2, 25).and_hms(22, 29, 36); + let starting_date = Utc.ymd(2017, 2, 25).and_hms(22, 29, 36); let mut events = schedule.after(&starting_date); - assert_eq!(UTC.ymd(2017, 2, 25).and_hms(23, 0, 0), + assert_eq!(Utc.ymd(2017, 2, 25).and_hms(23, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2017, 2, 26).and_hms(0, 0, 0), + assert_eq!(Utc.ymd(2017, 2, 26).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2017, 2, 26).and_hms(1, 0, 0), + assert_eq!(Utc.ymd(2017, 2, 26).and_hms(1, 0, 0), events.next().unwrap()); } @@ -156,19 +159,26 @@ mod tests { fn test_step_schedule() { let expression = "0/20 0/5 0 1 1 * *"; let schedule = Schedule::from_str(expression).expect("Failed to parse expression."); - let starting_date = UTC.ymd(2017, 6, 15).and_hms(14, 29, 36); + let starting_date = Utc.ymd(2017, 6, 15).and_hms(14, 29, 36); let mut events = schedule.after(&starting_date); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 0, 20), events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 0, 40), events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 0, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 0, 20), + events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 0, 40), + events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 5, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 5, 20), events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 5, 40), events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 5, 0), events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 5, 20), + events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 5, 40), + events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 10, 0), events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 10, 20), events.next().unwrap()); - assert_eq!(UTC.ymd(2018, 1, 1).and_hms(0, 10, 40), events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 10, 0), + events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 10, 20), + events.next().unwrap()); + assert_eq!(Utc.ymd(2018, 1, 1).and_hms(0, 10, 40), + events.next().unwrap()); } }