veloren/common/src/calendar.rs

43 lines
1.2 KiB
Rust
Raw Normal View History

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>,
}
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 } }
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(),
};
if now.month() == 12 && (20..=30).contains(&now.day()) {
2021-12-07 22:00:43 +00:00
this.events.push(CalendarEvent::Christmas);
}
this
}
}