Coverage Report

Created: 2025-10-29 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/time-0.3.14/src/util.rs
Line
Count
Source
1
//! Utility functions.
2
3
use crate::Month;
4
5
/// Whether to adjust the date, and in which direction. Useful when implementing arithmetic.
6
pub(crate) enum DateAdjustment {
7
    /// The previous day should be used.
8
    Previous,
9
    /// The next day should be used.
10
    Next,
11
    /// The date should be used as-is.
12
    None,
13
}
14
15
/// Get the number of days in the month of a given year.
16
///
17
/// ```rust
18
/// # use time::{Month, util};
19
/// assert_eq!(util::days_in_year_month(2020, Month::February), 29);
20
/// ```
21
0
pub const fn days_in_year_month(year: i32, month: Month) -> u8 {
22
    use Month::*;
23
0
    match month {
24
0
        January | March | May | July | August | October | December => 31,
25
0
        April | June | September | November => 30,
26
0
        February if is_leap_year(year) => 29,
27
0
        February => 28,
28
    }
29
0
}
30
31
/// Returns if the provided year is a leap year in the proleptic Gregorian calendar. Uses
32
/// [astronomical year numbering](https://en.wikipedia.org/wiki/Astronomical_year_numbering).
33
///
34
/// ```rust
35
/// # use time::util::is_leap_year;
36
/// assert!(!is_leap_year(1900));
37
/// assert!(is_leap_year(2000));
38
/// assert!(is_leap_year(2004));
39
/// assert!(!is_leap_year(2005));
40
/// assert!(!is_leap_year(2100));
41
/// ```
42
0
pub const fn is_leap_year(year: i32) -> bool {
43
0
    year % 4 == 0 && (year % 25 != 0 || year % 16 == 0)
44
0
}
45
46
/// Get the number of calendar days in a given year.
47
///
48
/// The returned value will always be either 365 or 366.
49
///
50
/// ```rust
51
/// # use time::util::days_in_year;
52
/// assert_eq!(days_in_year(1900), 365);
53
/// assert_eq!(days_in_year(2000), 366);
54
/// assert_eq!(days_in_year(2004), 366);
55
/// assert_eq!(days_in_year(2005), 365);
56
/// assert_eq!(days_in_year(2100), 365);
57
/// ```
58
0
pub const fn days_in_year(year: i32) -> u16 {
59
0
    if is_leap_year(year) { 366 } else { 365 }
60
0
}
61
62
/// Get the number of weeks in the ISO year.
63
///
64
/// The returned value will always be either 52 or 53.
65
///
66
/// ```rust
67
/// # use time::util::weeks_in_year;
68
/// assert_eq!(weeks_in_year(2019), 52);
69
/// assert_eq!(weeks_in_year(2020), 53);
70
/// ```
71
0
pub const fn weeks_in_year(year: i32) -> u8 {
72
0
    match year.rem_euclid(400) {
73
        4 | 9 | 15 | 20 | 26 | 32 | 37 | 43 | 48 | 54 | 60 | 65 | 71 | 76 | 82 | 88 | 93 | 99
74
        | 105 | 111 | 116 | 122 | 128 | 133 | 139 | 144 | 150 | 156 | 161 | 167 | 172 | 178
75
        | 184 | 189 | 195 | 201 | 207 | 212 | 218 | 224 | 229 | 235 | 240 | 246 | 252 | 257
76
        | 263 | 268 | 274 | 280 | 285 | 291 | 296 | 303 | 308 | 314 | 320 | 325 | 331 | 336
77
0
        | 342 | 348 | 353 | 359 | 364 | 370 | 376 | 381 | 387 | 392 | 398 => 53,
78
0
        _ => 52,
79
    }
80
0
}