Struct saffron::Cron[][src]

pub struct Cron { /* fields omitted */ }

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

impl Cron[src]

pub fn new(expr: CronExpr) -> Self[src]

Simplifies the cron expression into a cron value.

pub fn any(&self) -> bool[src]

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());

pub fn contains(&self, dt: DateTime<Utc>) -> bool[src]

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)));

pub fn iter_from(self, start: DateTime<Utc>) -> CronTimesIter

Notable traits for CronTimesIter

impl Iterator for CronTimesIter type Item = DateTime<Utc>;
[src]

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)
}

pub fn iter_after(self, start: DateTime<Utc>) -> CronTimesIter

Notable traits for CronTimesIter

impl Iterator for CronTimesIter type Item = DateTime<Utc>;
[src]

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)
}

pub fn next_from(&self, date: DateTime<Utc>) -> Option<DateTime<Utc>>[src]

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));

pub fn next_after(&self, date: DateTime<Utc>) -> Option<DateTime<Utc>>[src]

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 Clone for Cron[src]

impl Debug for Cron[src]

impl Eq for Cron[src]

impl FromStr for Cron[src]

type Err = CronParseError

The associated error which can be returned from parsing.

impl Hash for Cron[src]

impl PartialEq<Cron> for Cron[src]

impl StructuralEq for Cron[src]

impl StructuralPartialEq for Cron[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.