1use jcore::tz::posix;
10
11use crate::fmt;
12
13pub(crate) struct TimeZoneFormatter<'a>(pub(crate) &'a posix::TimeZone);
18
19impl<'a> core::fmt::Display for TimeZoneFormatter<'a> {
20 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
21 fmt::temporal::DateTimePrinter::new()
22 .print_posix_time_zone(self.0, fmt::StdFmtWrite(f))
23 .map_err(|_| core::fmt::Error)
24 }
25}
26
27impl<'a> core::fmt::Debug for TimeZoneFormatter<'a> {
28 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
29 core::fmt::Display::fmt(self, f)
30 }
31}
32
33#[cfg(feature = "defmt")]
34impl<'a> defmt::Format for TimeZoneFormatter<'a> {
35 fn format(&self, f: defmt::Formatter) {
36 defmt::unwrap!(
37 fmt::temporal::DateTimePrinter::new()
38 .print_posix_time_zone(self.0, fmt::DefmtWrite(f))
39 )
40 }
41}
42
43#[cfg(feature = "alloc")]
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[cfg(feature = "alloc")]
59 #[test]
60 fn roundtrips() {
61 fn assert_roundtrip(tz: &str) {
62 use alloc::string::{String, ToString};
63
64 use jcore::tz::posix;
65
66 fn to_string(tz: &posix::TimeZone) -> String {
67 TimeZoneFormatter(tz).to_string()
68 }
69
70 let tz = posix::TimeZone::parse(tz).unwrap();
71 let reparsed = posix::TimeZone::parse(to_string(&tz)).unwrap();
72 assert_eq!(tz, reparsed);
73 assert_eq!(to_string(&tz), to_string(&reparsed));
74 }
75
76 let cases = [
77 "WART4WARST,J1/-3,J365/20",
78 "WART4WARST,J1/-4,J365/21",
79 "EST5EDT,M3.2.0,M11.1.0",
80 "XXX-2<+01>-1,0/0,J365/23",
81 "EST24EDT,J1,J365",
82 "EST-24EDT,J1,J365",
83 "EST5EDT,J1,J365/5:12:34",
84 "EST+5EDT,M3.2.0/2,M11.1.0/2",
85 "EST+5EDT,M1.1.1,M12.5.2",
86 "EST5EDT,0/0,J365/25",
87 "XXX3EDT4,0/0,J365/23",
88 "XXX3EDT4,0/0,365",
89 "XXX3EDT4,J1/-167:59:59,J365/167:59:59",
90 "EST5EDT,J1,J365/5:12:34",
91 "EST+5EDT,M3.2.0/2,M11.1.0/2",
92 "EST+5EDT,J60,J365",
93 "EST+5EDT,59,365",
94 "EST+5EDT,M1.1.1,M12.5.2",
95 "EST5EDT,J1,J365/5:12:34",
96 "EST5EDT,J1/23:59:59,J365/24:00:00",
97 "EST5EDT,J1/-1,J365/167:00:00",
98 ];
99 for tz in cases {
100 assert_roundtrip(tz);
101 }
102 }
103}