Coverage Report

Created: 2026-03-19 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/time-0.3.47/src/month.rs
Line
Count
Source
1
//! The `Month` enum and its associated `impl`s.
2
3
use core::fmt;
4
use core::num::NonZero;
5
use core::str::FromStr;
6
7
use powerfmt::smart_display::{FormatterOptions, Metadata, SmartDisplay};
8
9
use self::Month::*;
10
use crate::{error, util};
11
12
/// Months of the year.
13
#[repr(u8)]
14
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15
pub enum Month {
16
    #[expect(missing_docs)]
17
    January = 1,
18
    #[expect(missing_docs)]
19
    February = 2,
20
    #[expect(missing_docs)]
21
    March = 3,
22
    #[expect(missing_docs)]
23
    April = 4,
24
    #[expect(missing_docs)]
25
    May = 5,
26
    #[expect(missing_docs)]
27
    June = 6,
28
    #[expect(missing_docs)]
29
    July = 7,
30
    #[expect(missing_docs)]
31
    August = 8,
32
    #[expect(missing_docs)]
33
    September = 9,
34
    #[expect(missing_docs)]
35
    October = 10,
36
    #[expect(missing_docs)]
37
    November = 11,
38
    #[expect(missing_docs)]
39
    December = 12,
40
}
41
42
impl Month {
43
    /// Create a `Month` from its numerical value.
44
    #[inline]
45
0
    pub(crate) const fn from_number(n: NonZero<u8>) -> Result<Self, error::ComponentRange> {
46
0
        match n.get() {
47
0
            1 => Ok(January),
48
0
            2 => Ok(February),
49
0
            3 => Ok(March),
50
0
            4 => Ok(April),
51
0
            5 => Ok(May),
52
0
            6 => Ok(June),
53
0
            7 => Ok(July),
54
0
            8 => Ok(August),
55
0
            9 => Ok(September),
56
0
            10 => Ok(October),
57
0
            11 => Ok(November),
58
0
            12 => Ok(December),
59
0
            _ => Err(error::ComponentRange::unconditional("month")),
60
        }
61
0
    }
Unexecuted instantiation: <time::month::Month>::from_number
Unexecuted instantiation: <time::month::Month>::from_number
62
63
    /// Get the number of days in the month of a given year.
64
    ///
65
    /// ```rust
66
    /// # use time::Month;
67
    /// assert_eq!(Month::February.length(2020), 29);
68
    /// ```
69
    #[inline]
70
0
    pub const fn length(self, year: i32) -> u8 {
71
0
        util::days_in_month(self, year)
72
0
    }
73
74
    /// Get the previous month.
75
    ///
76
    /// ```rust
77
    /// # use time::Month;
78
    /// assert_eq!(Month::January.previous(), Month::December);
79
    /// ```
80
    #[inline]
81
0
    pub const fn previous(self) -> Self {
82
0
        match self {
83
0
            January => December,
84
0
            February => January,
85
0
            March => February,
86
0
            April => March,
87
0
            May => April,
88
0
            June => May,
89
0
            July => June,
90
0
            August => July,
91
0
            September => August,
92
0
            October => September,
93
0
            November => October,
94
0
            December => November,
95
        }
96
0
    }
97
98
    /// Get the next month.
99
    ///
100
    /// ```rust
101
    /// # use time::Month;
102
    /// assert_eq!(Month::January.next(), Month::February);
103
    /// ```
104
    #[inline]
105
0
    pub const fn next(self) -> Self {
106
0
        match self {
107
0
            January => February,
108
0
            February => March,
109
0
            March => April,
110
0
            April => May,
111
0
            May => June,
112
0
            June => July,
113
0
            July => August,
114
0
            August => September,
115
0
            September => October,
116
0
            October => November,
117
0
            November => December,
118
0
            December => January,
119
        }
120
0
    }
121
122
    /// Get n-th next month.
123
    ///
124
    /// ```rust
125
    /// # use time::Month;
126
    /// assert_eq!(Month::January.nth_next(4), Month::May);
127
    /// assert_eq!(Month::July.nth_next(9), Month::April);
128
    /// ```
129
    #[inline]
130
0
    pub const fn nth_next(self, n: u8) -> Self {
131
0
        match (self as u8 - 1 + n % 12) % 12 {
132
0
            0 => January,
133
0
            1 => February,
134
0
            2 => March,
135
0
            3 => April,
136
0
            4 => May,
137
0
            5 => June,
138
0
            6 => July,
139
0
            7 => August,
140
0
            8 => September,
141
0
            9 => October,
142
0
            10 => November,
143
0
            val => {
144
0
                debug_assert!(val == 11);
145
0
                December
146
            }
147
        }
148
0
    }
149
150
    /// Get n-th previous month.
151
    ///
152
    /// ```rust
153
    /// # use time::Month;
154
    /// assert_eq!(Month::January.nth_prev(4), Month::September);
155
    /// assert_eq!(Month::July.nth_prev(9), Month::October);
156
    /// ```
157
    #[inline]
158
0
    pub const fn nth_prev(self, n: u8) -> Self {
159
0
        match self as i8 - 1 - (n % 12).cast_signed() {
160
0
            1 | -11 => February,
161
0
            2 | -10 => March,
162
0
            3 | -9 => April,
163
0
            4 | -8 => May,
164
0
            5 | -7 => June,
165
0
            6 | -6 => July,
166
0
            7 | -5 => August,
167
0
            8 | -4 => September,
168
0
            9 | -3 => October,
169
0
            10 | -2 => November,
170
0
            11 | -1 => December,
171
0
            val => {
172
0
                debug_assert!(val == 0);
173
0
                January
174
            }
175
        }
176
0
    }
177
}
178
179
mod private {
180
    /// Metadata for `Month`.
181
    #[non_exhaustive]
182
    #[derive(Debug, Clone, Copy)]
183
    pub struct MonthMetadata;
184
}
185
use private::MonthMetadata;
186
187
impl SmartDisplay for Month {
188
    type Metadata = MonthMetadata;
189
190
    #[inline]
191
0
    fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> {
192
0
        match self {
193
0
            January => Metadata::new(7, self, MonthMetadata),
194
0
            February => Metadata::new(8, self, MonthMetadata),
195
0
            March => Metadata::new(5, self, MonthMetadata),
196
0
            April => Metadata::new(5, self, MonthMetadata),
197
0
            May => Metadata::new(3, self, MonthMetadata),
198
0
            June => Metadata::new(4, self, MonthMetadata),
199
0
            July => Metadata::new(4, self, MonthMetadata),
200
0
            August => Metadata::new(6, self, MonthMetadata),
201
0
            September => Metadata::new(9, self, MonthMetadata),
202
0
            October => Metadata::new(7, self, MonthMetadata),
203
0
            November => Metadata::new(8, self, MonthMetadata),
204
0
            December => Metadata::new(8, self, MonthMetadata),
205
        }
206
0
    }
207
208
    #[inline]
209
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210
0
        f.pad(match self {
211
0
            January => "January",
212
0
            February => "February",
213
0
            March => "March",
214
0
            April => "April",
215
0
            May => "May",
216
0
            June => "June",
217
0
            July => "July",
218
0
            August => "August",
219
0
            September => "September",
220
0
            October => "October",
221
0
            November => "November",
222
0
            December => "December",
223
        })
224
0
    }
