Expand description
§Croner
Croner is a fully-featured, lightweight, and efficient Rust library designed for parsing and evaluating cron patterns.
§Features
- Parses a wide range of cron expressions, including extended formats.
- Evaluates cron patterns to calculate upcoming execution times.
- Supports time zone-aware scheduling.
- Offers granularity up to seconds for precise task scheduling.
- Compatible with the
chrono
library for dealing with date and time in Rust.
§Example
The following example demonstrates how to use Croner to parse a cron expression and find the next occurrence of a specified time:
use chrono::Utc;
use croner::Cron;
// Parse a cron expression to find the next occurrence at 00:00 on Friday
let cron = Cron::new("0 0 * * FRI").parse().expect("Successful parsing");
// Get the next occurrence from the current time, excluding the current time
let next = cron.find_next_occurrence(&Utc::now(), false).unwrap();
println!(
"Pattern \"{}\" will match next at {}",
cron.pattern.to_string(),
next
);
In this example, Cron::new("0 0 * * FRI")
creates a new Cron instance for the pattern that represents every Friday at midnight. The find_next_occurrence
method calculates the next time this pattern will be true from the current moment.
The false
argument in find_next_occurrence
specifies that the current time is not included in the calculation, ensuring that only future occurrences are considered.
§Getting Started
To start using Croner, add it to your project’s Cargo.toml
and follow the examples to integrate cron pattern parsing and scheduling into your application.
§Pattern
The expressions used by Croner are very similar to those of Vixie Cron, but with a few additions as outlined below:
// ┌──────────────── (optional) second (0 - 59)
// │ ┌────────────── minute (0 - 59)
// │ │ ┌──────────── hour (0 - 23)
// │ │ │ ┌────────── day of month (1 - 31)
// │ │ │ │ ┌──────── month (1 - 12, JAN-DEC)
// │ │ │ │ │ ┌────── day of week (0 - 6, SUN-Mon)
// │ │ │ │ │ │ (0 to 6 are Sunday to Saturday; 7 is Sunday, the same as 0)
// │ │ │ │ │ │
// * * * * * *
Field | Required | Allowed values | Allowed special characters | Remarks |
---|---|---|---|---|
Seconds | Optional | 0-59 | * , - / ? | |
Minutes | Yes | 0-59 | * , - / ? | |
Hours | Yes | 0-23 | * , - / ? | |
Day of Month | Yes | 1-31 | * , - / ? L W | |
Month | Yes | 1-12 or JAN-DEC | * , - / ? | |
Day of Week | Yes | 0-7 or SUN-MON | * , - / ? # L | 0 to 6 are Sunday to Saturday, 7 is Sunday, the same as 0. ‘#’ is used to specify the nth occurrence of a weekday |
For more information, refer to the full README.