windows_timezones/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4mod generated;
5
6pub use generated::WindowsTimezone;
7
8#[cfg(feature = "std")]
9impl std::fmt::Display for generated::WindowsTimezone {
10    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11        self.name().fmt(f)
12    }
13}
14
15#[cfg(feature = "std")]
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17/// An error returned when parsing a [`WindowsTimezone`] using [`FromStr::from_str`](std::str::FromStr::from_str) fails.
18pub struct ParseWindowsTimezoneError;
19
20#[cfg(feature = "std")]
21impl std::fmt::Display for ParseWindowsTimezoneError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        "the given string was not a known Windows timezone identifier".fmt(f)
24    }
25}
26
27#[cfg(feature = "std")]
28impl std::error::Error for ParseWindowsTimezoneError {}
29
30#[cfg(all(
31    feature = "std",
32    any(feature = "chrono-tz-0_9", feature = "chrono-tz-0_10")
33))]
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35/// An error returned when failing to convert a `chrono_tz::Tz` to a [`WindowsTimezone`] using [`TryFrom::try_from`].
36pub struct FromChronoTzError;
37
38#[cfg(all(
39    feature = "std",
40    any(feature = "chrono-tz-0_9", feature = "chrono-tz-0_10")
41))]
42impl std::fmt::Display for FromChronoTzError {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        "there is no corresponding Windows timezone identifier for the given IANA timezone".fmt(f)
45    }
46}
47
48#[cfg(all(
49    feature = "std",
50    any(feature = "chrono-tz-0_9", feature = "chrono-tz-0_10")
51))]
52impl std::error::Error for FromChronoTzError {}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn it_works() {
60        let tz = WindowsTimezone::WEuropeStandardTime;
61
62        let description = tz.description();
63        assert!(description.starts_with("(UTC+01:00)"));
64        assert!(description.contains("Berlin"));
65        assert!(description.contains("Stockholm"));
66
67        assert_eq!(tz.tzdb_id(), "Europe/Berlin");
68    }
69
70    #[cfg(feature = "std")]
71    #[test]
72    fn from_str() {
73        assert_eq!(
74            "W. Europe Standard Time".parse::<WindowsTimezone>(),
75            Ok(WindowsTimezone::WEuropeStandardTime),
76        );
77
78        assert_eq!(
79            WindowsTimezone::WEuropeStandardTime.name(),
80            "W. Europe Standard Time",
81        );
82
83        assert_eq!(
84            "I am a teapot".parse::<WindowsTimezone>(),
85            Err(ParseWindowsTimezoneError),
86        );
87    }
88
89    #[cfg(feature = "chrono-tz-0_9")]
90    #[test]
91    fn chrono_tz_0_9_conversion() {
92        assert_eq!(
93            Into::<chrono_tz_0_9::Tz>::into(WindowsTimezone::WEuropeStandardTime),
94            chrono_tz_0_9::Tz::Europe__Berlin
95        );
96        assert_eq!(
97            WindowsTimezone::try_from(chrono_tz_0_9::Tz::Europe__Berlin),
98            Ok(WindowsTimezone::WEuropeStandardTime)
99        );
100    }
101
102    #[cfg(feature = "chrono-tz-0_10")]
103    #[test]
104    fn chrono_tz_0_10_conversion() {
105        assert_eq!(
106            Into::<chrono_tz_0_10::Tz>::into(WindowsTimezone::WEuropeStandardTime),
107            chrono_tz_0_10::Tz::Europe__Berlin
108        );
109        assert_eq!(
110            WindowsTimezone::try_from(chrono_tz_0_10::Tz::Europe__Berlin),
111            Ok(WindowsTimezone::WEuropeStandardTime)
112        );
113    }
114
115    #[cfg(feature = "strum")]
116    #[test]
117    fn strum() {
118        use strum::IntoEnumIterator;
119
120        let tzs = WindowsTimezone::iter().collect::<Vec<_>>();
121        assert!(tzs.contains(&WindowsTimezone::WEuropeStandardTime));
122    }
123}