#cron-expression #solana #expression-parser #month #run-time #schedule #safe

mat-clockwork-cron

A cron expression parser that's safe to use in the Solana runtime

1 stable release

5.9.0 Apr 24, 2023
5.7.0 Apr 24, 2023
4.0.0 Apr 24, 2023
3.0.0 Apr 24, 2023
2.0.9 Mar 31, 2023

#19 in #month

Download history 108/week @ 2024-07-28 56/week @ 2024-09-22 13/week @ 2024-09-29

130 downloads per month
Used in 5 crates (3 directly)

AGPL-3.0-or-later

65KB
1.5K SLoC

clockwork-cron

A cron expression parser that's safe to use in the Solana runtime. Works with stable Rust v1.28.0.

use clockwork_cron::Schedule;
use chrono::{DateTime, NaiveDateTime, Utc};
use std::str::FromStr;

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();
  let ts = 1234567890;
  let next_ts = schedule
      .after(&DateTime::<Utc>::from_utc(
          NaiveDateTime::from_timestamp(ts, 0),
          Utc,
      ))
      .take(1)
      .next()
    {
        Some(datetime) => Some(datetime.timestamp()),
        None => None,
    }
}

/*
Upcoming fire times:
-> 2018-06-01 09:30:00 UTC
-> 2018-06-01 12:30:00 UTC
-> 2018-06-01 15:30:00 UTC
-> 2018-06-15 09:30:00 UTC
-> 2018-06-15 12:30:00 UTC
-> 2018-06-15 15:30:00 UTC
-> 2018-08-01 09:30:00 UTC
-> 2018-08-01 12:30:00 UTC
-> 2018-08-01 15:30:00 UTC
-> 2018-08-15 09:30:00 UTC
*/

⚠️ Syntax

sec  min   hour   day of month   month   day of week   year

If you use tools such as crontab guru, note that the clockwork parser is a 7 columns string. You probably need to add the seconds (left most column) and can optionally add the year (right most column). e.g. the following 5 columns cron:

min hour day of month month day of week
0 18 * * FRI

becomes

sec min hour day of month month day of week year
0 0 18 * * FRI *

or

sec min hour day of month month day of week
0 0 18 * * FRI

These are also supported:

"@yearly"
"@weekly"
"@daily"
"@hourly"

Dependencies

~2MB
~37K SLoC