225
}
226
227
impl fmt::Display for Month {
228
    #[inline]
229
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230
0
        SmartDisplay::fmt(self, f)
231
0
    }
232
}
233
234
impl FromStr for Month {
235
    type Err = error::InvalidVariant;
236
237
    #[inline]
238
0
    fn from_str(s: &str) -> Result<Self, Self::Err> {
239
0
        match s {
240
0
            "January" => Ok(January),
241
0
            "February" => Ok(February),
242
0
            "March" => Ok(March),
243
0
            "April" => Ok(April),
244
0
            "May" => Ok(May),
245
0
            "June" => Ok(June),
246
0
            "July" => Ok(July),
247
0
            "August" => Ok(August),
248
0
            "September" => Ok(September),
249
0
            "October" => Ok(October),
250
0
            "November" => Ok(November),
251
0
            "December" => Ok(December),
252
0
            _ => Err(error::InvalidVariant),
253
        }
254
0
    }
255
}
256
257
impl From<Month> for u8 {
258
    #[inline]
259
0
    fn from(month: Month) -> Self {
260
0
        month as Self
261
0
    }
Unexecuted instantiation: <u8 as core::convert::From<time::month::Month>>::from
Unexecuted instantiation: <u8 as core::convert::From<time::month::Month>>::from
262
}
263
264
impl TryFrom<u8> for Month {
265
    type Error = error::ComponentRange;
266
267
    #[inline]
268
0
    fn try_from(value: u8) -> Result<Self, Self::Error> {
269
0
        match NonZero::new(value) {
270
0
            Some(value) => Self::from_number(value),
271
0
            None => Err(error::ComponentRange::unconditional("month")),
272
        }
273
0
    }
274
}