summaryrefslogtreecommitdiff
path: root/src/month.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-03-16 14:51:49 -0400
committerBotahamec <botahamec@outlook.com>2022-03-16 14:51:49 -0400
commit5ca69f1830763b689bae9c4873a2912b3f1e23b1 (patch)
tree86d4e9c5cf3a79ae372509a28e962c200fa094f5 /src/month.rs
parent0fb870509821eb6f8158a9ee3cc02e6a0f951c86 (diff)
Crude add year and add month
Diffstat (limited to 'src/month.rs')
-rw-r--r--src/month.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/month.rs b/src/month.rs
index fa19324..c5407f7 100644
--- a/src/month.rs
+++ b/src/month.rs
@@ -181,6 +181,7 @@ impl Month {
// TODO docs
+ // TODO handle ordinals greater than 365
pub const fn from_ordinal_common(ordinal: u16) -> Self {
if ordinal < 31 {
January
@@ -314,6 +315,48 @@ impl Month {
}
}
+ pub const fn days_common_year(self) -> u8 {
+ match self {
+ January => 31,
+ February => 28,
+ March => 31,
+ April => 30,
+ May => 31,
+ June => 30,
+ July => 31,
+ August => 31,
+ September => 30,
+ October => 31,
+ November => 30,
+ December => 31,
+ }
+ }
+
+ pub const fn days_leap_year(self) -> u8 {
+ match self {
+ January => 31,
+ February => 29,
+ March => 31,
+ April => 30,
+ May => 31,
+ June => 30,
+ July => 31,
+ August => 31,
+ September => 30,
+ October => 31,
+ November => 30,
+ December => 31,
+ }
+ }
+
+ pub const fn days(self, leap_year: bool) -> u8 {
+ if leap_year {
+ self.days_leap_year()
+ } else {
+ self.days_common_year()
+ }
+ }
+
/// Returns the number of days up to the end of the month in a leap year.
pub const fn last_day_ordinal_leap(self) -> u16 {
match self {
@@ -341,6 +384,15 @@ impl Month {
self.last_day_ordinal_common()
}
}
+
+ pub const fn add_overflowing(self, months: u8) -> (Self, u8) {
+ let zero_indexed_num = ((self as u16) - 1) + months as u16;
+ let wraps = (zero_indexed_num as u8) / 12;
+ let zero_indexed_month = zero_indexed_num % 12;
+ let month = Self::from_u8((zero_indexed_month as u8) + 1).unwrap();
+
+ (month, wraps)
+ }
}
impl From<Month> for u8 {