Skip to main content

Crate jiff_cron

Crate jiff_cron 

Source
Expand description

A cron expression parser and schedule explorer built with jiff.

§Examples

use std::str::FromStr;

use jiff_cron::{jiff::tz::TimeZone, Schedule};

fn main() {
    //               sec  min   hour   day of month   month   day of week   year
    let expression = "0   30   9,12,15     1,15       May-Aug  Mon,Wed,Fri  2018/2";
    let schedule = Schedule::from_str(expression).unwrap();
    println!("Upcoming fire times:");
    for datetime in schedule.upcoming(TimeZone::UTC).take(10) {
        println!("-> {}", datetime);
    }
}

/*

Upcoming fire times:

→ 2018-06-01T09:30:00+00:00[UTC]
→ 2018-06-01T12:30:00+00:00[UTC]
→ 2018-06-01T15:30:00+00:00[UTC]
→ 2018-06-15T09:30:00+00:00[UTC]
→ 2018-06-15T12:30:00+00:00[UTC]
→ 2018-06-15T15:30:00+00:00[UTC]
→ 2018-08-01T09:30:00+00:00[UTC]
→ 2018-08-01T12:30:00+00:00[UTC]
→ 2018-08-01T15:30:00+00:00[UTC]
→ 2018-08-15T09:30:00+00:00[UTC]

*/

§DST behavior

jiff also handles daylight savings gaps and folding appropriately:

use std::str::FromStr;

use jiff_cron::{
    jiff::{civil::date, tz::TimeZone},
    Schedule,
};

fn main() {
    let expression = "0 0 * * * * *";
    let schedule = Schedule::from_str(expression).unwrap();
    let after_datetime = date(2022, 11, 5)
        .at(23, 30, 0, 0)
        .in_tz("America/Chicago")
        .unwrap();
    println!("Upcoming fire times:");
    for datetime in schedule.after(&after_datetime).take(5) {
        println!("-> {}", datetime);
    }
}

/*

Upcoming fire times:

→ 2022-11-06T00:00:00-05:00[America/Chicago]
→ 2022-11-06T01:00:00-05:00[America/Chicago]
→ 2022-11-06T01:00:00-06:00[America/Chicago]
→ 2022-11-06T02:00:00-06:00[America/Chicago]
→ 2022-11-06T03:00:00-06:00[America/Chicago]

*/

§Installation

Add to your Cargo.toml:

jiff-cron = "0.2.0"

You can enable optional serde support via crate feature toggle.

Re-exports§

pub use jiff;

Modules§

error
Error types used by this crate.

Structs§

OwnedScheduleIterator
A ScheduleIterator with a static lifetime.
Schedule
ScheduleIterator

Traits§

TimeUnitSpec
Methods exposing a schedule’s configured ordinals for each individual unit of time.