cron-lite
Lightweight cron expressions parser and time series generator.
This tiny crate is intended to:
- parse almost all kinds of popular cron schedule formats;
- generate a series of timestamps according to the schedule.
It has a single external dependency - chrono (with default features set).
This is not a cron jobs scheduler or runner. If you need a scheduler/runner, look for sacs of any other similar crate.
Cron schedule format
Traditionally, cron schedule expression has a 5-fields format: minutes, hours, days, months, and days of the week. This crate uses such a format by default, but two optional fields may be added, seconds and years:
- if seconds is empty,
0is used by default; - if years is empty,
*is used by default; - if the 6-fields schedule is specified, then seconds filed is assumed as first and years as empty (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,...,59for 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.Lor5L;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.
Also, short aliases for well-known schedule expressions are allowed:
| Alias | Expression |
|---|---|
@yearly (or @annually) |
0 0 0 1 1 ? * |
@monthly |
0 0 0 1 * ? * |
@weekly |
0 0 0 ? * 0 * |
@daily (or @midnight) |
0 0 0 * * * * |
@hourly |
0 0 * * * * * |
Some additional information and fields description and relationships may be found here (this is not complete or exceptional documentation).
Schedule with timezone
If tz feature is enabled, it's possible to prefix cron schedule with timezone, for example:
TZ=Europe/Paris @monthlyTZ=EET 0 12 * * *
How to use
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 anIteratorwhich produces series of timestamps according to the schedule;sleep(): falls asleep until time of the upcoming schedule's event (asyncfeature only);stream(): constructStreamwhich asynchronously generates events right in scheduled time (asyncfeature only).
Example with upcoming
use Utc;
use ;
Example with iter
use Utc;
use ;
Example with stream
use Local;
use ;
use StreamExt;
async
Feature flags
serde: addsSerializeandDeserializetrait implementation forSchedule.tz: enables support of cron schedules with timezone.async: adds several methods to use in async environments. See documentation of the module for details.
TODO
- Descriptive example.
- More unit tests for edge cases.
License
This project is licensed under the MIT license.