| 1 |
/*! |
| 2 |
Provides a `Display` impl wrapper for POSIX time zones. |
| 3 |
|
| 4 |
This lives here because none of the formatting machinery is available |
| 5 |
in `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 |
|
| 9 |
use jcore::tz::posix; |
| 10 |
|
| 11 |
use 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.) |
| 17 |
pub(crate) struct TimeZoneFormatter<'a>(pub(crate) &'a posix::TimeZone); |
| 18 |
|
| 19 |
impl<'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 |
|
| 27 |
impl<'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")] |
| 34 |
impl<'a> defmt::Format for TimeZoneFormatter<'a> { |
| 35 |
fn format(&self, f: defmt::Formatter) { |
| 36 |
defmt::unwrap!(fmt::temporal::DateTimePrinter::new() |
| 37 |
.print_posix_time_zone(self.0, fmt::DefmtWrite(f))) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// The tests below require parsing which requires alloc. |
| 42 |
#[cfg(feature = "alloc")] |
| 43 |
#[cfg(test)] |
| 44 |
mod tests { |
| 45 |
use super::*; |
| 46 |
|
| 47 |
/// Assert that converting the TZ back to a string matches what we got. In |
| 48 |
/// the original version of the POSIX TZ parser, we were very meticulous |
| 49 |
/// about capturing the exact AST of the time zone. But I've since |
| 50 |
/// simplified the data structure considerably such that it is lossy in |
| 51 |
/// terms of what was actually parsed (but of course, not lossy in terms of |
| 52 |
/// the semantic meaning of the time zone). |
| 53 |
/// |
| 54 |
/// So to account for this, we serialize to a string and then parse it |
| 55 |
/// back. We should get what we started with. |
| 56 |
#[cfg(feature = "alloc")] |
| 57 |
#[test] |
| 58 |
fn roundtrips() { |
| 59 |
fn assert_roundtrip(tz: &str) { |
| 60 |
use alloc::string::{String, ToString}; |
| 61 |
|
| 62 |
use jcore::tz::posix; |
| 63 |
|
| 64 |
fn to_string(tz: &posix::TimeZone) -> String { |
| 65 |
TimeZoneFormatter(tz).to_string() |
| 66 |
} |
| 67 |
|
| 68 |
let tz = posix::TimeZone::parse(tz).unwrap(); |
| 69 |
let reparsed = posix::TimeZone::parse(to_string(&tz)).unwrap(); |
| 70 |
assert_eq!(tz, reparsed); |
| 71 |
assert_eq!(to_string(&tz), to_string(&reparsed)); |
| 72 |
} |
| 73 |
|
| 74 |
let cases = [ |
| 75 |
"WART4WARST,J1/-3,J365/20", |
| 76 |
"WART4WARST,J1/-4,J365/21", |
| 77 |
"EST5EDT,M3.2.0,M11.1.0", |
| 78 |
"XXX-2<+01>-1,0/0,J365/23", |
| 79 |
"EST24EDT,J1,J365", |
| 80 |
"EST-24EDT,J1,J365", |
| 81 |
"EST5EDT,J1,J365/5:12:34", |
| 82 |
"EST+5EDT,M3.2.0/2,M11.1.0/2", |
| 83 |
"EST+5EDT,M1.1.1,M12.5.2", |
| 84 |
"EST5EDT,0/0,J365/25", |
| 85 |
"XXX3EDT4,0/0,J365/23", |
| 86 |
"XXX3EDT4,0/0,365", |
| 87 |
"XXX3EDT4,J1/-167:59:59,J365/167:59:59", |
| 88 |
"EST5EDT,J1,J365/5:12:34", |
| 89 |
"EST+5EDT,M3.2.0/2,M11.1.0/2", |
| 90 |
"EST+5EDT,J60,J365", |
| 91 |
"EST+5EDT,59,365", |
| 92 |
"EST+5EDT,M1.1.1,M12.5.2", |
| 93 |
"EST5EDT,J1,J365/5:12:34", |
| 94 |
"EST5EDT,J1/23:59:59,J365/24:00:00", |
| 95 |
"EST5EDT,J1/-1,J365/167:00:00", |
| 96 |
]; |
| 97 |
for tz in cases { |
| 98 |
assert_roundtrip(tz); |
| 99 |
} |
| 100 |
} |
| 101 |
} |