#time #date #calendar #chrono #sequence

kronos

A tool to calculate complex time expressions

10 releases

Uses new Rust 2021

0.1.5 Mar 11, 2022
0.1.4 Sep 17, 2020
0.1.3 Jul 1, 2020
0.1.2 May 3, 2019
0.0.1 Sep 11, 2016

#33 in Date and time

Download history 30/week @ 2022-01-25 11/week @ 2022-02-01 5/week @ 2022-02-08 11/week @ 2022-02-15 13/week @ 2022-02-22 16/week @ 2022-03-01 25/week @ 2022-03-08 48/week @ 2022-03-15 45/week @ 2022-03-22 6/week @ 2022-03-29 13/week @ 2022-04-05 20/week @ 2022-04-12 10/week @ 2022-04-19 75/week @ 2022-04-26 47/week @ 2022-05-03 28/week @ 2022-05-10

161 downloads per month
Used in fluxcap

MIT license

345KB
1.5K SLoC

Documentation

Kronos is a tool for calculating date/times. It is meant to give a concrete date for questions like "When is the 2nd Monday of June?", or "What was the 3rd to last day-of-the-week of past February?".

To answer these questions kronos composes TimeSequence iterators. These are infinite sequences into the past and the future which you can pin to a particular instant and get resulting time Ranges.

Example

Lets first define a TimeSequence that represents any and all Mondays. Then use it to get an iterator of all future Mondays from a specific t0 instant onward.

// Reference time: Tuesday, 5th Feb 2019
let t0 = chrono::NaiveDate::from_ymd(2019, 2, 5)
  .and_hms(0, 0, 0);

// A sequence for *Mondays*
let mondays = kronos::Weekday(1);

// First Monday after t0 reference time
mondays.future(&t0).next()

Ranges

The previous example would return a Range which represents an open-ended time interval [start, end). Ranges also have a Grain that specifies the resolution of the start and end instants.

Examples of a Range could be Aug 26th 2018.

  • It has a resolution of Grain::Day,
  • starts on 2018/08/26 00:00:00,
  • ends on 2018/08/27 00:00:00

Composing TimeSequences

Basic sequences

Some simple TimeSequences can be built almost out of thin air. For example:

  • Weekday(2) a sequence for Tuesdays.
  • Month(6) a sequence for all June months.
  • Grains(Grain::Day) a sequence to iterate over days.

Composite sequences

More comples TimeSequences can be created by combining other sequences. For example:

  • NthOf(2, Weekday(1), Month(6)) creates a sequence for "the second Mondays of June".
  • LastOf(1, Weekend, Grains(Grain::Year)) for "last weekend of the year".
  • Intersect(Weekday(1), NthOf(28, Grains(Grain::Day), Grains(Grains::Month))) for "all Monday 28th".

Other compositions allow unions, intersections, intervals, exceptions, etc. Please check each module's tests for examples on how to use them.

References

Dependencies

~1MB
~13K SLoC