Skip to main content

jiff/tz/
posix.rs

1/*!
2Provides a `Display` impl wrapper for POSIX time zones.
3
4This lives here because none of the formatting machinery is available
5in `jiff-core`. And while we could still write out a `Display` impl in
6`jiff-core`, it doesn't seem worth that duplication.
7*/
8
9use jcore::tz::posix;
10
11use crate::fmt;
12
13/// A wrapper around jcore's posix time zone implementation.
14///
15/// This wrapper provides `Display`, `Debug` and `Format` impls using the
16/// formatter in this crate. (jcore does not do formatting.)
17pub(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// The tests below require parsing which requires alloc.
44#[cfg(feature = "alloc")]
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    /// Assert that converting the TZ back to a string matches what we got. In
50    /// the original version of the POSIX TZ parser, we were very meticulous
51    /// about capturing the exact AST of the time zone. But I've since
52    /// simplified the data structure considerably such that it is lossy in
53    /// terms of what was actually parsed (but of course, not lossy in terms of
54    /// the semantic meaning of the time zone).
55    ///
56    /// So to account for this, we serialize to a string and then parse it
57    /// back. We should get what we started with.
58    #[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}