tpt-cron-parse 0.1.0

Cron expression parser with precise error reporting and human-readable output
Documentation
  • Coverage
  • 100%
    30 out of 30 items documented4 out of 8 items with examples
  • Size
  • Source code size: 37.48 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 609.86 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • tpt-solutions/tpt-data-parsers
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • PhillipC05

tpt-cron-parse

docs.rs crates.io

Cron expression parser with precise error reporting and human-readable output. No dependencies.

Supports standard 5-field and extended 6-field (with seconds) cron syntax.

Features

  • 5-field (min hour dom month dow) and 6-field (sec min hour dom month dow) cron
  • Precise errorsCronError includes the byte position, which field failed, what was expected, and what was found
  • Human-readableto_human_readable() converts expressions to English
  • No dependencies — pure Rust

Usage

use tpt_cron_parse::CronExpr;

let expr = CronExpr::parse("0 9 * * 1").unwrap();
println!("{}", expr.to_human_readable()); // Every Monday at 9:00 AM

let expr = CronExpr::parse("*/5 * * * *").unwrap();
println!("{}", expr.to_human_readable()); // Every 5 minutes

Error Reporting

use tpt_cron_parse::CronExpr;

let err = CronExpr::parse("x * * * *").unwrap_err();
println!("{}", err); // cron parse error in minutes field at position 0: expected digit, found 'x'

Human-Readable Examples

Expression Output
* * * * * Every minute
0 * * * * Every hour
0 9 * * * Every day at 9:00 AM
0 9 * * 1 Every Monday at 9:00 AM
0 9 1 * * At 9:00 AM on the 1st of every month
*/5 * * * * Every 5 minutes
0 0 1 1 * At 12:00 AM on January 1st

Computing the next run time

The crate stays dependency-free by default. Enable the optional chrono feature to compute the next firing time of a schedule:

[dependencies]
tpt-cron-parse = { version = "0.1", features = ["chrono"] }
use tpt_cron_parse::CronExpr;
use chrono::{TimeZone, Utc};

let expr = CronExpr::parse("0 9 * * 1-5").unwrap();
let after = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap(); // Monday
let next = expr.next_after(after).unwrap(); // next weekday at 09:00

next_after respects the standard cron day-of-month / day-of-week OR rule and searches at most ~4 years ahead (the maximum period of a cron schedule).

License

Licensed under either of Apache License 2.0 or MIT at your option.