recurring
A recurring event series implementation based on
jiff
which provides a flexible
alternative to jiff
's builtin series iterators provided by the
DateTime::series
,
Date::series
and
Time::series
methods.
The essential types in this crate are:
Pattern
: a trait implemented by recurrence patterns to yield aDateTime
before, after or close to a given instant.Event
: an event with a start and optional end date.Series
: producesEvent
s following a recurrencePattern
.
Features
- Interval-based and cron-based recurrence patterns
- Support for optional event duration to produce series events with start and end date.
- Querying the
Series
for events. before, after or close to a user-defined datetime. - Finding series events containing a given datetime.
- Composition of events following different recurrence patterns into a single series.
- Splitting series at a cutoff point.
- Iterating over a sub-range of a series.
Examples
Series with interval-based recurrence pattern
Construct a Series
yielding events on a fixed interval.
use ;
use daily;
use date;
// Every two days.
let pattern = daily;
// The pattern is relative to the series start.
let start = date.at;
let series = new;
let events: = series.iter.take.collect;
assert_eq!;
Series with cron-based recurrence pattern
Construct a Series
yielding events according to a cron schedule.
This also showcases the ToSeries
trait which provides the .to_series()
method for builtin jiff
types like Zoned
, DateTime
and Date
.
use ;
use cron;
use date;
// Create a cron pattern which yields events every day at 12:05:10, 12:05:20,
// 16:05:10 and 16:05:20.
let pattern = cron.hours.minute.seconds;
let start = date.at;
// `.to_series()` is provided by the `ToSeries` trait.
let series = start.to_series.unwrap;
let events: = series.iter.take.collect;
assert_eq!;
Querying a series for events
The Series
type has various methods to query for events. It provides methods
to find the event before, after or closest to a given instant and more. The
example below showcases some of these methods.
use ;
use cron;
use ;
// Create a cron pattern which yields events every day at 12:05:00, 12:10:00,
// 18:05:00 and 18:10:00.
let pattern = cron.hours.minutes.second;
// In addition to the series start date, we also specify an end in this
// example. Series bounds can be open or closed on both sides.
let start = date.at;
let end = date.at;
let series = new;
assert_eq!;
assert_eq!;
assert_eq!;
Advanced usage
The following example shows some more advanced features.
A series can yield events from multiple recurrence patterns, e.g. if you have events occurring at a fixed interval with occasional exceptions.
Additionally, the example below also shows how a Series
can also be
configured to return Event
s that have a start and end date by configuring an
event duration.
Finally, we use the .range()
method of Series
to iterate over a sub-range
of the series events.
use ;
use ;
use ;
// We have a cron-based pattern...
let daily_around_lunch = cron.hour.minute.second;
// ...and an interval-based pattern
let every_two_days = daily;
// ...and construct a pattern which combines the two into a single pattern.
let pattern = daily_around_lunch.and;
// The interval-based `every_two_days` pattern is relative to the series start.
let series_start = date.at;
let series = builder
.event_duration // Series events span 1 hour.
.build
.unwrap;
// We iterate over the events starting 2 days after the series start.
let range_start = series_start + 48.hours;
let events: = series.range.take.collect;
assert_eq!;
License
The source code of recurring is licensed under either of Apache License, Version 2.0 or MIT license at your option.