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
use core::fmt::{self, Debug, Formatter};
use crate::calendar::Iso;
use crate::{Calendar, YearMonth};
/// A **month** on a calendar.
///
/// This type does not store or represent a year, day, time, or time-zone.
///
/// It is not possible to know the number of days in the month from this type
/// as that may change based on the year.
///
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct Month<C: Calendar = Iso> {
calendar: C,
number: u8,
}
// Constructor
impl<C: Calendar> Month<C> {
/// Obtains a month from the number of the year on the given calendar, without any checks.
#[allow(unsafe_code)]
#[must_use]
pub(crate) const unsafe fn unchecked_of(calendar: C, number: u8) -> Self {
Self { calendar, number }
}
}
// Components
impl<C: Calendar> Month<C> {
/// Gets the calendar that this month is interpreted in.
#[must_use]
pub const fn calendar(self) -> C {
self.calendar
}
/// Gets the month-of-year number, starting from `1`.
#[must_use]
pub const fn number(self) -> u8 {
self.number
}
// TODO: name()
}
// Composition
impl<C: Calendar> Month<C> {
/// Combines this month with the given year to create a [`YearMonth`].
///
/// # Examples
///
/// ```rust
/// # use ako::calendar::Iso;
/// # fn main() -> ako::Result<()> {
/// let days_in_feb = Iso::FEBRUARY.with_year(2024)?.days();
///
/// assert_eq!(days_in_feb, 29);
/// # Ok(()) }
/// ```
///
pub fn with_year(self, year: i32) -> crate::Result<YearMonth<C>> {
YearMonth::of(self.calendar, year, self.number)
}
}
// TODO: with_day
impl<C: Calendar> Debug for Month<C> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.pad(&self.format_rfc3339())
}
}