fn is_leak(year: usize) -> bool {
if year % 400 == 0 {
true
} else if year % 100 == 0 {
false
} else {
year % 4 == 0
}
}
pub struct Date {
pub year: usize,
pub month: usize,
pub day: usize,
pub leak: bool,
}
impl Date {
pub fn from(date: [usize; 3]) -> Self {
Self {
year: date[2],
month: date[1],
day: date[0],
leak: is_leak(date[2]),
}
}
pub fn next_day(self) -> Self {
Self {
year: self.year,
month: self.month,
day: self.day + 1,
leak: is_leak(self.year),
}
}
pub fn next_month(self) -> Self {
Self {
year: self.year,
month: self.month + 1,
day: 1,
leak: is_leak(self.year),
}
}
pub fn next_year(self) -> Self {
Self {
year: self.year + 1,
month: 1,
day: 1,
leak: is_leak(self.year),
}
}
}
pub fn next(date: Date, counter: i64) -> (Date, i64) {
let mut _month_tup = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if !date.leak {
_month_tup = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
} else {
_month_tup = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
}
if date.day < _month_tup[date.month - 1] {
(date.next_day(), counter + 1)
} else if date.month < 12 {
(date.next_month(), counter + 1)
} else {
(date.next_year(), counter + 1)
}
}