1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! Timekeeping datasets including weekdays and months.
#![allow(missing_docs)]
#![allow(clippy::missing_docs_in_private_items)]
numeric_enum! {
/// The days of the week
pub enum Day {
/// [Monday](https://en.wikipedia.org/wiki/Monday) is the first day of the week
Monday = 1 => "Monday",
/// [Tuesday](https://en.wikipedia.org/wiki/Tuesday) is the second day of the week
Tuesday = 2 => "Tuesday",
/// [Wednesday](https://en.wikipedia.org/wiki/Wednesday) is the third day of the week
Wednesday = 3 => "Wednesday",
/// [Thursday](https://en.wikipedia.org/wiki/Thursday) is the fourth day of the week
Thursday = 4 => "Thursday",
/// [Friday](https://en.wikipedia.org/wiki/Friday) is the fifth day of the week
Friday = 5 => "Friday",
/// [Saturday](https://en.wikipedia.org/wiki/Saturday) is the sixth day of the week
Saturday = 6 => "Saturday",
/// [Sunday](https://en.wikipedia.org/wiki/Sunday) is the seventh day of the week
Sunday = 7 => "Sunday",
}
}
numeric_enum! {
/// The months of the year
pub enum Month {
/// [January](https://en.wikipedia.org/wiki/January) is the first month of the year
January = 1 => "January",
/// [February](https://en.wikipedia.org/wiki/February) is the second month of the year
February = 2 => "February",
/// [March](https://en.wikipedia.org/wiki/March) is the third month of the year
March = 3 => "March",
/// [April](https://en.wikipedia.org/wiki/April) is the fourth month of the year
April = 4 => "April",
/// [May](https://en.wikipedia.org/wiki/May) is the fifth month of the year
May = 5 => "May",
/// [June](https://en.wikipedia.org/wiki/June) is the sixth month of the year
June = 6 => "June",
/// [July](https://en.wikipedia.org/wiki/July) is the seventh month of the year
July = 7 => "July",
/// [August](https://en.wikipedia.org/wiki/August) is the eighth month of the year
August = 8 => "August",
/// [September](https://en.wikipedia.org/wiki/September) is the ninth month of the year
September = 9 => "September",
/// [October](https://en.wikipedia.org/wiki/October) is the tenth month of the year
October = 10 => "October",
/// [November](https://en.wikipedia.org/wiki/November) is the eleventh month of the year
November = 11 => "November",
/// [December](https://en.wikipedia.org/wiki/December) is the twelfth month of the year
December = 12 => "December",
}
}
dataset_enum! {
/// The four meteorological seasons in Northern Hemisphere calendar order.
///
/// This order is a teaching convention and does not apply to both hemispheres.
///
/// # Examples
/// ```
/// use hodgepodge::Season;
///
/// let vacation = Season::Summer;
/// assert_eq!(vacation as u8, 3);
/// ```
pub enum Season {
/// [Winter](https://en.wikipedia.org/wiki/Winter) is the first season of the year.
Winter = 1 => "Winter",
/// [Spring](https://en.wikipedia.org/wiki/Spring_(season)) is the second season of the year.
Spring = 2 => "Spring",
/// [Summer](https://en.wikipedia.org/wiki/Summer) is the third season of the year.
Summer = 3 => "Summer",
/// [Fall](https://en.wikipedia.org/wiki/Autumn) (autumn) is the fourth season of the year.
Fall = 4 => "Fall",
}
}
dataset_enum! {
/// Fiscal quarters in chronological order.
///
/// # Examples
/// ```
/// use hodgepodge::Quarter;
///
/// let reporting_period = Quarter::Q4;
/// assert_eq!(reporting_period as u8, 4);
/// ```
pub enum Quarter {
/// The first quarter of the fiscal year.
Q1 = 1 => "Quarter 1",
/// The second quarter of the fiscal year.
Q2 = 2 => "Quarter 2",
/// The third quarter of the fiscal year.
Q3 = 3 => "Quarter 3",
/// The fourth quarter of the fiscal year.
Q4 = 4 => "Quarter 4",
}
}
#[cfg(all(test, feature = "strum"))]
mod tests {
use super::{Quarter, Season};
use crate::{EnumCount, IntoEnumIterator};
#[test]
fn seasons_iterate_in_calendar_order() {
let observed: Vec<u8> = Season::iter().map(|season| season as u8).collect();
assert_eq!(observed, vec![1, 2, 3, 4]);
}
#[test]
fn seasons_count_matches_four() {
assert_eq!(<Season as EnumCount>::COUNT, 4);
}
#[test]
fn quarters_iterate_in_chronological_order() {
let observed: Vec<u8> = Quarter::iter().map(|quarter| quarter as u8).collect();
assert_eq!(observed, vec![1, 2, 3, 4]);
}
#[test]
fn quarters_count_matches_four() {
assert_eq!(<Quarter as EnumCount>::COUNT, 4);
}
}
impl Day {
/// Returns the Monday-first weekday number, from 1 to 7.
/// Use `Day::try_from(number)` for checked conversion back.
#[must_use]
pub const fn number(self) -> u8 {
self as u8
}
}
impl Month {
/// Returns the calendar month number, from 1 to 12.
/// Use `Month::try_from(number)` for checked conversion back.
#[must_use]
pub const fn number(self) -> u8 {
self as u8
}
}
impl Day {
/// Returns the three-letter English abbreviation.
#[must_use]
pub const fn abbreviation(self) -> &'static str {
match self {
Self::Monday => "Mon",
Self::Tuesday => "Tue",
Self::Wednesday => "Wed",
Self::Thursday => "Thu",
Self::Friday => "Fri",
Self::Saturday => "Sat",
Self::Sunday => "Sun",
}
}
}
impl Month {
/// Returns the three-letter English abbreviation.
#[must_use]
pub const fn abbreviation(self) -> &'static str {
match self {
Self::January => "Jan",
Self::February => "Feb",
Self::March => "Mar",
Self::April => "Apr",
Self::May => "May",
Self::June => "Jun",
Self::July => "Jul",
Self::August => "Aug",
Self::September => "Sep",
Self::October => "Oct",
Self::November => "Nov",
Self::December => "Dec",
}
}
}
impl Day {
/// Whether this is Saturday or Sunday under the common five-day workweek convention.
#[must_use]
pub const fn is_weekend(self) -> bool {
matches!(self, Self::Saturday | Self::Sunday)
}
}
impl Month {
/// Returns the month length in a non-leap Gregorian year.
#[must_use]
pub const fn days_in_common_year(self) -> u8 {
match self {
Self::January
| Self::March
| Self::May
| Self::July
| Self::August
| Self::October
| Self::December => 31,
Self::February => 28,
Self::April | Self::June | Self::September | Self::November => 30,
}
}
}
impl Season {
/// Returns Northern Hemisphere meteorological months, in seasonal order.
#[must_use]
pub const fn months(self) -> [Month; 3] {
match self {
Self::Winter => [Month::December, Month::January, Month::February],
Self::Spring => [Month::March, Month::April, Month::May],
Self::Summer => [Month::June, Month::July, Month::August],
Self::Fall => [Month::September, Month::October, Month::November],
}
}
}
impl Quarter {
/// Returns the fiscal quarter number (1–4), independent of fiscal start month.
#[must_use]
pub const fn number(self) -> u8 {
self as u8
}
}
checked_u8_enum!(Quarter, number);
impl Day {
/// Looks up a abbreviation ignoring ASCII case; does not trim whitespace.
#[must_use]
pub fn from_abbreviation(code: &str) -> Option<Self> {
Self::ALL
.iter()
.copied()
.find(|value| value.abbreviation().eq_ignore_ascii_case(code))
}
}
impl Month {
/// Looks up a abbreviation ignoring ASCII case; does not trim whitespace.
#[must_use]
pub fn from_abbreviation(code: &str) -> Option<Self> {
Self::ALL
.iter()
.copied()
.find(|value| value.abbreviation().eq_ignore_ascii_case(code))
}
}
dataset_enum! {
/// Northern and Southern Hemisphere meteorological-season conventions.
/// Local tropical wet/dry seasons are outside this classification.
pub enum Hemisphere { Northern => "Northern", Southern => "Southern" }
}
impl Day {
/// Returns the following day, wrapping Sunday to Monday.
#[must_use]
pub const fn next(self) -> Self {
Self::ALL[self.number() as usize % 7]
}
/// Returns the preceding day, wrapping Monday to Sunday.
#[must_use]
pub const fn previous(self) -> Self {
Self::ALL[(self.number() as usize + 5) % 7]
}
}
impl Month {
/// Returns the following month, wrapping December to January.
#[must_use]
pub const fn next(self) -> Self {
Self::ALL[self.number() as usize % 12]
}
/// Returns the preceding month, wrapping January to December.
#[must_use]
pub const fn previous(self) -> Self {
Self::ALL[(self.number() as usize + 10) % 12]
}
/// Returns the length in a proleptic Gregorian year (astronomical year numbering).
#[must_use]
pub const fn days_in_year(self, year: i32) -> u8 {
if matches!(self, Self::February) && is_gregorian_leap_year(year) {
29
} else {
self.days_in_common_year()
}
}
/// Returns the meteorological season under the specified hemisphere convention.
#[must_use]
pub const fn season(self, hemisphere: Hemisphere) -> Season {
let north = match self {
Self::December | Self::January | Self::February => Season::Winter,
Self::March | Self::April | Self::May => Season::Spring,
Self::June | Self::July | Self::August => Season::Summer,
Self::September | Self::October | Self::November => Season::Fall,
};
match hemisphere {
Hemisphere::Northern => north,
Hemisphere::Southern => north.opposite(),
}
}
/// Returns the quarter with an explicit fiscal start month; January gives calendar quarters.
#[must_use]
pub const fn quarter(self, fiscal_start: Self) -> Quarter {
Quarter::ALL[((self.number() as usize + 12 - fiscal_start.number() as usize) % 12) / 3]
}
}
/// Whether a year is leap in the proleptic Gregorian calendar, including year zero.
#[must_use]
pub const fn is_gregorian_leap_year(year: i32) -> bool {
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}
impl Season {
/// Returns the season six months away in the four-season convention.
#[must_use]
pub const fn opposite(self) -> Self {
match self {
Self::Winter => Self::Summer,
Self::Summer => Self::Winter,
Self::Spring => Self::Fall,
Self::Fall => Self::Spring,
}
}
/// Returns meteorological months for the specified hemisphere.
#[must_use]
pub const fn months_in(self, hemisphere: Hemisphere) -> [Month; 3] {
match hemisphere {
Hemisphere::Northern => self.months(),
Hemisphere::Southern => self.opposite().months(),
}
}
}
impl Quarter {
/// Returns months in fiscal order for an explicit fiscal start month.
#[must_use]
pub const fn months(self, fiscal_start: Month) -> [Month; 3] {
let first = (fiscal_start.number() as usize - 1 + (self.number() as usize - 1) * 3) % 12;
[
Month::ALL[first],
Month::ALL[(first + 1) % 12],
Month::ALL[(first + 2) % 12],
]
}
}