1use crate::prelude::*;
8use chrono_tz::{Tz, TZ_VARIANTS};
9
10pub const LOCAL: Zone = Zone::Local;
12
13pub const UTC: Zone = Zone::Tz(Tz::UTC);
15
16#[derive(Debug, Clone, Copy, Eq, PartialEq)]
18pub enum Zone {
19 Local,
20 Tz(Tz),
21}
22
23impl Zone {
24 pub fn from_name(name: impl AsRef<str>) -> Result<Zone, Unrecognized> {
27 name.as_ref().parse()
28 }
29
30 pub fn all() -> impl Iterator<Item = Self> {
32 TZ_VARIANTS.iter().cloned().map(Zone::Tz)
33 }
34
35 pub fn name(&self) -> &'static str {
37 match &self {
38 Self::Local => "Local",
39 Self::Tz(tz) => tz.name(),
40 }
41 }
42}
43
44impl FromStr for Zone {
45 type Err = Unrecognized;
46
47 fn from_str(s: &str) -> Result<Self, Self::Err> {
48 match s.parse() {
49 Ok(tz) => Ok(Zone::Tz(tz)),
50 Err(_) => Err(Unrecognized),
51 }
52 }
53}
54
55#[derive(Debug, Error)]
57#[error("Unrecognized time zone.")]
58pub struct Unrecognized;