2021-12-06 22:17:20 +00:00
|
|
|
use chrono::{DateTime, Datelike, Local, TimeZone, Utc};
|
|
|
|
use chrono_tz::Tz;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2022-11-07 22:39:25 +00:00
|
|
|
use strum::EnumIter;
|
2021-12-06 22:17:20 +00:00
|
|
|
|
2022-11-07 22:39:25 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, EnumIter)]
|
2021-12-06 22:17:20 +00:00
|
|
|
#[repr(u16)]
|
|
|
|
pub enum CalendarEvent {
|
|
|
|
Christmas = 0,
|
2022-10-09 19:35:04 +00:00
|
|
|
Halloween = 1,
|
2023-03-29 23:11:59 +00:00
|
|
|
AprilFools = 2,
|
2021-12-06 22:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 19:51:02 +00:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
2021-12-06 22:17:20 +00:00
|
|
|
pub struct Calendar {
|
2021-12-07 22:00:43 +00:00
|
|
|
events: Vec<CalendarEvent>,
|
2021-12-06 22:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Calendar {
|
|
|
|
pub fn is_event(&self, event: CalendarEvent) -> bool { self.events.contains(&event) }
|
|
|
|
|
|
|
|
pub fn events(&self) -> impl ExactSizeIterator<Item = &CalendarEvent> + '_ {
|
|
|
|
self.events.iter()
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:00:43 +00:00
|
|
|
pub fn from_events(events: Vec<CalendarEvent>) -> Self { Self { events } }
|
2021-12-06 22:17:20 +00:00
|
|
|
|
|
|
|
pub fn from_tz(tz: Option<Tz>) -> Self {
|
|
|
|
let mut this = Self::default();
|
|
|
|
|
|
|
|
let now = match tz {
|
|
|
|
Some(tz) => {
|
|
|
|
let utc = Utc::now().naive_utc();
|
|
|
|
DateTime::<Tz>::from_utc(utc, tz.offset_from_utc_datetime(&utc)).naive_local()
|
|
|
|
},
|
|
|
|
None => Local::now().naive_local(),
|
|
|
|
};
|
|
|
|
|
2021-12-12 11:31:19 +00:00
|
|
|
if now.month() == 12 && (20..=30).contains(&now.day()) {
|
2021-12-07 22:00:43 +00:00
|
|
|
this.events.push(CalendarEvent::Christmas);
|
2021-12-06 22:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:04:04 +00:00
|
|
|
if now.month() == 10 && (24..=31).contains(&now.day()) {
|
2022-10-09 19:35:04 +00:00
|
|
|
this.events.push(CalendarEvent::Halloween);
|
|
|
|
}
|
|
|
|
|
2023-03-29 23:11:59 +00:00
|
|
|
if now.month() == 4 && now.day() == 1 {
|
|
|
|
this.events.push(CalendarEvent::AprilFools);
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:17:20 +00:00
|
|
|
this
|
|
|
|
}
|
|
|
|
}
|