Debian Codegraph @

src/lib.rs

raw
1 #![warn(missing_docs)]
2 //! # Chronoutil provides powerful extensions to rust's Chrono crate
3 //!
4 //! ChronoUtil provides the following utilities:
5 //! - `RelativeDuration`: extending Chrono's `Duration` to add months and years
6 //! - `DateRule`: useful iterators yielding regular (e.g. monthly) dates
7 //! - Procedural helper functions for shifting datelike values by months and years
8 //!
9 //! It is heavily inspired by Python's [dateutil](https://github.com/dateutil/dateutil)
10 //! and provides a similar API, but with less of the niche functionality.
11 //! ## Overview
12 //!
13 //! ### RelativeDuration
14 //!
15 //! ChronoUtils uses a [RelativeDuration] type to represent the magnitude of a time span
16 //! which may not be absolute (i.e. which is not simply a fixed number of nanoseconds).
17 //! A relative duration is made up of a number of months together with an absolute duration
18 //! component.
19 //!
20 //! ```rust
21 //! # use chrono::{NaiveDate};
22 //! # use chronoutil::{RelativeDuration};
23 //! let one_day = RelativeDuration::days(1);
24 //! let one_month = RelativeDuration::months(1);
25 //! let delta = one_month + one_day;
26 //! let start = NaiveDate::from_ymd_opt(2020, 1, 1).unwrap();
27 //! assert_eq!(start + delta, NaiveDate::from_ymd_opt(2020, 2, 2).unwrap());
28 //! ```
29 //!
30 //! The behaviour of `RelativeDuration` is consistent and well-defined in edge-cases
31 //! (see the Design Decisions section for an explanation):
32 //!
33 //! ```rust
34 //! # use chrono::{NaiveDate};
35 //! # use chronoutil::{RelativeDuration};
36 //! let one_day = RelativeDuration::days(1);
37 //! let one_month = RelativeDuration::months(1);
38 //! let delta = one_month + one_day;
39 //! let start = NaiveDate::from_ymd_opt(2020, 1, 30).unwrap();
40 //! assert_eq!(start + delta, NaiveDate::from_ymd_opt(2020, 3, 1).unwrap());
41 //! ```
42 //!
43 //! ### DateRule
44 //!
45 //! ChronoUtil provides a
46 //! [DateRule]
47 //! iterator to reliably generate a collection of dates at regular intervals.
48 //! For example, the following will yield one `NaiveDate` on the last day of each
49 //! month in 2025:
50 //!
51 //! ```rust
52 //! # use chrono::NaiveDate;
53 //! # use chronoutil::DateRule;
54 //! let start = NaiveDate::from_ymd_opt(2025, 1, 31).unwrap();
55 //! let rule = DateRule::monthly(start).with_count(12);
56 //! // 2025-1-31, 2025-2-28, 2025-3-31, 2025-4-30, ...
57 //! ```
58 //!
59 //! ### Shift functions
60 //!
61 //! ChronoUtil also exposes useful shift functions which are used internally, namely:
62 //!
63 //! - [shift_months] to shift a datelike value by a given number of months
64 //! - [shift_years] to shift a datelike value by a given number of years
65 //! - [with_day] to shift a datelike value to a given day
66 //! - [with_month] to shift a datelike value to a given month
67 //! - [with_year] to shift a datelike value to a given year
68 //!
69 //! ## Design decisions and gotchas
70 //!
71 //! We favour simplicity over complexity: we use only the Gregorian calendar and
72 //! make no changes e.g. for dates before the 1500s.
73 //!
74 //! For days between the 1st and 28th, shifting by months has an obvious
75 //! unambiguous meaning which we always stick to. One month after Jan 28th is
76 //! always Feb 28th. Shifting Feb 28th by another month will give Mar 28th.
77 //!
78 //! When shifting a day that has no equivalent in another month (e.g. asking
79 //! for one month after Jan 30th), we first compute the target month, and then if
80 //! the corresponding day does not exist in that month, we take the final day of the
81 //! month as the result. So, on a leap year, one month after Jan 30th is Feb 29th.
82 //!
83 //! The order of precidence for a `RelativeDuration` is as follows:
84 //!
85 //! 1. Work out the target month, if shifting by months
86 //! 2. If the initial day does not exist in that month, take the final day of the month
87 //! 3. Execute any further `Duration` shifts
88 //!
89 //! So a `RelativeDuration` of 1 month and 1 day applied to Jan 31st first shifts to the
90 //! last day of Feb, and then adds a single day, giving the 1st of Mar. Applying to Jan 30th
91 //! gives the same result.
92 //!
93 //! Shifted dates have no _memory_ of the date they were shifted from. Thus if we shift
94 //! Jan 31st by one month and obtain Feb 28th, a further shift of one month will be Mar 28th,
95 //! _not_ Mar 31st.
96 //!
97 //! This leads us to an interesting point about the `RelativeDuration`: addition is not
98 //! _associative_:
99 //!
100 //! ```rust
101 //! # use chrono::NaiveDate;
102 //! # use chronoutil::RelativeDuration;
103 //!
104 //! let d1 = (NaiveDate::from_ymd_opt(2020, 1, 31).unwrap() + RelativeDuration::months(1))
105 //! + RelativeDuration::months(1);
106 //! let d2 = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap()
107 //! + (RelativeDuration::months(1) + RelativeDuration::months(1));
108 //!
109 //! assert_eq!(d1, NaiveDate::from_ymd_opt(2020, 3, 29).unwrap());
110 //! assert_eq!(d2, NaiveDate::from_ymd_opt(2020, 3, 31).unwrap());
111 //! ```
112 //!
113 //! If you want a series of shifted dates, we advise using the `DateRule`, which takes
114 //! account of some of these subtleties:
115 //! ```rust
116 //! # use chrono::NaiveDate;
117 //! # use chronoutil::{RelativeDuration, DateRule};
118 //! let start = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();
119 //! let delta = RelativeDuration::months(1);
120 //! let mut rule = DateRule::new(start, delta);
121 //! assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd_opt(2020, 1, 31).unwrap());
122 //! assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd_opt(2020, 2, 29).unwrap());
123 //! assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd_opt(2020, 3, 31).unwrap());
124 //! ```
125
126 extern crate chrono;
127
128 pub mod delta;
129 pub mod relative_duration;
130 pub mod rule;
131
132 pub use relative_duration::RelativeDuration;
133 pub use rule::DateRule;
134 // Utility functions may be useful for others
135 pub use delta::{is_leap_year, shift_months, shift_years, with_day, with_month, with_year};