cron_lite/lib.rs
1//! Lightweight cron expression parser and time series generator.
2#![deny(unsafe_code, warnings, missing_docs)]
3
4//! This is a tiny crate, intended to:
5//! - parse almost all kinds of popular cron schedule formats;
6//! - generate series of timestamps according to the schedule.
7//!
8//! It has a single external dependency - [chrono](https://crates.io/crates/chrono).
9//!
10//! _This is not a cron jobs scheduler or runner._ If you need a scheduler/runner,
11//! look for [sacs](https://crates.io/crates/sacs)
12//! of any [other similar crate](https://crates.io/search?q=async%20cron%20scheduler).
13//!
14//! ## Cron schedule format
15//!
16//! Traditionally, cron schedule expression has a 5-fields format: minutes, hours, days, months and days of week.
17//! This crate uses such a format by default, but two optional fields may be added, seconds and years:
18//! - if _seconds_ is empty, `0` is used by default;
19//! - if _years_ is empty, `*` is used by default;
20//! - if 6-fields schedule is specified, then _seconds_ filed is assumed as first and years as empty (default).
21//!
22//! The table below describes valid values and patterns of each field:
23//!
24//! | Field | Required | Allowed values | Allowed special characters |
25//! |--------------|----------|-----------------|----------------------------|
26//! | Seconds | No | 0-59 | * , - / |
27//! | Minutes | Yes | 0-59 | * , - / |
28//! | Hours | Yes | 0-23 | * , - / |
29//! | Day of Month | Yes | 1-31 | * , - / ? L W |
30//! | Month | Yes | 1-12 or JAN-DEC | * , - / |
31//! | Day of Week | Yes | 0-6 or SUN-SAT | * , - ? L # |
32//! | Year | No | 1970-2099 | * , - / |
33//!
34//! Patterns meanings:
35//! - `*` - each possible value, i.e. `0,1,2,...,59` for minutes;
36//! - `,` - list of values or patterns, i.e. `1,7,12`, `SUN,FRI`;
37//! - `-` - range of values, i.e. `0-15`, `JAN-MAR`;
38//! - `/` - repeating values, i.e. `*/12`, `10/5`, `30-59/2`;
39//! - `L` - last day of the month (for month field), or last particular day of the week (for weekday field), i.e. `L` or `5L`;
40//! - `W` - the weekday (not Sunday or Saturday), nearest to the specified days of month in the same month, i.e. `22W`;
41//! - `#` - specific day of the week, i.e. `fri#1`, `1#4`;
42//! - `?` - for days of month or week means that value doesn't matter: if day of month is specified (not `*`), then day of week should be `?` and vise versa.
43//!
44//! Also, short aliases for well-known schedule expressions are allowed:
45//!
46//! | Alias | Expression |
47//! |----------------------------|---------------|
48//! | `@yearly` (or `@annually`) | 0 0 0 1 1 ? * |
49//! | `@monthly` | 0 0 0 1 * ? * |
50//! | `@weekly` | 0 0 0 ? * 0 * |
51//! | `@daily` (or `@midnight`) | 0 0 0 * * * * |
52//! | `@hourly` | 0 0 * * * * * |
53//!
54//! Some additional information and fields description and relationships may be found [here](https://en.wikipedia.org/wiki/Cron#Cron_expression) (this is not complete or exceptional documentation).
55//!
56//! ### Schedule with timezone
57//! If `tz` feature is enabled, it's possible to prefix cron schedule with timezone, for example:
58//! - `TZ=Europe/Paris @monthly`
59//! - `TZ=EET 0 12 * * *`
60//!
61//! ## How to use
62//!
63//! The single public entity of the crate is a [`Schedule`] structure, which has three basic methods:
64//! - [new()](Schedule::new): constructor to parse and validate provided schedule;
65//! - [upcoming()](Schedule::upcoming): returns time of the next schedule's event, starting from the provided timestamp;
66//! - [iter()](Schedule::iter): returns an `Iterator` which produces a series of timestamps according to the schedule.
67//!
68//! ### Example with `upcoming`
69//! ```rust
70//! use chrono::Utc;
71//! use cron_lite::{Result, Schedule};
72//!
73//! fn upcoming() -> Result<()> {
74//! let schedule = Schedule::new("0 0 0 * * *")?;
75//! let now = Utc::now();
76//!
77//! // Get the next event's timestamp starting from now
78//! let next = schedule.upcoming(&now);
79//! assert!(next.is_some());
80//!
81//! println!("next: {:?}", next.unwrap());
82//!
83//!
84//! Ok(())
85//! }
86//! ```
87//!
88//! ### Example with `iter`
89//! ```rust
90//! use chrono::Utc;
91//! use cron_lite::{Result, Schedule};
92//!
93//! fn iterator() -> Result<()> {
94//! let schedule = Schedule::new("0 0 0 * * *")?;
95//! let now = Utc::now();
96//!
97//! // Get the next 10 timestamps starting from now
98//! schedule.iter(&now).take(10).for_each(|t| println!("next: {t}"));
99//!
100//! Ok(())
101//! }
102//! ```
103//!
104//! # Feature flags
105//! * `serde`: adds [`Serialize`](https://docs.rs/serde/latest/serde/trait.Serialize.html) and [`Deserialize`](https://docs.rs/serde/latest/serde/trait.Deserialize.html) trait implementation for [`Schedule`].
106//! * `tz`: enables support of cron [schedules with timezone](#schedule-with-timezone).
107
108/// Crate specific Error implementation.
109pub mod error;
110mod pattern;
111/// Cron schedule pattern parser and upcoming event generator.
112pub mod schedule;
113mod series;
114mod utils;
115
116/// Re-export of public entities.
117pub use error::CronError;
118pub use schedule::Schedule;
119
120/// Convenient alias for `Result`.
121pub type Result<T, E = CronError> = std::result::Result<T, E>;