Skip to main content

zmanim/prelude/tz/
antarctica.rs

1use crate::prelude::tz::*;
2use chrono::offset::{Offset, TimeZone};
3use chrono::{Datelike, FixedOffset, NaiveDate, NaiveDateTime};
4use chrono_tz::Antarctica::*;
5use std::str::FromStr;
6use strum_macros::EnumString;
7#[derive(Debug, EnumString, Clone)]
8#[non_exhaustive]
9pub enum Antarctica {
10    Casey,
11    Davis,
12    DumontDUrville,
13    Macquarie,
14    Mawson,
15    McMurdo,
16    Palmer,
17    Rothera,
18    SouthPole,
19    Syowa,
20    Troll,
21    Vostok,
22}
23impl Antarctica {
24    pub(crate) fn try_from_path(p: &[&str]) -> Result<Self, Error> {
25        if p.len() != 1 {
26            return Err(Error::TooManyElements(p.len()));
27        }
28        match p[0] {
29            "South_Pole" => Ok(Self::SouthPole),
30            other => Self::from_str(other).map_err(|_| Error::WrongTimeZone(p[0].to_string())),
31        }
32    }
33    pub(crate) fn get_tz(&self, datetime: &NaiveDateTime) -> FixedOffset {
34        let p = match self {
35            Self::Casey => Casey.from_utc_datetime(datetime),
36            Self::Davis => Davis.from_utc_datetime(datetime),
37            Self::DumontDUrville => DumontDUrville.from_utc_datetime(datetime),
38            Self::Macquarie => Macquarie.from_utc_datetime(datetime),
39            Self::Mawson => Mawson.from_utc_datetime(datetime),
40            Self::McMurdo => McMurdo.from_utc_datetime(datetime),
41            Self::Palmer => Palmer.from_utc_datetime(datetime),
42            Self::Rothera => Rothera.from_utc_datetime(datetime),
43            Self::SouthPole => South_Pole.from_utc_datetime(datetime),
44            Self::Syowa => Syowa.from_utc_datetime(datetime),
45            Self::Troll => Troll.from_utc_datetime(datetime),
46            Self::Vostok => Vostok.from_utc_datetime(datetime),
47        };
48        p.timezone()
49            .offset_from_utc_date(&NaiveDate::from_ymd(
50                datetime.year(),
51                datetime.month(),
52                datetime.day(),
53            ))
54            .fix()
55    }
56}