use chrono::{Datelike, FixedOffset, Utc};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DayAvailability {
OutsideEdition,
Locked,
Unlocked,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct CalendarDate {
pub year: usize,
pub month: u32,
pub day: usize,
}
impl CalendarDate {
pub(crate) fn today() -> Self {
let offset = FixedOffset::west_opt(5 * 3600).expect("UTC-5 is a valid offset");
let now = Utc::now().with_timezone(&offset);
Self {
year: usize::try_from(now.year()).expect("Expected a positive calendar year"),
month: now.month(),
day: now.day() as usize,
}
}
pub(crate) fn unlocked_days(&self, year: usize) -> usize {
if year > self.year || year < 2015 {
return 0;
}
let total = CalendarDate::edition_days(year);
if year < self.year {
return total;
}
if self.month != 12 {
return 0;
}
self.day.min(total)
}
pub(crate) fn day_availability(&self, year: usize, day: usize) -> DayAvailability {
if !(1..=CalendarDate::edition_days(year)).contains(&day) {
return DayAvailability::OutsideEdition;
}
if day > self.unlocked_days(year) {
return DayAvailability::Locked;
}
DayAvailability::Unlocked
}
pub(crate) fn edition_days(year: usize) -> usize {
match year {
2015..=2024 => 25,
2025.. => 12,
_ => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::{CalendarDate, DayAvailability};
#[test]
fn classifies_days_by_edition_length_and_unlock_date() {
use DayAvailability::{Locked, OutsideEdition, Unlocked};
let december = CalendarDate {
year: 2026,
month: 12,
day: 3,
};
let november = CalendarDate {
year: 2026,
month: 11,
day: 30,
};
for (date, year, day, expected) in [
(december, 2014, 1, OutsideEdition),
(december, 2024, 25, Unlocked),
(december, 2024, 26, OutsideEdition),
(december, 2025, 12, Unlocked),
(december, 2025, 13, OutsideEdition),
(december, 2026, 0, OutsideEdition),
(december, 2026, 3, Unlocked),
(december, 2026, 4, Locked),
(december, 2026, 13, OutsideEdition),
(december, 2027, 1, Locked),
(november, 2026, 1, Locked),
] {
assert_eq!(date.day_availability(year, day), expected, "{year}/{day}");
}
}
}