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