use std::fmt;
use crate::*;
#[derive(Debug)]
pub struct Doomsday(pub usize);
impl Doomsday {
#[inline]
pub fn new(year: usize) -> Doomsday {
Doomsday(year)
}
pub fn anchor(&self, month: Month) -> usize {
match month {
January => if is_leap(self.0) { 4 } else { 3 },
February => if is_leap(self.0) { 29 } else { 28 },
March => 0,
April => 4,
May => 9,
June => 6,
July => 11,
August => 8,
September => 5,
October => 10,
November => 7,
December => 12,
}
}
pub fn day(&self) -> Day {
Day::from_anchor(self.0 + leaps(self.0))
}
}
impl fmt::Display for Doomsday {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.day())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_day() {
assert_eq!(Day::Tuesday, Doomsday(1752).day());
assert_eq!(Day::Sunday, Doomsday(1993).day());
assert_eq!(Day::Saturday, Doomsday(2020).day());
}
#[test]
fn test_anchor() {
assert_eq!(3, Doomsday(1993).anchor(Month::January));
assert_eq!(4, Doomsday(2020).anchor(Month::January));
assert_eq!(28, Doomsday(1993).anchor(Month::February));
assert_eq!(29, Doomsday(2020).anchor(Month::February));
assert_eq!(0, Doomsday(2000).anchor(Month::March));
assert_eq!(4, Doomsday(2020).anchor(Month::April));
assert_eq!(9, Doomsday(2020).anchor(Month::May));
assert_eq!(6, Doomsday(2020).anchor(Month::June));
assert_eq!(11, Doomsday(2020).anchor(Month::July));
assert_eq!(8, Doomsday(2020).anchor(Month::August));
assert_eq!(5, Doomsday(2020).anchor(Month::September));
assert_eq!(10, Doomsday(2020).anchor(Month::October));
assert_eq!(7, Doomsday(2020).anchor(Month::November));
assert_eq!(12, Doomsday(2020).anchor(Month::December));
}
}