Skip to content

Commit

Permalink
Merge pull request #26 from william20111/master
Browse files Browse the repository at this point in the history
chrono 0.4 with UTC -> Utc change
  • Loading branch information
zslayton authored Jul 5, 2017
2 parents 45ffc1d + b744e01 commit f64904b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ license = "MIT"
name = "cron"

[dependencies]
chrono = "~0.3"
chrono = "~0.4"
nom = "~2.1"
error-chain="~0.10.0"
35 changes: 19 additions & 16 deletions src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -150,10 +153,10 @@ impl Schedule {
None
}

pub fn upcoming<Z>(& self, timezone: Z) -> ScheduleIterator<Z>
pub fn upcoming<Z>(&self, timezone: Z) -> ScheduleIterator<Z>
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<Z>) -> ScheduleIterator<'a, Z>
Expand Down Expand Up @@ -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());
}
Expand All @@ -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();
Expand Down
80 changes: 45 additions & 35 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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!");
Expand All @@ -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);
Expand Down Expand Up @@ -85,90 +85,100 @@ 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());
}

#[test]
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());
}

#[test]
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());
}
}

0 comments on commit f64904b

Please sign in to comment.