Debian Codegraph @

debian/rust-vendor/jiff-core/src/civil/mod.rs

raw
1 /*!
2 Primitives for civil dates and times.
3 */
4 pub use self::{
5 date::{Date, ISOWeekDate, UnixEpochDay},
6 datetime::DateTime,
7 time::{Time, TimeNanosecond, TimeSecond},
8 weekday::{Weekday, WeekdaysForward, WeekdaysReverse},
9 };
10 use crate::macros::unwrapr;
11
12 mod date;
13 mod datetime;
14 mod time;
15 mod weekday;
16
17 /// Creates a new `DateTime` value in a `const` context.
18 ///
19 /// This is a convenience free function for [`DateTime::new`] that panics when
20 /// the given datetime is not valid. It is intended to provide a terse syntax
21 /// for constructing `DateTime` values from parameters that are known to be
22 /// valid.
23 ///
24 /// # Panics
25 ///
26 /// This routine panics when [`DateTime::new`] would return an error. That
27 /// is, when the given components do not correspond to a valid datetime.
28 /// Namely, all of the following must be true:
29 ///
30 /// * The year must be in the range `-9999..=9999`.
31 /// * The month must be in the range `1..=12`.
32 /// * The day must be at least `1` and must be at most the number of days
33 /// in the corresponding month. So for example, `2024-02-29` is valid but
34 /// `2023-02-29` is not.
35 /// * `0 <= hour <= 23`
36 /// * `0 <= minute <= 59`
37 /// * `0 <= second <= 59`
38 /// * `0 <= subsec_nanosecond <= 999,999,999`
39 ///
40 /// Similarly, when used in a const context, invalid parameters will prevent
41 /// your Rust program from compiling.
42 ///
43 /// # Example
44 ///
45 /// ```
46 /// use jiff_core::civil::datetime;
47 ///
48 /// let dt = datetime(2024, 2, 29, 21, 30, 5, 123_456_789);
49 /// assert_eq!(dt.date().year(), 2024);
50 /// assert_eq!(dt.date().month(), 2);
51 /// assert_eq!(dt.date().day(), 29);
52 /// assert_eq!(dt.time().hour(), 21);
53 /// assert_eq!(dt.time().minute(), 30);
54 /// assert_eq!(dt.time().second(), 5);
55 /// assert_eq!(dt.time().millisecond(), 123);
56 /// assert_eq!(dt.time().microsecond(), 456);
57 /// assert_eq!(dt.time().nanosecond(), 789);
58 /// ```
59 #[inline]
60 pub const fn datetime(
61 year: i16,
62 month: i8,
63 day: i8,
64 hour: i8,
65 minute: i8,
66 second: i8,
67 subsec_nanosecond: i32,
68 ) -> DateTime {
69 unwrapr!(
70 DateTime::new(
71 year,
72 month,
73 day,
74 hour,
75 minute,
76 second,
77 subsec_nanosecond,
78 ),
79 "invalid datetime"
80 )
81 }
82
83 /// Creates a new `Date` value in a `const` context.
84 ///
85 /// This is a convenience free function for [`Date::new`] that panics when
86 /// the given date is not valid. It is intended to provide a terse syntax for
87 /// constructing `Date` values from parameters that are known to be valid.
88 ///
89 /// # Panics
90 ///
91 /// This routine panics when [`Date::new`] would return an error. That is,
92 /// when the given year-month-day does not correspond to a valid date.
93 /// Namely, all of the following must be true:
94 ///
95 /// * The year must be in the range `-9999..=9999`.
96 /// * The month must be in the range `1..=12`.
97 /// * The day must be at least `1` and must be at most the number of days
98 /// in the corresponding month. So for example, `2024-02-29` is valid but
99 /// `2023-02-29` is not.
100 ///
101 /// Similarly, when used in a const context, invalid parameters will prevent
102 /// your Rust program from compiling.
103 ///
104 /// # Example
105 ///
106 /// ```
107 /// use jiff_core::civil::date;
108 ///
109 /// let d = date(2024, 2, 29);
110 /// assert_eq!(d.year(), 2024);
111 /// assert_eq!(d.month(), 2);
112 /// assert_eq!(d.day(), 29);
113 /// ```
114 #[inline]
115 pub const fn date(year: i16, month: i8, day: i8) -> Date {
116 unwrapr!(Date::new(year, month, day), "invalid date")
117 }
118
119 /// Creates a new `Time` value in a `const` context.
120 ///
121 /// This is a convenience free function for [`Time::new`] that panics when
122 /// the given time is not valid. It is intended to provide a terse syntax for
123 /// constructing `Time` values from parameters that are known to be valid.
124 ///
125 /// # Panics
126 ///
127 /// This panics if the given values do not correspond to a valid `Time`.
128 /// All of the following conditions must be true:
129 ///
130 /// * `0 <= hour <= 23`
131 /// * `0 <= minute <= 59`
132 /// * `0 <= second <= 59`
133 /// * `0 <= subsec_nanosecond <= 999,999,999`
134 ///
135 /// Similarly, when used in a const context, invalid parameters will
136 /// prevent your Rust program from compiling.
137 ///
138 /// # Example
139 ///
140 /// This shows an example of a valid time in a `const` context:
141 ///
142 /// ```
143 /// use jiff_core::civil::time;
144 ///
145 /// let t = time(21, 30, 5, 123_456_789);
146 /// assert_eq!(t.hour(), 21);
147 /// assert_eq!(t.minute(), 30);
148 /// assert_eq!(t.second(), 5);
149 /// assert_eq!(t.millisecond(), 123);
150 /// assert_eq!(t.microsecond(), 456);
151 /// assert_eq!(t.nanosecond(), 789);
152 /// assert_eq!(t.subsec_nanosecond(), 123_456_789);
153 /// ```
154 #[inline]
155 pub const fn time(
156 hour: i8,
157 minute: i8,
158 second: i8,
159 subsec_nanosecond: i32,
160 ) -> Time {
161 unwrapr!(
162 Time::new(hour, minute, second, subsec_nanosecond),
163 "invalid time"
164 )
165 }
166
167 /// Returns true if and only if the given year is a leap year.
168 ///
169 /// A leap year is a year with 366 days. Non-leap years have 365 days.
170 #[inline]
171 pub const fn is_leap_year(year: i16) -> bool {
172 // From: https://github.com/BurntSushi/jiff/pull/23
173 let d = if year % 25 != 0 { 4 } else { 16 };
174 (year % d) == 0
175 }
176
177 /// Return the number of days in the given year.
178 ///
179 /// This is guaranteed to either return `365` or `366`.
180 #[inline]
181 pub const fn days_in_year(year: i16) -> i16 {
182 if is_leap_year(year) {
183 366
184 } else {
185 365
186 }
187 }
188
189 /// Return the number of days in the given month.
190 ///
191 /// This is guaranteed to return a value in the range `1..=31`.
192 #[inline]
193 pub const fn days_in_month(year: i16, month: i8) -> i8 {
194 // From: https://github.com/BurntSushi/jiff/pull/23
195 if month == 2 {
196 if is_leap_year(year) {
197 29
198 } else {
199 28
200 }
201 } else {
202 30 | (month ^ month >> 3)
203 }
204 }
205
206 /// Returns true if the given ISO 8601 week date year is a "long" year or not.
207 ///
208 /// A "long" year is a year with 53 weeks. Otherwise, it's a "short" year
209 /// with 52 weeks.
210 #[inline]
211 pub const fn is_long_iso_week_year(year: i16) -> bool {
212 // Inspired by: https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year
213 let last =
214 unwrapr!(Date::new(year, 12, 31), "last day of year is always valid");
215 let weekday = last.weekday();
216 matches!(weekday, Weekday::Thursday)
217 || (last.in_leap_year() && matches!(weekday, Weekday::Friday))
218 }
219
220 /// Returns the total number of weeks in the year of this ISO 8601 week
221 /// date.
222 ///
223 /// It is guaranteed that the value returned is either 52 or 53. The
224 /// latter case occurs precisely when [`ISOWeekDate::in_long_year`]
225 /// returns `true`.
226 #[inline]
227 pub const fn weeks_in_iso_week_year(year: i16) -> i8 {
228 if is_long_iso_week_year(year) {
229 53
230 } else {
231 52
232 }
233 }
234
235 #[cfg(test)]
236 mod tests {
237
238 use super::*;
239
240 static LEAPS: &[i16] = &[-400, -104, -4, 0, 1904, 2000, 2004, 2024];
241 static NOT_LEAPS: &[i16] = &[-401, -100, -1, 1900, 1999, 2001, 2002, 2003];
242
243 #[test]
244 fn year_leap() {
245 for &y in LEAPS {
246 assert!(is_leap_year(y), "{y} should be a leap year");
247 }
248 for &y in NOT_LEAPS {
249 assert!(!is_leap_year(y), "{y} should NOT be a leap year");
250 }
251 }
252
253 #[test]
254 fn year_days() {
255 for &y in LEAPS {
256 assert_eq!(days_in_year(y), 366, "{y} should be a leap year");
257 }
258 for &y in NOT_LEAPS {
259 assert_eq!(days_in_year(y), 365, "{y} should NOT be a leap year");
260 }
261 }
262
263 #[test]
264 fn month_days() {
265 assert_eq!(days_in_month(2001, 1), 31);
266 assert_eq!(days_in_month(2001, 2), 28);
267 assert_eq!(days_in_month(2001, 3), 31);
268 assert_eq!(days_in_month(2001, 4), 30);
269 assert_eq!(days_in_month(2001, 5), 31);
270 assert_eq!(days_in_month(2001, 6), 30);
271 assert_eq!(days_in_month(2001, 7), 31);
272 assert_eq!(days_in_month(2001, 8), 31);
273 assert_eq!(days_in_month(2001, 9), 30);
274 assert_eq!(days_in_month(2001, 10), 31);
275 assert_eq!(days_in_month(2001, 11), 30);
276 assert_eq!(days_in_month(2001, 12), 31);
277
278 for &y in LEAPS {
279 assert_eq!(days_in_month(y, 2), 29, "{y} should be a leap year");
280 }
281 for &y in NOT_LEAPS {
282 assert_eq!(
283 days_in_month(y, 2),
284 28,
285 "{y} should NOT be a leap year"
286 );
287 }
288 }
289 }