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
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Dia-Time

Copyright (C) 2018-2022, 2024  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2018-2022".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Month

use {
    core::{
        fmt::{self, Debug, Display, Formatter},
        ops::Deref,
        str::FromStr,
    },
    crate::{Error, Result as CrateResult},
};

mod tests;

pub (crate) const END_OF_FEBRUARY_IN_LEAP_YEARS: u8 = 29;
pub (crate) const END_OF_FEBRUARY_IN_COMMON_YEARS: u8 = 28;

const SECONDS_OF_28_DAYS: i64 = (crate::DAY * 28) as i64;
const SECONDS_OF_29_DAYS: i64 = (crate::DAY * 29) as i64;
const SECONDS_OF_30_DAYS: i64 = (crate::DAY * 30) as i64;
const SECONDS_OF_31_DAYS: i64 = (crate::DAY * 31) as i64;

/// # Month
///
/// ## Notes
///
/// - The months' names are English only. That applies to implementations of [`FromStr`][trait:core/str/FromStr],
///   [`Deref<Target=str>`][trait:core/ops/Deref]...
/// - Implementation of `Deref<Target=str>` is same as [`Display`][trait:core/fmt/Display]'s, but it's faster because it provides references to
///   static strings.
///
/// [trait:core/fmt/Display]: https://doc.rust-lang.org/core/fmt/trait.Display.html
/// [trait:core/ops/Deref]: https://doc.rust-lang.org/core/ops/trait.Deref.html
/// [trait:core/str/FromStr]: https://doc.rust-lang.org/core/str/trait.FromStr.html
#[derive(Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Clone, Copy)]
pub enum Month {

    /// # January,
    January,

    /// # February,
    February,

    /// # March,
    March,

    /// # April,
    April,

    /// # May,
    May,

    /// # June,
    June,

    /// # July,
    July,

    /// # August,
    August,

    /// # September,
    September,

    /// # October,
    October,

    /// # November,
    November,

    /// # December,
    December,

}

impl Month {

    /// # Returns ordinal number of this month
    ///
    /// January will be `1`, February will be `2`...
    pub fn order(&self) -> u8 {
        match self {
            Month::January => 1,
            Month::February => 2,
            Month::March => 3,
            Month::April => 4,
            Month::May => 5,
            Month::June => 6,
            Month::July => 7,
            Month::August => 8,
            Month::September => 9,
            Month::October => 10,
            Month::November => 11,
            Month::December => 12,
        }
    }

    /// # Parses month from an ordinal number
    ///
    /// `1` will be January, `2` will be February...
    pub fn try_from_order(order: u8) -> CrateResult<Self> {
        match order {
            1 => Ok(Month::January),
            2 => Ok(Month::February),
            3 => Ok(Month::March),
            4 => Ok(Month::April),
            5 => Ok(Month::May),
            6 => Ok(Month::June),
            7 => Ok(Month::July),
            8 => Ok(Month::August),
            9 => Ok(Month::September),
            10 => Ok(Month::October),
            11 => Ok(Month::November),
            12 => Ok(Month::December),
            _ => Err(err!("Invalid month: {}", order)),
        }
    }

    /// # Gets next month
    pub fn next(&self) -> Option<Self> {
        match self {
            Month::January => Some(Month::February),
            Month::February => Some(Month::March),
            Month::March => Some(Month::April),
            Month::April => Some(Month::May),
            Month::May => Some(Month::June),
            Month::June => Some(Month::July),
            Month::July => Some(Month::August),
            Month::August => Some(Month::September),
            Month::September => Some(Month::October),
            Month::October => Some(Month::November),
            Month::November => Some(Month::December),
            Month::December => None,
        }
    }

    /// # Gets next month
    ///
    /// Next of December will be January.
    pub fn wrapping_next(&self) -> Self {
        match self {
            Month::January => Month::February,
            Month::February => Month::March,
            Month::March => Month::April,
            Month::April => Month::May,
            Month::May => Month::June,
            Month::June => Month::July,
            Month::July => Month::August,
            Month::August => Month::September,
            Month::September => Month::October,
            Month::October => Month::November,
            Month::November => Month::December,
            Month::December => Month::January,
        }
    }

    /// # Gets last month
    pub fn last(&self) -> Option<Self> {
        match self {
            Month::January => None,
            Month::February => Some(Month::January),
            Month::March => Some(Month::February),
            Month::April => Some(Month::March),
            Month::May => Some(Month::April),
            Month::June => Some(Month::May),
            Month::July => Some(Month::June),
            Month::August => Some(Month::July),
            Month::September => Some(Month::August),
            Month::October => Some(Month::September),
            Month::November => Some(Month::October),
            Month::December => Some(Month::November),
        }
    }

