Crates.io | cron-lite |
lib.rs | cron-lite |
version | |
source | src |
created_at | 2024-12-10 18:10:15.811513 |
updated_at | 2024-12-10 18:10:15.811513 |
description | Lightweight cron expressions parser and time series generator |
homepage | https://github.com/alex-karpenko/cron-lite |
repository | https://github.com/alex-karpenko/cron-lite |
max_upload_size | |
id | 1478865 |
Cargo.toml error: | TOML parse error at line 28, column 1 | 28 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Lightweight cron expressions parser and time series generator.
This is a tiny crate, intended to:
It has the single external dependency - chrono.
This is not a cron jobs scheduler or runner. If you need a scheduler/runner, look for sacs of any other similar crate.
Traditionally, cron schedule expression has a 5-fields format: minutes, hours, days, months and days of week. This crate uses such format by default, but two optional fields may be added, seconds and years:
0
is used by default;*
is used by default;The table below describes valid values and patterns of each field:
Field | Required | Allowed values | Allowed special characters |
---|---|---|---|
Seconds | No | 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-6 or SUN-SAT | * , - ? L # |
Year | No | 1970-2099 | * , - / |
Patterns meanings:
*
- each possible value, i.e. 0,1,2,...,59
for minutes;,
- list of values or patterns, i.e. 1,7,12
, SUN,FRI
;-
- range of values, i.e. 0-15
, JAN-MAR
;/
- repeating values, i.e. */12
, 10/5
, 30-59/2
;L
- last day of the month (for month field), or last particular day of the week (for weekday field), i.e. L
or
5L
;W
- the weekday (not Sunday or Saturday), nearest to the specified days of month in the same month, i.e. 22W
;#
- specific day of the week, i.e. fri#1
, 1#4
;?
- 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.Some additional information and fields description and relationships may be found here (this is not complete or exceptional documentation).
The single entity of the crate is a Schedule
structure, which has three basic methods:
new()
: constructor to parse and validate provided schedule;upcoming()
: returns time of the next schedule's event, starting from the provided timestamp;iter()
: returns an Iterator
which produces series of timestamps according to the schedule.upcoming
use chrono::Utc;
use cron_lite::{Result, Schedule};
fn main() -> Result<()> {
let schedule = Schedule::new("0 0 0 * * *")?;
let now = Utc::now();
// Get the next event's timestamp starting from now
let next = schedule.upcoming(&now).unwrap();
println!("next: {next}");
Ok(())
}
iter
use chrono::Utc;
use cron_lite::{Result, Schedule};
fn main() -> Result<()> {
let schedule = Schedule::new("0 0 0 * * *")?;
let now = Utc::now();
// Get the next 10 timestamps starting from now
schedule.iter(&now).take(10).for_each(|t| println!("next: {t}"));
Ok(())
}
@yearly
, @annually
, @monthly
, @daily
, @midnight
, @hourly
.tz
: timezone-aware schedule pattern.This project is licensed under the MIT license.