saffron

Struct Cron

Source
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

Source

pub fn new(expr: CronExpr) -> Self

Simplifies the cron expression into a cron value.

Source

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

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

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

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

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

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§

Source§

impl Clone for Cron

Source§

fn clone(&self) -> Cron

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cron

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Cron

Source§

type Err = CronParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Cron

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Cron

Source§

fn eq(&self, other: &Cron) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Cron

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.