| 1 |
# [ChronoUtil][docsrs]: powerful extensions to Rust's [Chrono](https://github.com/chronotope/chrono) crate. |
| 2 |
|
| 3 |
[![ChronoUtil GitHub Actions][gh-image]][gh-checks] |
| 4 |
[![ChronoUtil on crates.io][cratesio-image]][cratesio] |
| 5 |
[![ChronoUtil on docs.rs][docsrs-image]][docsrs] |
| 6 |
[](https://opensource.org/licenses/MIT) |
| 7 |
|
| 8 |
[gh-image]: https://github.com/olliemath/chronoutil/workflows/test/badge.svg |
| 9 |
[gh-checks]: https://github.com/olliemath/chronoutil/actions?query=workflow%3Atest |
| 10 |
[cratesio-image]: https://img.shields.io/crates/v/chronoutil.svg |
| 11 |
[cratesio]: https://crates.io/crates/chronoutil |
| 12 |
[docsrs-image]: https://docs.rs/chronoutil/badge.svg |
| 13 |
[docsrs]: https://docs.rs/chronoutil |
| 14 |
|
| 15 |
ChronoUtil provides the following utilities: |
| 16 |
|
| 17 |
- `RelativeDuration`: extending Chrono's `Duration` to add months and years |
| 18 |
- `DateRule`s: useful iterators yielding regular (e.g. monthly) dates |
| 19 |
- Procedural helper functions for shifting datelike values by months and years |
| 20 |
|
| 21 |
It is heavily inspired by Python's [dateutil](https://github.com/dateutil/dateutil) |
| 22 |
and provides a similar API, but with less of the niche functionality. |
| 23 |
|
| 24 |
## Usage |
| 25 |
|
| 26 |
Put this in your `Cargo.toml`: |
| 27 |
|
| 28 |
```toml |
| 29 |
[dependencies] |
| 30 |
chronoutil = "0.2.7" |
| 31 |
``` |
| 32 |
|
| 33 |
## Overview |
| 34 |
|
| 35 |
### RelativeDuration |
| 36 |
|
| 37 |
ChronoUtils uses a [**`RelativeDuration`**](https://docs.rs/chronoutil/0.2.7/chronoutil/relative_duration/struct.RelativeDuration.html) type to represent the magnitude of a time span |
| 38 |
which may not be absolute (i.e. which is not simply a fixed number of nanoseconds). |
| 39 |
A relative duration is made up of a number of months together with an absolute [`Duration`]() |
| 40 |
component. |
| 41 |
|
| 42 |
```rust |
| 43 |
let one_day = RelativeDuration::days(1); |
| 44 |
let one_month = RelativeDuration::months(1); |
| 45 |
let delta = one_month + one_day; |
| 46 |
let start = NaiveDate::from_ymd_opt(2020, 1, 1).unwrap(); |
| 47 |
assert_eq!(start + delta, NaiveDate::from_ymd_opt(2020, 2, 2).unwrap()); |
| 48 |
``` |
| 49 |
|
| 50 |
The behaviour of `RelativeDuration` is consistent and well-defined in edge-cases |
| 51 |
(see the Design Decisions section for an explanation): |
| 52 |
|
| 53 |
```rust |
| 54 |
let one_day = RelativeDuration::days(1); |
| 55 |
let one_month = RelativeDuration::months(1); |
| 56 |
let delta = one_month + one_day; |
| 57 |
let start = NaiveDate::from_ymd_opt(2020, 1, 30).unwrap(); |
| 58 |
assert_eq!(start + delta, NaiveDate::from_ymd_opt(2020, 3, 1).unwrap()); |
| 59 |
``` |
| 60 |
|
| 61 |
Relative durations also support parsing a subset of the ISO8601 spec for durations. For example: |
| 62 |
|
| 63 |
```rust |
| 64 |
let payload = String::from("P1Y2M-3DT1H2M3.4S"); |
| 65 |
let parsed = RelativeDuration::parse_from_iso8601(&payload).unwrap(); |
| 66 |
assert_eq!( |
| 67 |
parsed, |
| 68 |
RelativeDuration::years(1) |
| 69 |
+ RelativeDuration::months(2) |
| 70 |
+ RelativeDuration::days(-3) |
| 71 |
+ RelativeDuration::hours(1) |
| 72 |
+ RelativeDuration::minutes(2) |
| 73 |
+ RelativeDuration::seconds(3) |
| 74 |
+ RelativeDuration::nanoseconds(400_000_000) |
| 75 |
) |
| 76 |
assert_eq!(parsed.format_to_iso8601().unwrap(), payload) |
| 77 |
``` |
| 78 |
|
| 79 |
Specifically, we require that all fields except the seconds be integers. |
| 80 |
|
| 81 |
### DateRule |
| 82 |
|
| 83 |
ChronoUtil provides a |
| 84 |
[**`DateRule`**](https://docs.rs/chronoutil/0.2.7/chronoutil/rule/struct.DateRule.html) |
| 85 |
iterator to reliably generate a collection of dates at regular intervals. |
| 86 |
For example, the following will yield one `NaiveDate` on the last day of each |
| 87 |
month in 2025: |
| 88 |
|
| 89 |
```rust |
| 90 |
let start = NaiveDate::from_ymd_opt(2025, 1, 31).unwrap(); |
| 91 |
let rule = DateRule::monthly(start).with_count(12); |
| 92 |
// 2025-1-31, 2025-2-28, 2025-3-31, 2025-4-30, ... |
| 93 |
``` |
| 94 |
|
| 95 |
### Shift functions |
| 96 |
|
| 97 |
ChronoUtil also exposes useful shift functions which are used internally, namely: |
| 98 |
|
| 99 |
- [**`shift_months`**](https://docs.rs/chronoutil/0.2.7/chronoutil/delta/fn.shift_months.html) to shift a datelike value by a given number of months |
| 100 |
- [**`shift_years`**](https://docs.rs/chronoutil/0.2.7/chronoutil/delta/fn.shift_years.html) to shift a datelike value by a given number of years |
| 101 |
- [**`with_year`**](https://docs.rs/chronoutil/0.2.7/chronoutil/delta/fn.shift_months.html) to shift a datelike value to a given day |
| 102 |
- [**`with_month`**](https://docs.rs/chronoutil/0.2.7/chronoutil/delta/fn.with_month.html) to shift a datelike value to a given month |
| 103 |
- [**`with_year`**](https://docs.rs/chronoutil/0.2.7/chronoutil/delta/fn.with_year.html) to shift a datelike value to a given year |
| 104 |
|
| 105 |
## Design decisions and gotchas |
| 106 |
|
| 107 |
We favour simplicity over complexity: we use only the Gregorian calendar and |
| 108 |
make no changes e.g. for dates before the 1500s. |
| 109 |
|
| 110 |
For days between the 1st and 28th, shifting by months has an obvious |
| 111 |
unambiguous meaning which we always stick to. One month after Jan 28th is |
| 112 |
always Feb 28th. Shifting Feb 28th by another month will give Mar 28th. |
| 113 |
|
| 114 |
When shifting a day that has no equivalent in another month (e.g. asking |
| 115 |
for one month after Jan 30th), we first compute the target month, and then if |
| 116 |
the corresponding day does not exist in that month, we take the final day of the |
| 117 |
month as the result. So, on a leap year, one month after Jan 30th is Feb 29th. |
| 118 |
|
| 119 |
The order of precidence for a `RelativeDuration` is as follows: |
| 120 |
|
| 121 |
1. Work out the target month, if shifting by months |
| 122 |
2. If the initial day does not exist in that month, take the final day of the month |
| 123 |
3. Execute any further `Duration` shifts |
| 124 |
|
| 125 |
So a `RelativeDuration` of 1 month and 1 day applied to Jan 31st first shifts to the |
| 126 |
last day of Feb, and then adds a single day, giving the 1st of Mar. Applying to Jan 30th |
| 127 |
gives the same result. |
| 128 |
|
| 129 |
Shifted dates have no _memory_ of the date they were shifted from. Thus if we shift |
| 130 |
Jan 31st by one month and obtain Feb 28th, a further shift of one month will be Mar 28th, |
| 131 |
_not_ Mar 31st. |
| 132 |
|
| 133 |
This leads us to an interesting point about the `RelativeDuration`: addition is not |
| 134 |
_[associative](https://en.wikipedia.org/wiki/Associative_property)_: |
| 135 |
|
| 136 |
```rust |
| 137 |
let start = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap(); |
| 138 |
let delta = RelativeDuration::months(1); |
| 139 |
|
| 140 |
let d1 = (start + delta) + delta; |
| 141 |
let d2 = start + (delta + delta); |
| 142 |
|
| 143 |
assert_eq!(d1, NaiveDate::from_ymd_opt(2020, 3, 29).unwrap()); |
| 144 |
assert_eq!(d2, NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()); |
| 145 |
``` |
| 146 |
|
| 147 |
If you want a series of shifted dates, we advise using the `DateRule`, which takes |
| 148 |
account of some of these subtleties: |
| 149 |
```rust |
| 150 |
let start = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap(); |
| 151 |
let delta = RelativeDuration::months(1); |
| 152 |
let mut rule = DateRule::new(start, delta); |
| 153 |
assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd_opt(2020, 1, 31).unwrap()); |
| 154 |
assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()); |
| 155 |
assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()); |
| 156 |
``` |
| 157 |
|
| 158 |
## Using custom Datelike types |
| 159 |
|
| 160 |
If you have your own custom type which implements chrono's |
| 161 |
[Datelike](https://docs.rs/chrono/0.4.19/chrono/trait.Datelike.html) trait, |
| 162 |
then you can already use all of the shift functions (`shift_months`, `shift_year`). |
| 163 |
|
| 164 |
Using relative duration for your type will involve some simple boilerplate. |
| 165 |
Assuming that your custom date type `MyAwesomeUnicornDate` already has an |
| 166 |
implementation of `Add` for chrono's `Duration`, this would look like: |
| 167 |
```rust |
| 168 |
impl Add<RelativeDuration> for MyAwesomeUnicornDate { |
| 169 |
type Output = MyAwesomeUnicornDate; |
| 170 |
|
| 171 |
#[inline] |
| 172 |
fn add(self, rhs: RelativeDuration) -> MyAwesomeUnicornDate { |
| 173 |
shift_months(self, rhs.months) + rhs.duration |
| 174 |
} |
| 175 |
} |
| 176 |
``` |