#cron #timestamp #next #schedule #pattern #matching #calculate

cronexpr

Calculate the next timestamp matching a given crontab pattern

5 releases (3 breaking)

new 0.4.0 Sep 18, 2024
0.3.1 Sep 18, 2024
0.3.0 Sep 12, 2024
0.2.0 Sep 11, 2024
0.1.0 Sep 11, 2024

#278 in Hardware support

Download history 462/week @ 2024-09-09

462 downloads per month

Apache-2.0

39KB
686 lines

Crontab Expression Parser and Driver

Crates.io Documentation MSRV 1.75 Apache 2.0 licensed Build Status

Overview

This library provides functionalities to calculate the next timestamp matching a given crontab pattern.

Usage

cargo add cronexpr
fn main() {
    use std::str::FromStr;

    // with jiff timestamp
    let timestamp = jiff::Timestamp::from_str("2024-01-01T00:00:00+08:00").unwrap();
    let crontab = cronexpr::Crontab::from_str("0 0 1 1 * Asia/ Shanghai").unwrap();
    let driver = crontab.drive_with_timestamp(timestamp);
    assert_eq!(driver.find_next_timestamp().unwrap().as_millisecond(), 1735660800000);

    // for compatibility, bridge by timestamp milliseconds (crontab support at most second level so it's fine)
    let crontab: cronexpr::Crontab = "2 4 * * * Asia/ Shanghai".parse().unwrap();
    let driver = crontab.drive_with_timestamp_millis(1704038400000).unwrap();
    assert_eq!(driver.find_next_timestamp_millis().unwrap(), 1704052920000);

    // can also be used as an iterator
    let crontab: cronexpr::Crontab = "2 4 * * * Asia/ Shanghai".parse().unwrap();
    let mut driver = crontab.drive_with_timestamp_millis(1704038400000).unwrap();
    assert_eq!(driver.next_timestamp_millis().unwrap(), 1704052920000);
    assert_eq!(driver.next_timestamp_millis().unwrap(), 1704139320000);
    assert_eq!(driver.next_timestamp_millis().unwrap(), 1704225720000);
}

Dependencies

~4MB
~67K SLoC