Dates and their structs
This commit is contained in:
parent
00aa0efab9
commit
3b961b93d7
1 changed files with 86 additions and 3 deletions
89
src/date.rs
89
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<usize> for Day {
|
||||
fn from(value: usize) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<usize> 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<usize> 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue