time_core/util.rs
1//! Utility functions.
2
3use crate::hint;
4
5/// Returns if the provided year is a leap year in the proleptic Gregorian calendar. Uses
6/// [astronomical year numbering](https://en.wikipedia.org/wiki/Astronomical_year_numbering).
7///
8/// ```rust
9/// # use time::util::is_leap_year;
10/// assert!(!is_leap_year(1900));
11/// assert!(is_leap_year(2000));
12/// assert!(is_leap_year(2004));
13/// assert!(!is_leap_year(2005));
14/// assert!(!is_leap_year(2100));
15/// ```
16#[inline]
17pub const fn is_leap_year(year: i32) -> bool {
18 let d = if year % 100 == 0 { 15 } else { 3 };
19 year & d == 0
20}
21
22/// Get the number of calendar days in a given year.
23///
24/// The returned value will always be either 365 or 366.
25///
26/// ```rust
27/// # use time::util::days_in_year;
28/// assert_eq!(days_in_year(1900), 365);
29/// assert_eq!(days_in_year(2000), 366);
30/// assert_eq!(days_in_year(2004), 366);
31/// assert_eq!(days_in_year(2005), 365);
32/// assert_eq!(days_in_year(2100), 365);
33/// ```
34#[inline]
35pub const fn days_in_year(year: i32) -> u16 {
36 if is_leap_year(year) {
37 366
38 } else {
39 365
40 }
41}
42
43/// Get the number of weeks in the ISO year.
44///
45/// The returned value will always be either 52 or 53.
46///
47/// ```rust
48/// # use time::util::weeks_in_year;
49/// assert_eq!(weeks_in_year(2019), 52);
50/// assert_eq!(weeks_in_year(2020), 53);
51/// ```
52#[inline]
53pub const fn weeks_in_year(year: i32) -> u8 {
54 match year % 400 {
55 -396 | -391 | -385 | -380 | -374 | -368 | -363 | -357 | -352 | -346 | -340 | -335
56 | -329 | -324 | -318 | -312 | -307 | -301 | -295 | -289 | -284 | -278 | -272 | -267
57 | -261 | -256 | -250 | -244 | -239 | -233 | -228 | -222 | -216 | -211 | -205 | -199
58 | -193 | -188 | -182 | -176 | -171 | -165 | -160 | -154 | -148 | -143 | -137 | -132
59 | -126 | -120 | -115 | -109 | -104 | -97 | -92 | -86 | -80 | -75 | -69 | -64 | -58
60 | -52 | -47 | -41 | -36 | -30 | -24 | -19 | -13 | -8 | -2 | 4 | 9 | 15 | 20 | 26 | 32
61 | 37 | 43 | 48 | 54 | 60 | 65 | 71 | 76 | 82 | 88 | 93 | 99 | 105 | 111 | 116 | 122
62 | 128 | 133 | 139 | 144 | 150 | 156 | 161 | 167 | 172 | 178 | 184 | 189 | 195 | 201
63 | 207 | 212 | 218 | 224 | 229 | 235 | 240 | 246 | 252 | 257 | 263 | 268 | 274 | 280
64 | 285 | 291 | 296 | 303 | 308 | 314 | 320 | 325 | 331 | 336 | 342 | 348 | 353 | 359
65 | 364 | 370 | 376 | 381 | 387 | 392 | 398 => 53,
66 _ => 52,
67 }
68}
69
70/// Get the number of days in the month of a given year.
71///
72/// ```rust
73/// # use time_core::util::days_in_month;
74/// assert_eq!(days_in_month(2, 2020), 29);
75/// ```
76///
77/// Note: This function is not exposed by the `time` crate. It is an implementation detail.
78#[inline]
79pub const fn days_in_month(month: u8, year: i32) -> u8 {
80 debug_assert!(month >= 1);
81 debug_assert!(month <= 12);
82
83 if hint::unlikely(month == 2) {
84 if is_leap_year(year) {
85 29
86 } else {
87 28
88 }
89 } else {
90 30 | month ^ (month >> 3)
91 }
92}