From 3b961b93d7bf5a081e7e7289e9782a3d36f2f963 Mon Sep 17 00:00:00 2001 From: Rendo Date: Sat, 14 Feb 2026 19:38:26 +0500 Subject: [PATCH] Dates and their structs --- src/date.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/src/date.rs b/src/date.rs index 2bc9147..f3a9465 100644 --- a/src/date.rs +++ b/src/date.rs @@ -1,5 +1,88 @@ +pub const DAYS_IN_YEAR: f64 = 365.2425; + pub struct Date { - year: u16, - month: u8, - day: u8, + year: Year, + month: Month, + day: Day, +} + +struct Day(usize); +enum Month { + January, + February, + March, + April, + May, + June, + July, + August, + September, + October, + November, + December, +} +struct Year(usize); +enum WeekDay { + Sunday, + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday, +} + +impl From for Day { + fn from(value: usize) -> Self { + Self(value) + } +} + +impl From for Month { + fn from(value: usize) -> Self { + match value % 12 { + 0 => 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, + _ => Month::January, + } + } +} + +impl From for Year { + fn from(value: usize) -> Self { + Self(value) + } +} +impl Year { + pub fn is_leap(&self) -> bool { + self.0 % 4 == 0 && (self.0 % 100 != 0 || self.0 % 400 == 0) + } + pub fn get_days(&self) -> Day { + { if self.is_leap() { 366 } else { 365 } }.into() + } +} +impl Month { + pub fn get_max_days(&self, leap: bool) -> usize { + match self { + Month::February => { + if leap { + 29 + } else { + 28 + } + } + Month::April | Month::June | Month::September | Month::November => 30, + _ => 31, + } + } }