    /// # Gets last month
    ///
    /// Last of January will be December.
    pub fn wrapping_last(&self) -> Self {
        match self {
            Month::January => Month::December,
            Month::February => Month::January,
            Month::March => Month::February,
            Month::April => Month::March,
            Month::May => Month::April,
            Month::June => Month::May,
            Month::July => Month::June,
            Month::August => Month::July,
            Month::September => Month::August,
            Month::October => Month::September,
            Month::November => Month::October,
            Month::December => Month::November,
        }
    }

    /// # Converts self into Unix value
    pub (crate) fn to_unix(&self) -> i32 {
        match self {
            Month::January => 0,
            Month::February => 1,
            Month::March => 2,
            Month::April => 3,
            Month::May => 4,
            Month::June => 5,
            Month::July => 6,
            Month::August => 7,
            Month::September => 8,
            Month::October => 9,
            Month::November => 10,
            Month::December => 11,
        }
    }

    /// # Tries to convert a Unix value into self
    #[cfg(test)]
    #[cfg(not(windows))]
    pub (crate) fn try_from_unix(tm_mon: i32) -> CrateResult<Self> {
        match tm_mon {
            0 => Ok(Month::January),
            1 => Ok(Month::February),
            2 => Ok(Month::March),
            3 => Ok(Month::April),
            4 => Ok(Month::May),
            5 => Ok(Month::June),
            6 => Ok(Month::July),
            7 => Ok(Month::August),
            8 => Ok(Month::September),
            9 => Ok(Month::October),
            10 => Ok(Month::November),
            11 => Ok(Month::December),
            _ => Err(err!("Invalid Unix month: {tm_mon}", tm_mon=tm_mon)),
        }
    }

}

impl Deref for Month {

    type Target = str;

    fn deref(&self) -> &Self::Target {
        match self {
            Month::January => "January",
            Month::February => "February",
            Month::March => "March",
            Month::April => "April",
            Month::May => "May",
            Month::June => "June",
            Month::July => "July",
            Month::August => "August",
            Month::September => "September",
            Month::October => "October",
            Month::November => "November",
            Month::December => "December",
        }
    }

}

impl Display for Month {

    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        f.write_str(self)
    }

}

impl FromStr for Month {

    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.eq_ignore_ascii_case(&*Month::January) {
            Ok(Month::January)
        } else if s.eq_ignore_ascii_case(&*Month::February) {
            Ok(Month::February)
        } else if s.eq_ignore_ascii_case(&*Month::March) {
            Ok(Month::March)
        } else if s.eq_ignore_ascii_case(&*Month::April) {
            Ok(Month::April)
        } else if s.eq_ignore_ascii_case(&*Month::May) {
            Ok(Month::May)
        } else if s.eq_ignore_ascii_case(&*Month::June) {
            Ok(Month::June)
        } else if s.eq_ignore_ascii_case(&*Month::July) {
            Ok(Month::July)
        } else if s.eq_ignore_ascii_case(&*Month::August) {
            Ok(Month::August)
        } else if s.eq_ignore_ascii_case(&*Month::September) {
            Ok(Month::September)
        } else if s.eq_ignore_ascii_case(&*Month::October) {
            Ok(Month::October)
        } else if s.eq_ignore_ascii_case(&*Month::November) {
            Ok(Month::November)
        } else if s.eq_ignore_ascii_case(&*Month::December) {
            Ok(Month::December)
        } else {
            Err(err!("Unknown month: {:?}", s))
        }
    }

}

/// # Gets last day of month
pub (crate) fn last_day_of_month(month: &Month, leap_year: bool) -> u8 {
    match month {
        Month::January => 31,
        Month::February => if leap_year { END_OF_FEBRUARY_IN_LEAP_YEARS } else { END_OF_FEBRUARY_IN_COMMON_YEARS },
        Month::March => 31,
        Month::April => 30,
        Month::May => 31,
        Month::June => 30,
        Month::July => 31,
        Month::August => 31,
        Month::September => 30,
        Month::October => 31,
        Month::November => 30,
        Month::December => 31,
    }
}

/// # Gets seconds of month
pub (crate) fn seconds_of_month(month: &Month, leap_year: bool) -> i64 {
    match month {
        Month::January | Month::March | Month::May | Month::July | Month::August | Month::October | Month::December => SECONDS_OF_31_DAYS,
        Month::February => if leap_year { SECONDS_OF_29_DAYS } else { SECONDS_OF_28_DAYS },
        Month::April | Month::June | Month::September | Month::November => SECONDS_OF_30_DAYS,
    }
}