jiff 1.1.0

A date-time library that encourages you to jump into the pit of success. This library is heavily inspired by the Temporal project.
Documentation
/*!
Provides a `Display` impl wrapper for POSIX time zones.

This lives here because none of the formatting machinery is available
in `jiff-core`. And while we could still write out a `Display` impl in
`jiff-core`, it doesn't seem worth that duplication.
*/

use jcore::tz::posix;

use crate::fmt;

/// A wrapper around jcore's posix time zone implementation.
///
/// This wrapper provides `Display`, `Debug` and `Format` impls using the
/// formatter in this crate. (jcore does not do formatting.)
pub(crate) struct TimeZoneFormatter<'a>(pub(crate) &'a posix::TimeZone);

impl<'a> core::fmt::Display for TimeZoneFormatter<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        fmt::temporal::DateTimePrinter::new()
            .print_posix_time_zone(self.0, fmt::StdFmtWrite(f))
            .map_err(|_| core::fmt::Error)
    }
}

impl<'a> core::fmt::Debug for TimeZoneFormatter<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        core::fmt::Display::fmt(self, f)
    }
}

#[cfg(feature = "defmt")]
impl<'a> defmt::Format for TimeZoneFormatter<'a> {
    fn format(&self, f: defmt::Formatter) {
        defmt::unwrap!(
            fmt::temporal::DateTimePrinter::new()
                .print_posix_time_zone(self.0, fmt::DefmtWrite(f))
        )
    }
}

// The tests below require parsing which requires alloc.
#[cfg(feature = "alloc")]
#[cfg(test)]
mod tests {
    use super::*;

    /// Regression test for <https://github.com/BurntSushi/jiff/issues/748>.
    #[test]
    fn fold_across_year_boundary() {
        let tz = crate::tz::TimeZone::posix("EST1EDT,M10.1.0,0/0").unwrap();
        let dt = crate::civil::date(2021, 12, 31).at(23, 30, 0, 0);
        let earlier = "2021-12-31T23:30:00Z".parse().unwrap();
        let later = "2022-01-01T00:30:00Z".parse().unwrap();
        assert_eq!(tz.to_offset(earlier), crate::tz::offset(0));
        assert_eq!(tz.to_offset(later), crate::tz::offset(-1));
        for ts in [earlier, later] {
            assert_eq!(tz.to_offset(ts).to_datetime(ts), dt);
        }
        let ambiguous = tz.to_ambiguous_timestamp(dt);
        assert_eq!(
            ambiguous.offset(),
            crate::tz::AmbiguousOffset::Fold {
                before: crate::tz::offset(0),
                after: crate::tz::offset(-1),
            },
        );
        assert_eq!(ambiguous.earlier().unwrap(), earlier);
        assert_eq!(ambiguous.later().unwrap(), later);
        assert!(ambiguous.unambiguous().is_err());
    }

    /// Regression test for <https://github.com/BurntSushi/jiff/issues/747>.
    #[test]
    fn transition_in_adjacent_utc_year() {
        let tz = crate::tz::TimeZone::posix("STD-10DST,J1/0,J60/0").unwrap();
        let start = "2025-07-01T00:00Z".parse().unwrap();
        let expected = "2025-12-31T14:00:00Z".parse().unwrap();
        let next = tz.following(start).next().unwrap();
        assert_eq!(next.timestamp(), expected);
        assert_eq!(next.offset(), crate::tz::offset(11));
        assert_eq!(next.abbreviation(), "DST");

        let before = expected - crate::SignedDuration::from_nanos(1);
        assert_eq!(tz.to_offset(before), crate::tz::offset(10));
        assert_eq!(tz.to_offset(expected), crate::tz::offset(11));

        let after = "2025-12-31T20:00Z".parse().unwrap();
        let info = tz.to_offset_info(after);
        assert_eq!(info.offset(), crate::tz::offset(11));
        assert_eq!(info.abbreviation(), "DST");
        assert!(info.dst().is_dst());
        assert_eq!(tz.preceding(after).next().unwrap().timestamp(), expected);
    }

    /// Regression test for <https://github.com/BurntSushi/jiff/issues/746>.
    #[test]
    fn all_year_dst() {
        for (spec, offset, abbreviation) in [
            ("XXX3EDT4,0/0,J365/23", crate::tz::offset(-4), "EDT"),
            ("XXX-2<+01>-1,0/0,J365/23", crate::tz::offset(1), "+01"),
        ] {
            let tz = crate::tz::TimeZone::posix(spec).unwrap();
            for input in [
                "2030-01-01T02:00Z",
                "2030-07-01T00:00Z",
                "2087-12-31T23:59:59.999999999Z",
                "2088-12-31T23:59:59.999999999Z",
            ] {
                let ts = input.parse().unwrap();
                let info = tz.to_offset_info(ts);
                assert_eq!(info.offset(), offset, "{spec}: {ts}");
                assert_eq!(info.abbreviation(), abbreviation);
                assert!(info.dst().is_dst());
                assert_eq!(tz.to_offset(ts), offset);
                assert!(tz.preceding(ts).next().is_none());
                assert!(tz.following(ts).next().is_none());
            }
        }
    }

    /// Assert that converting the TZ back to a string matches what we got. In
    /// the original version of the POSIX TZ parser, we were very meticulous
    /// about capturing the exact AST of the time zone. But I've since
    /// simplified the data structure considerably such that it is lossy in
    /// terms of what was actually parsed (but of course, not lossy in terms of
    /// the semantic meaning of the time zone).
    ///
    /// So to account for this, we serialize to a string and then parse it
    /// back. We should get what we started with.
    #[cfg(feature = "alloc")]
    #[test]
    fn roundtrips() {
        fn assert_roundtrip(tz: &str) {
            use alloc::string::{String, ToString};

            use jcore::tz::posix;

            fn to_string(tz: &posix::TimeZone) -> String {
                TimeZoneFormatter(tz).to_string()
            }

            let tz = posix::TimeZone::parse(tz).unwrap();
            let reparsed = posix::TimeZone::parse(to_string(&tz)).unwrap();
            assert_eq!(tz, reparsed);
            assert_eq!(to_string(&tz), to_string(&reparsed));
        }

        let cases = [
            "WART4WARST,J1/-3,J365/20",
            "WART4WARST,J1/-4,J365/21",
            "EST5EDT,M3.2.0,M11.1.0",
            "XXX-2<+01>-1,0/0,J365/23",
            "EST24EDT,J1,J365",
            "EST-24EDT,J1,J365",
            "EST5EDT,J1,J365/5:12:34",
            "EST+5EDT,M3.2.0/2,M11.1.0/2",
            "EST+5EDT,M1.1.1,M12.5.2",
            "EST5EDT,0/0,J365/25",
            "XXX3EDT4,0/0,J365/23",
            "XXX3EDT4,0/0,365",
            "XXX3EDT4,J1/-167:59:59,J365/167:59:59",
            "EST5EDT,J1,J365/5:12:34",
            "EST+5EDT,M3.2.0/2,M11.1.0/2",
            "EST+5EDT,J60,J365",
            "EST+5EDT,59,365",
            "EST+5EDT,M1.1.1,M12.5.2",
            "EST5EDT,J1,J365/5:12:34",
            "EST5EDT,J1/23:59:59,J365/24:00:00",
            "EST5EDT,J1/-1,J365/167:00:00",
        ];
        for tz in cases {
            assert_roundtrip(tz);
        }
    }
}