/rust/registry/src/index.crates.io-1949cf8c6b5b557f/time-0.3.44/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 | 36.4k | pub(crate) const fn from_number(n: NonZero<u8>) -> Result<Self, error::ComponentRange> { |
46 | 36.4k | match n.get() { |
47 | 2.96k | 1 => Ok(January), |
48 | 7.34k | 2 => Ok(February), |
49 | 8.18k | 3 => Ok(March), |
50 | 1.05k | 4 => Ok(April), |
51 | 1.25k | 5 => Ok(May), |
52 | 1.13k | 6 => Ok(June), |
53 | 266 | 7 => Ok(July), |
54 | 382 | 8 => Ok(August), |
55 | 830 | 9 => Ok(September), |
56 | 9.99k | 10 => Ok(October), |
57 | 332 | 11 => Ok(November), |
58 | 2.72k | 12 => Ok(December), |
59 | 6 | n => Err(error::ComponentRange { |
60 | 6 | name: "month", |
61 | 6 | minimum: 1, |
62 | 6 | maximum: 12, |
63 | 6 | value: n as i64, |
64 | 6 | conditional_message: None, |
65 | 6 | }), |
66 | | } |
67 | 36.4k | } <time::month::Month>::from_number Line | Count | Source | 45 | 4.32k | pub(crate) const fn from_number(n: NonZero<u8>) -> Result<Self, error::ComponentRange> { | 46 | 4.32k | match n.get() { | 47 | 882 | 1 => Ok(January), | 48 | 252 | 2 => Ok(February), | 49 | 178 | 3 => Ok(March), | 50 | 228 | 4 => Ok(April), | 51 | 402 | 5 => Ok(May), | 52 | 224 | 6 => Ok(June), | 53 | 194 | 7 => Ok(July), | 54 | 184 | 8 => Ok(August), | 55 | 176 | 9 => Ok(September), | 56 | 228 | 10 => Ok(October), | 57 | 230 | 11 => Ok(November), | 58 | 1.14k | 12 => Ok(December), | 59 | 0 | n => Err(error::ComponentRange { | 60 | 0 | name: "month", | 61 | 0 | minimum: 1, | 62 | 0 | maximum: 12, | 63 | 0 | value: n as i64, | 64 | 0 | conditional_message: None, | 65 | 0 | }), | 66 | | } | 67 | 4.32k | } |
<time::month::Month>::from_number Line | Count | Source | 45 | 32.1k | pub(crate) const fn from_number(n: NonZero<u8>) -> Result<Self, error::ComponentRange> { | 46 | 32.1k | match n.get() { | 47 | 2.08k | 1 => Ok(January), | 48 | 7.09k | 2 => Ok(February), | 49 | 8.01k | 3 => Ok(March), | 50 | 824 | 4 => Ok(April), | 51 | 849 | 5 => Ok(May), | 52 | 909 | 6 => Ok(June), | 53 | 72 | 7 => Ok(July), | 54 | 198 | 8 => Ok(August), | 55 | 654 | 9 => Ok(September), | 56 | 9.76k | 10 => Ok(October), | 57 | 102 | 11 => Ok(November), | 58 | 1.58k | 12 => Ok(December), | 59 | 6 | n => Err(error::ComponentRange { | 60 | 6 | name: "month", | 61 | 6 | minimum: 1, | 62 | 6 | maximum: 12, | 63 | 6 | value: n as i64, | 64 | 6 | conditional_message: None, | 65 | 6 | }), | 66 | | } | 67 | 32.1k | } |
|
68 | | |
69 | | /// Get the number of days in the month of a given year. |
70 | | /// |
71 | | /// ```rust |
72 | | /// # use time::Month; |
73 | | /// assert_eq!(Month::February.length(2020), 29); |
74 | | /// ``` |
75 | | #[inline] |
76 | 22.2k | pub const fn length(self, year: i32) -> u8 { |
77 | 22.2k | util::days_in_month(self, year) |
78 | 22.2k | } Unexecuted instantiation: <time::month::Month>::length <time::month::Month>::length Line | Count | Source | 76 | 22.2k | pub const fn length(self, year: i32) -> u8 { | 77 | 22.2k | util::days_in_month(self, year) | 78 | 22.2k | } |
|
79 | | |
80 | | /// Get the previous month. |
81 | | /// |
82 | | /// ```rust |
83 | | /// # use time::Month; |
84 | | /// assert_eq!(Month::January.previous(), Month::December); |
85 | | /// ``` |
86 | | #[inline] |
87 | 0 | pub const fn previous(self) -> Self { |
88 | 0 | match self { |
89 | 0 | January => December, |
90 | 0 | February => January, |
91 | 0 | March => February, |
92 | 0 | April => March, |
93 | 0 | May => April, |
94 | 0 | June => May, |
95 | 0 | July => June, |
96 | 0 | August => July, |
97 | 0 | September => August, |
98 | 0 | October => September, |
99 | 0 | November => October, |
100 | 0 | December => November, |
101 | | } |
102 | 0 | } |
103 | | |
104 | | /// Get the next month. |
105 | | /// |
106 | | /// ```rust |
107 | | /// # use time::Month; |
108 | | /// assert_eq!(Month::January.next(), Month::February); |
109 | | /// ``` |
110 | | #[inline] |
111 | 0 | pub const fn next(self) -> Self { |
112 | 0 | match self { |
113 | 0 | January => February, |
114 | 0 | February => March, |
115 | 0 | March => April, |
116 | 0 | April => May, |
117 | 0 | May => June, |
118 | 0 | June => July, |
119 | 0 | July => August, |
120 | 0 | August => September, |
121 | 0 | September => October, |
122 | 0 | October => November, |
123 | 0 | November => December, |
124 | 0 | December => January, |
125 | | } |
126 | 0 | } |
127 | | |
128 | | /// Get n-th next month. |
129 | | /// |
130 | | /// ```rust |
131 | | /// # use time::Month; |
132 | | /// assert_eq!(Month::January.nth_next(4), Month::May); |
133 | | /// assert_eq!(Month::July.nth_next(9), Month::April); |
134 | | /// ``` |
135 | | #[inline] |
136 | 0 | pub const fn nth_next(self, n: u8) -> Self { |
137 | 0 | match (self as u8 - 1 + n % 12) % 12 { |
138 | 0 | 0 => January, |
139 | 0 | 1 => February, |
140 | 0 | 2 => March, |
141 | 0 | 3 => April, |
142 | 0 | 4 => May, |
143 | 0 | 5 => June, |
144 | 0 | 6 => July, |
145 | 0 | 7 => August, |
146 | 0 | 8 => September, |
147 | 0 | 9 => October, |
148 | 0 | 10 => November, |
149 | 0 | val => { |
150 | 0 | debug_assert!(val == 11); |
151 | 0 | December |
152 | | } |
153 | | } |
154 | 0 | } |
155 | | |
156 | | /// Get n-th previous month. |
157 | | /// |
158 | | /// ```rust |
159 | | /// # use time::Month; |
160 | | /// assert_eq!(Month::January.nth_prev(4), Month::September); |
161 | | /// assert_eq!(Month::July.nth_prev(9), Month::October); |
162 | | /// ``` |
163 | | #[inline] |
164 | 0 | pub const fn nth_prev(self, n: u8) -> Self { |
165 | 0 | match self as i8 - 1 - (n % 12) as i8 { |
166 | 0 | 1 | -11 => February, |
167 | 0 | 2 | -10 => March, |
168 | 0 | 3 | -9 => April, |
169 | 0 | 4 | -8 => May, |
170 | 0 | 5 | -7 => June, |
171 | 0 | 6 | -6 => July, |
172 | 0 | 7 | -5 => August, |
173 | 0 | 8 | -4 => September, |
174 | 0 | 9 | -3 => October, |
175 | 0 | 10 | -2 => November, |
176 | 0 | 11 | -1 => December, |
177 | 0 | val => { |
178 | 0 | debug_assert!(val == 0); |
179 | 0 | January |
180 | | } |
181 | | } |
182 | 0 | } |
183 | | } |
184 | | |
185 | | mod private { |
186 | | #[non_exhaustive] |
187 | | #[derive(Debug, Clone, Copy)] |
188 | | pub struct MonthMetadata; |
189 | | } |
190 | | use private::MonthMetadata; |
191 | | |
192 | | impl SmartDisplay for Month { |
193 | | type Metadata = MonthMetadata; |
194 | | |
195 | | #[inline] |
196 | 0 | fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> { |
197 | 0 | match self { |
198 | 0 | January => Metadata::new(7, self, MonthMetadata), |
199 | 0 | February => Metadata::new(8, self, MonthMetadata), |
200 | 0 | March => Metadata::new(5, self, MonthMetadata), |
201 | 0 | April => Metadata::new(5, self, MonthMetadata), |
202 | 0 | May => Metadata::new(3, self, MonthMetadata), |
203 | 0 | June => Metadata::new(4, self, MonthMetadata), |
204 | 0 | July => Metadata::new(4, self, MonthMetadata), |
205 | 0 | August => Metadata::new(6, self, MonthMetadata), |
206 | 0 | September => Metadata::new(9, self, MonthMetadata), |
207 | 0 | October => Metadata::new(7, self, MonthMetadata), |
208 | 0 | November => Metadata::new(8, self, MonthMetadata), |
209 | 0 | December => Metadata::new(8, self, MonthMetadata), |
210 | | } |
211 | 0 | } |
212 | | |
213 | | #[inline] |
214 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
215 | 0 | f.pad(match self { |
216 | 0 | January => "January", |
217 | 0 | February => "February", |
218 | 0 | March => "March", |
219 | 0 | April => "April", |
220 | 0 | May => "May", |
221 | 0 | June => "June", |
222 | 0 | July => "July", |
223 | 0 | August => "August", |
224 | 0 | September => "September", |
225 | 0 | October => "October", |
226 | 0 | November => "November", |
227 | 0 | December => "December", |
228 | | }) |
229 | 0 | } |
230 | | } |
231 | | |
232 | | impl fmt::Display for Month { |
233 | | #[inline] |
234 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
235 | 0 | SmartDisplay::fmt(self, f) |
236 | 0 | } |
237 | | } |
238 | | |
239 | | impl FromStr for Month { |
240 | | type Err = error::InvalidVariant; |
241 | | |
242 | | #[inline] |
243 | 0 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
244 | 0 | match s { |
245 | 0 | "January" => Ok(January), |
246 | 0 | "February" => Ok(February), |
247 | 0 | "March" => Ok(March), |
248 | 0 | "April" => Ok(April), |
249 | 0 | "May" => Ok(May), |
250 | 0 | "June" => Ok(June), |
251 | 0 | "July" => Ok(July), |
252 | 0 | "August" => Ok(August), |
253 | 0 | "September" => Ok(September), |
254 | 0 | "October" => Ok(October), |
255 | 0 | "November" => Ok(November), |
256 | 0 | "December" => Ok(December), |
257 | 0 | _ => Err(error::InvalidVariant), |
258 | | } |
259 | 0 | } |
260 | | } |
261 | | |
262 | | impl From<Month> for u8 { |
263 | | #[inline] |
264 | 8.64k | fn from(month: Month) -> Self { |
265 | 8.64k | month as Self |
266 | 8.64k | } <u8 as core::convert::From<time::month::Month>>::from Line | Count | Source | 264 | 8.64k | fn from(month: Month) -> Self { | 265 | 8.64k | month as Self | 266 | 8.64k | } |
Unexecuted instantiation: <u8 as core::convert::From<time::month::Month>>::from |
267 | | } |
268 | | |
269 | | impl TryFrom<u8> for Month { |
270 | | type Error = error::ComponentRange; |
271 | | |
272 | | #[inline] |
273 | 0 | fn try_from(value: u8) -> Result<Self, Self::Error> { |
274 | 0 | match NonZero::new(value) { |
275 | 0 | Some(value) => Self::from_number(value), |
276 | 0 | None => Err(error::ComponentRange { |
277 | 0 | name: "month", |
278 | 0 | minimum: 1, |
279 | 0 | maximum: 12, |
280 | 0 | value: 0, |
281 | 0 | conditional_message: None, |
282 | 0 | }), |
283 | | } |
284 | 0 | } Unexecuted instantiation: <time::month::Month as core::convert::TryFrom<u8>>::try_from Unexecuted instantiation: <time::month::Month as core::convert::TryFrom<u8>>::try_from |
285 | | } |