Crate curds_cron[−][src]
A library for creating and consuming Cron expressions. It supports some extended syntax.
Overview
Each expression consists of five fields, space separated, representing different parts of the day in the following order:
- Minute
- Hour
- Day of Month
- Month
- Day of Week
Each field consists of some number of values, comma separated, indicating when the expression is a match. In addition to the regular:
Expressions can also include:
- Step ranges
- Weekday nearest to Day of Month
- Last Day of Month (with or without an offset)
- Nth Day of Week
- Last Day of Week
Additionally, Month and Day of Week values can be represented numerically or with a three-letter abbreviation (JAN -> 01 or wed -> 3, respectively).
Examples
Wildcard Values
An expression that is always a match.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let anytime = "* * * * *".parse::<CronExpression>()?; assert_eq!(true, anytime.is_match(&Utc::now()));
Single Values
An expression that matches at the start of a new year.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let new_year = "0 0 1 1 *".parse::<CronExpression>()?; assert_eq!(true, new_year.is_match(&"2021-01-01T00:00:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(false, new_year.is_match(&"2021-01-01T00:01:00Z".parse::<DateTime<Utc>>()?));
Range Values
An expression that matches in the middle of everything.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let middles = "15-45 6-18 10-20 3-9 2-4".parse::<CronExpression>()?; assert_eq!(true, middles.is_match(&"2021-04-13T07:24:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(false, middles.is_match(&"2021-04-13T18:46:00Z".parse::<DateTime<Utc>>()?));
Step Range Values
An expression that matches on even minutes in even hours on odd days in odd months.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let steps = "*/2 */2 */2 */2 *".parse::<CronExpression>()?; assert_eq!(true, steps.is_match(&"2021-01-05T06:44:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(false, steps.is_match(&"2021-01-05T06:43:00Z".parse::<DateTime<Utc>>()?));
Weekday Nearest to Day of Month
An expression that matches the weekday nearest to the 10th in August.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let tenth = "* * 10W Aug *".parse::<CronExpression>()?; assert_eq!(false, tenth.is_match(&"2019-08-10T00:00:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(true, tenth.is_match(&"2019-08-09T00:00:00Z".parse::<DateTime<Utc>>()?));
Last Day of Month
An expression that matches on the two last days of every month.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let last_two = "* * L,L-1 * *".parse::<CronExpression>()?; assert_eq!(false, last_two.is_match(&"2021-04-28T00:00:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(true, last_two.is_match(&"2021-04-29T00:00:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(true, last_two.is_match(&"2021-04-30T00:00:00Z".parse::<DateTime<Utc>>()?));
Nth Day of Week
An expression that matches on the 2nd Monday of June.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let monday = "* * * jun 1#2".parse::<CronExpression>()?; assert_eq!(false, monday.is_match(&"2021-06-07T12:00:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(true, monday.is_match(&"2021-06-14T12:00:00Z".parse::<DateTime<Utc>>()?));
Last Day of Week
An expression that matches on the last Friday of every month.
use chrono::{DateTime, Utc}; use curds_cron::CronExpression; let friday = "* * * * FriL".parse::<CronExpression>()?; assert_eq!(false, friday.is_match(&"2021-01-22T00:00:00Z".parse::<DateTime<Utc>>()?)); assert_eq!(true, friday.is_match(&"2021-01-29T00:00:00Z".parse::<DateTime<Utc>>()?));
Structs
CronExpression | A representation of a Cron Expression. |
Enums
CronDatePart | An enum to represent the various parts of a DateTime against which a CronExpression can match. |
CronParsingError | An enum covering the possible failures when parsing a CronExpression. |