pub struct Cron { /* private fields */ }
Expand description
A cron value. This can be used to iterate over all future matching times or quickly check if a given time matches.
§Example
use saffron::Cron;
use chrono::prelude::*;
let cron: Cron = "*/10 0 * OCT MON".parse().expect("Couldn't parse expression!");
// check if a given time is contained in an expression
assert!(cron.contains(Utc.ymd(2020, 10, 19).and_hms(0, 30, 0)));
// iterate over all future matching times
for time in cron.clone().iter_from(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0)).take(5) {
// Prints
// 1970-10-05 00:00:00 UTC
// 1970-10-05 00:10:00 UTC
// 1970-10-05 00:20:00 UTC
// 1970-10-05 00:30:00 UTC
// 1970-10-05 00:40:00 UTC
println!("{}", time);
assert!(cron.contains(time));
}
Implementations§
Source§impl Cron
impl Cron
Sourcepub fn any(&self) -> bool
pub fn any(&self) -> bool
Returns whether this cron value will ever match any giving time.
Some values can never match any given time. If an value matches for a day of the month that’s beyond any of the valid days of the months matched then the value can never match.
§Example
use saffron::Cron;
// Does have any since February has a 29th day on leap years
assert!("* * 29 2 *".parse::<Cron>().unwrap().any());
// Does not have any since November does not have a 31st day
assert!(!"* * 31 11 *".parse::<Cron>().unwrap().any());
Sourcepub fn contains(&self, dt: DateTime<Utc>) -> bool
pub fn contains(&self, dt: DateTime<Utc>) -> bool
Returns whether this cron value matches the given time.
§Example
use saffron::Cron;
use chrono::prelude::*;
let cron: Cron = "*/10 0 * OCT MON".parse().expect("Couldn't parse expression!");
// check if a given time is contained in an expression
assert!(cron.contains(Utc.ymd(2020, 10, 19).and_hms(0, 30, 0)));
Sourcepub fn iter_from(self, start: DateTime<Utc>) -> CronTimesIter ⓘ
pub fn iter_from(self, start: DateTime<Utc>) -> CronTimesIter ⓘ
Creates an iterator of date times that match with the cron value.
§Example
use saffron::Cron;
use chrono::prelude::*;
let cron = "*/10 * * * *".parse::<Cron>().expect("Couldn't parse expression!");
for time in cron.iter_from(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0)).take(5) {
// Prints
// 1970-01-01 00:00:00 UTC
// 1970-01-01 00:10:00 UTC
// 1970-01-01 00:20:00 UTC
// 1970-01-01 00:30:00 UTC
// 1970-01-01 00:40:00 UTC
println!("{}", time)
}
Sourcepub fn iter_after(self, start: DateTime<Utc>) -> CronTimesIter ⓘ
pub fn iter_after(self, start: DateTime<Utc>) -> CronTimesIter ⓘ
Creates an iterator of date times that match with the cron value after the given date.
§Example
use saffron::Cron;
use chrono::prelude::*;
let cron = "*/10 * * * *".parse::<Cron>().expect("Couldn't parse expression!");
for time in cron.iter_after(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0)).take(5) {
// Prints
// 1970-01-01 00:10:00 UTC
// 1970-01-01 00:20:00 UTC
// 1970-01-01 00:30:00 UTC
// 1970-01-01 00:40:00 UTC
// 1970-01-01 00:50:00 UTC
println!("{}", time)
}
Sourcepub fn next_from(&self, date: DateTime<Utc>) -> Option<DateTime<Utc>>
pub fn next_from(&self, date: DateTime<Utc>) -> Option<DateTime<Utc>>
Returns the next time the cron will match including the given date.
§Example
use saffron::Cron;
use chrono::prelude::*;
let cron = "*/10 * * * *".parse::<Cron>().expect("Couldn't parse expression!");
let date = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0);
// the given date matches the expression, so we get the same date back (truncated)
assert_eq!(cron.next_from(date), Some(date));
Sourcepub fn next_after(&self, date: DateTime<Utc>) -> Option<DateTime<Utc>>
pub fn next_after(&self, date: DateTime<Utc>) -> Option<DateTime<Utc>>
Returns the next time the cron will match after the given date.
§Example
use saffron::Cron;
use chrono::prelude::*;
let cron = "*/10 * * * *".parse::<Cron>().expect("Couldn't parse expression!");
let date = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0);
assert_eq!(cron.next_after(date), date.with_minute(10));
Trait Implementations§
impl Eq for Cron
impl StructuralPartialEq for Cron
Auto Trait Implementations§
impl Freeze for Cron
impl RefUnwindSafe for Cron
impl Send for Cron
impl Sync for Cron
impl Unpin for Cron
impl UnwindSafe for Cron
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)