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;