veloren_common/
calendar.rs1use chrono::{DateTime, Datelike, Local, TimeZone, Utc};
2use chrono_tz::Tz;
3use serde::{Deserialize, Serialize};
4use strum::EnumIter;
5
6#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, EnumIter)]
7#[repr(u16)]
8pub enum CalendarEvent {
9 Christmas = 0,
10 Halloween = 1,
11 AprilFools = 2,
12 Easter = 3,
13}
14
15#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
16pub struct Calendar {
17 events: Vec<CalendarEvent>,
18}
19
20impl Calendar {
21 pub fn is_event(&self, event: CalendarEvent) -> bool { self.events.contains(&event) }
22
23 pub fn events(&self) -> impl ExactSizeIterator<Item = &CalendarEvent> + '_ {
24 self.events.iter()
25 }
26
27 pub fn from_events(events: Vec<CalendarEvent>) -> Self { Self { events } }
28
29 pub fn from_tz(tz: Option<Tz>) -> Self {
30 let mut this = Self::default();
31
32 let now = match tz {
33 Some(tz) => {
34 let utc = Utc::now().naive_utc();
35 DateTime::<Tz>::from_naive_utc_and_offset(utc, tz.offset_from_utc_datetime(&utc))
36 .naive_local()
37 },
38 None => Local::now().naive_local(),
39 };
40
41 if now.month() == 12 && (20..=30).contains(&now.day()) {
42 this.events.push(CalendarEvent::Christmas);
43 }
44
45 if now.month() == 10 && (24..=31).contains(&now.day()) {
46 this.events.push(CalendarEvent::Halloween);
47 }
48
49 if now.month() == 4 && now.day() == 1 {
50 this.events.push(CalendarEvent::AprilFools);
51 }
52
53 if now.month() == 3 && now.day() == 31 || now.month() == 4 && (1..=7).contains(&now.day()) {
54 this.events.push(CalendarEvent::Easter);
55 }
56
57 this
58 }
59}