jiff_core/civil/mod.rs
1/*!
2Primitives for civil dates and times.
3*/
4pub use self::{
5 date::{Date, ISOWeekDate, UnixEpochDay},
6 datetime::DateTime,
7 time::{Time, TimeNanosecond, TimeSecond},
8 weekday::{Weekday, WeekdaysForward, WeekdaysReverse},
9};
10use crate::macros::unwrapr;
11
12mod date;
13mod datetime;
14mod time;
15mod 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]
60pub 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]
115pub 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]
155pub 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]
171pub 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]
181pub const fn days_in_year(year: i16) -> i16 {
182 if is_leap_year(year) { 366 } else { 365 }
183}
184
185/// Return the number of days in the given month.
186///
187/// This is guaranteed to return a value in the range `1..=31`.
188#[inline]
189pub const fn days_in_month(year: i16, month: i8) -> i8 {
190 // From: https://github.com/BurntSushi/jiff/pull/23
191 if month == 2 {
192 if is_leap_year(year) { 29 } else { 28 }
193 } else {
194 30 | (month ^ month >> 3)
195 }
196}
197
198/// Returns true if the given ISO 8601 week date year is a "long" year or not.
199///
200/// A "long" year is a year with 53 weeks. Otherwise, it's a "short" year
201/// with 52 weeks.
202#[inline]
203pub const fn is_long_iso_week_year(year: i16) -> bool {
204 // Inspired by: https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year
205 let last =
206 unwrapr!(Date::new(year, 12, 31), "last day of year is always valid");
207 let weekday = last.weekday();
208 matches!(weekday, Weekday::Thursday)
209 || (last.in_leap_year() && matches!(weekday, Weekday::Friday))
210}
211
212/// Returns the total number of weeks in the year of this ISO 8601 week
213/// date.
214///
215/// It is guaranteed that the value returned is either 52 or 53. The
216/// latter case occurs precisely when [`ISOWeekDate::in_long_year`]
217/// returns `true`.
218#[inline]
219pub const fn weeks_in_iso_week_year(year: i16) -> i8 {
220 if is_long_iso_week_year(year) { 53 } else { 52 }
221}
222
223#[cfg(test)]
224mod tests {
225
226 use super::*;
227
228 static LEAPS: &[i16] = &[-400, -104, -4, 0, 1904, 2000, 2004, 2024];
229 static NOT_LEAPS: &[i16] = &[-401, -100, -1, 1900, 1999, 2001, 2002, 2003];
230
231 #[test]
232 fn year_leap() {
233 for &y in LEAPS {
234 assert!(is_leap_year(y), "{y} should be a leap year");
235 }
236 for &y in NOT_LEAPS {
237 assert!(!is_leap_year(y), "{y} should NOT be a leap year");
238 }
239 }
240
241 #[test]
242 fn year_days() {
243 for &y in LEAPS {
244 assert_eq!(days_in_year(y), 366, "{y} should be a leap year");
245 }
246 for &y in NOT_LEAPS {
247 assert_eq!(days_in_year(y), 365, "{y} should NOT be a leap year");
248 }
249 }
250
251 #[test]
252 fn month_days() {
253 assert_eq!(days_in_month(2001, 1), 31);
254 assert_eq!(days_in_month(2001, 2), 28);
255 assert_eq!(days_in_month(2001, 3), 31);
256 assert_eq!(days_in_month(2001, 4), 30);
257 assert_eq!(days_in_month(2001, 5), 31);
258 assert_eq!(days_in_month(2001, 6), 30);
259 assert_eq!(days_in_month(2001, 7), 31);
260 assert_eq!(days_in_month(2001, 8), 31);
261 assert_eq!(days_in_month(2001, 9), 30);
262 assert_eq!(days_in_month(2001, 10), 31);
263 assert_eq!(days_in_month(2001, 11), 30);
264 assert_eq!(days_in_month(2001, 12), 31);
265
266 for &y in LEAPS {
267 assert_eq!(days_in_month(y, 2), 29, "{y} should be a leap year");
268 }
269 for &y in NOT_LEAPS {
270 assert_eq!(
271 days_in_month(y, 2),
272 28,
273 "{y} should NOT be a leap year"
274 );
275 }
276 }
277}