Cron expression parsing, scheduling, and human-readable descriptions
cargo add philiprehberger-cron-parser
Cron expression parsing, scheduling, and human-readable descriptions
[dependencies]
philiprehberger-cron-parser = "0.3.0"
use philiprehberger_cron_parser::{CronExpr, DateTime};
// Parse a cron expression
let expr = CronExpr::parse("0 9 * * 1-5").unwrap();
// Check next execution from a given time
let now = DateTime { year: 2026, month: 3, day: 15, hour: 8, minute: 0, second: 0 };
let next = expr.next_from(&now).unwrap();
// next = DateTime { year: 2026, month: 3, day: 16, hour: 9, minute: 0, second: 0 }
// Human-readable description
println!("{}", expr.describe());
// "At 9:00 AM, Monday through Friday"
// Use aliases
let hourly = CronExpr::parse("@hourly").unwrap();
// Parse using FromStr trait
let expr: CronExpr = "*/30 * * * *".parse().unwrap();
// Convert DateTime back to a Unix timestamp
let dt = DateTime { year: 2026, month: 3, day: 15, hour: 9, minute: 0, second: 0 };
let ts = dt.to_timestamp();
// ts = 1773565200
use philiprehberger_cron_parser::{CronExpr, DateTime};
let expr = CronExpr::parse("0 9 * * 1-5").unwrap();
let now = DateTime { year: 2026, month: 3, day: 16, hour: 14, minute: 0, second: 0 };
// Most recent match before `now`
let prev = expr.prev_from(&now).unwrap();
// prev = DateTime { year: 2026, month: 3, day: 16, hour: 9, minute: 0, second: 0 }
// Last 3 matches (reverse chronological)
let recent = expr.prev_n_from(&now, 3);
| Function / Type | Description |
|---|---|
CronExpr::parse(expr) | Parse a cron expression |
.next_from(dt) | Next execution time after dt |
.next_n_from(dt, n) | Next N execution times |
.prev_from(dt) | Most recent execution time before dt |
.prev_n_from(dt, n) | Previous N execution times (reverse chronological) |
.matches(dt) | Check if dt matches the expression |
.describe() | Human-readable description |
DateTime | Simple date/time struct |
cargo test
cargo clippy -- -D warnings
If you find this project useful: