2021-12-06 22:17:20 +00:00
|
|
|
use chrono::{DateTime, Datelike, Local, TimeZone, Utc};
|
|
|
|
use chrono_tz::Tz;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
|
|
|
#[repr(u16)]
|
|
|
|
pub enum CalendarEvent {
|
|
|
|
Christmas = 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
this
|
|
|
|
}
|
|
|
|
}
|