// The timezone file contains impls of `Timespans` for all timezones in the
// database. The `Wrap` wrapper in the `timezone_impl` module then implements
// TimeZone for any contained struct that implements `Timespans`.
The timezone file contains impls of `Timespans` for all timezones in the
database.
The timezone file contains impls of ` Timespans ` for all timezones in the database .
fn write_timezone_file(timezone_file: &mut File, table: &Table) -> io::Result<()> {
    let zones = table.zonesets.keys().chain(table.links.keys()).collect::<BTreeSet<_>>();
    writeln!(timezone_file, "use core::fmt::{{Debug, Formatter, Error}};",)?;
    writeln!(timezone_file, "use core::str::FromStr;\n",)?;
    writeln!(
        timezone_file,
        "use ::timezone_impl::{{TimeSpans, FixedTimespanSet, FixedTimespan}};\n",
    )?;
    writeln!(
        timezone_file,
        "/// TimeZones built at compile time from the tz database
///
/// This implements [`chrono::TimeZone`] so that it may be used in and to
/// construct chrono's DateTime type. See the root module documentation
/// for details."
    )?;
    writeln!(timezone_file, "#[derive(Clone, Copy, PartialEq, Eq, Hash)]\npub enum Tz {{")?;
    for zone in &zones {
        let zone_name = convert_bad_chars(zone);
        writeln!(
            timezone_file,
            "    /// {raw_zone_name}\n    {zone},",
            zone = zone_name,
            raw_zone_name = zone
        )?;
    }
    writeln!(timezone_file, "}}")?;
    writeln!(
        timezone_file,
        "impl FromStr for Tz {{
    #[cfg(feature = \"std\")]
    type Err = String;
    #[cfg(not(feature = \"std\"))]
    type Err = &'static str;
    fn from_str(s: &str) -> Result<Self, Self::Err> {{
        match s {{"
    )?;
    for zone in &zones {
        let zone_name = convert_bad_chars(zone);
        writeln!(
            timezone_file,
            "            \"{raw_zone_name}\" => Ok(Tz::{zone}),",
            zone = zone_name,
            raw_zone_name = zone
        )?;
    }
    writeln!(
        timezone_file,
        "            #[cfg(feature = \"std\")]
            s => Err(format!(\"'{{}}' is not a valid timezone\", s.to_string())),
            #[cfg(not(feature = \"std\"))]
            _ => Err(\"received invalid timezone\"),
        }}
    }}
}}\n"
    )?;
    writeln!(
        timezone_file,
        "impl Tz {{
    pub fn name(self) -> &'static str {{
        match self {{"
    )?;
    for zone in &zones {
        let zone_name = convert_bad_chars(zone);
        writeln!(
            timezone_file,
            "            Tz::{zone} => \"{raw_zone_name}\",",
            zone = zone_name,
            raw_zone_name = zone
        )?;
    }
    writeln!(
        timezone_file,
        "        }}
    }}
}}\n"
    )?;
    writeln!(
        timezone_file,
        "impl Debug for Tz {{
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {{
        write!(f, \"{{}}\", self.name())
    }}
}}\n"
    )?;
    writeln!(
        timezone_file,
        "impl TimeSpans for Tz {{
    fn timespans(&self) -> FixedTimespanSet {{
        match *self {{"
    )?;
    for zone in &zones {
        let timespans = table.timespans(&zone).unwrap();
        let zone_name = convert_bad_chars(zone);
        writeln!(
            timezone_file,
            "            Tz::{zone} => {{
                const REST: &[(i64, FixedTimespan)] = {rest};
                FixedTimespanSet {{
                    first: FixedTimespan {{
                        utc_offset: {utc},
                        dst_offset: {dst},
                        name: \"{name}\",
                    }},
                    rest: REST
                }}
            }},\n",
            zone = zone_name,
            rest = format_rest(timespans.rest),
            utc = timespans.first.utc_offset,
            dst = timespans.first.dst_offset,
            name = timespans.first.name,
        )?;
    }
    write!(
        timezone_file,
        "         }}
    }}
}}\n"
    )?;
    write!(
        timezone_file,
        "/// An array of every known variant
///
/// Useful for iterating over known timezones:
///
/// ```
/// use chrono_tz::{{TZ_VARIANTS, Tz}};
/// assert!(TZ_VARIANTS.iter().any(|v| *v == Tz::UTC));
/// ```
pub static TZ_VARIANTS: [Tz; {num}] = [
",
        num = zones.len()
    )?;
    for zone in &zones {
        writeln!(timezone_file, "    Tz::{zone},", zone = convert_bad_chars(zone))?;
    }
    write!(timezone_file, "];")?;
    Ok(())
}
fn write_timezone_file ( timezone_file : & mut File , table : & Table ) -> io :: Result < ( ) > { let zones = table . zonesets . keys ( ) . chain ( table . links . keys ( ) ) . collect :: < BTreeSet < _ > > ( ) ; writeln ! ( timezone_file " " ) ? ; writeln ! ( timezone_file " \n " ) ? ; writeln ! ( timezone_file " \n " ) ? ; writeln ! ( timezone_file " " ) ? ; writeln ! ( timezone_file " \n " ) ? ; for zone in & zones { let zone_name = convert_bad_chars ( zone ) ; writeln ! ( timezone_file " \n " zone zone_name raw_zone_name zone ) ? ; } writeln ! ( timezone_file " " ) ? ; writeln ! ( timezone_file " \" \" \" \" " ) ? ; for zone in & zones { let zone_name = convert_bad_chars ( zone ) ; writeln ! ( timezone_file " \" \" " zone zone_name raw_zone_name zone ) ? ; } writeln ! ( timezone_file " \" \" \" \" \" \" \" \" \n " ) ? ; writeln ! ( timezone_file " " ) ? ; for zone in & zones { let zone_name = convert_bad_chars ( zone ) ; writeln ! ( timezone_file " \" \" " zone zone_name raw_zone_name zone ) ? ; } writeln ! ( timezone_file " \n " ) ? ; writeln ! ( timezone_file " \" \" \n " ) ? ; writeln ! ( timezone_file " " ) ? ; for zone in & zones { let timespans = table . timespans ( & zone ) . unwrap ( ) ; let zone_name = convert_bad_chars ( zone ) ; writeln ! ( timezone_file " \" \" \n " zone zone_name rest format_rest ( timespans rest ) utc timespans first utc_offset dst timespans first dst_offset name timespans first name ) ? ; } write ! ( timezone_file " \n " ) ? ; write ! ( timezone_file " " num zones len ( ) ) ? ; for zone in & zones { writeln ! ( timezone_file " " zone convert_bad_chars ( zone ) ) ? ; } write ! ( timezone_file " " ) ? ; Ok ( ( ) ) }
The timezone file contains impls of `Timespans` for all timezones in the
database.
The timezone file contains impls of ` Timespans ` for all timezones in the database .
timezone_file
&mut File
table
&Table