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