Skip to main content

refix_message/interop/
jiff.rs

1use super::exact_precision;
2use crate::{Date, OutOfRange, Time, UtcTimestamp};
3use jiff_02::{Timestamp, civil};
4
5impl From<Date> for civil::Date {
6    fn from(date: Date) -> Self {
7        civil::Date::new(date.year() as i16, date.month() as i8, date.day() as i8)
8            .expect("years 0 to 9999 are within jiff's range")
9    }
10}
11
12impl TryFrom<civil::Date> for Date {
13    type Error = OutOfRange;
14
15    fn try_from(date: civil::Date) -> Result<Self, Self::Error> {
16        u16::try_from(date.year())
17            .ok()
18            .and_then(|year| Date::new(year, date.month() as u8, date.day() as u8))
19            .ok_or(OutOfRange)
20    }
21}
22
23impl From<Time> for civil::Time {
24    /// jiff has no leap second, so `23:59:60` clamps to `23:59:59.999999999`.
25    fn from(time: Time) -> Self {
26        let nanos = time.nanos_since_midnight().min(86_399_999_999_999);
27        let seconds = nanos / 1_000_000_000;
28        civil::Time::new(
29            (seconds / 3_600) as i8,
30            (seconds / 60 % 60) as i8,
31            (seconds % 60) as i8,
32            (nanos % 1_000_000_000) as i32,
33        )
34        .expect("the clamped time is within a day")
35    }
36}
37
38impl From<civil::Time> for Time {
39    fn from(time: civil::Time) -> Self {
40        let seconds = i64::from(time.hour()) * 3_600
41            + i64::from(time.minute()) * 60
42            + i64::from(time.second());
43        let nanos = seconds * 1_000_000_000 + i64::from(time.subsec_nanosecond());
44        Time::new(nanos, exact_precision(nanos)).expect("a jiff time is within a day")
45    }
46}
47
48impl From<UtcTimestamp> for Timestamp {
49    fn from(timestamp: UtcTimestamp) -> Self {
50        Timestamp::from_nanosecond(i128::from(timestamp.unix_nanos()))
51            .expect("an i64 of nanoseconds is within jiff's range")
52    }
53}
54
55impl TryFrom<Timestamp> for UtcTimestamp {
56    type Error = OutOfRange;
57
58    fn try_from(timestamp: Timestamp) -> Result<Self, Self::Error> {
59        let nanos = i64::try_from(timestamp.as_nanosecond()).map_err(|_| OutOfRange)?;
60        Ok(UtcTimestamp::new(nanos, exact_precision(nanos)))
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::TimePrecision;
68
69    mod date {
70        use super::*;
71
72        #[test]
73        fn converts_to_jiff() {
74            let date = Date::new(2026, 10, 30).unwrap();
75            assert_eq!(civil::Date::from(date), civil::date(2026, 10, 30));
76        }
77
78        #[test]
79        fn converts_from_jiff() {
80            let date = Date::new(2026, 10, 30).unwrap();
81            assert_eq!(Date::try_from(civil::date(2026, 10, 30)), Ok(date));
82        }
83
84        #[test]
85        fn round_trips_the_first_and_last_fix_years() {
86            for date in [
87                Date::new(0, 1, 1).unwrap(),
88                Date::new(9999, 12, 31).unwrap(),
89            ] {
90                assert_eq!(Date::try_from(civil::Date::from(date)), Ok(date));
91            }
92        }
93
94        #[test]
95        fn rejects_a_year_before_zero() {
96            assert_eq!(Date::try_from(civil::date(-1, 12, 31)), Err(OutOfRange));
97        }
98    }
99
100    mod time {
101        use super::*;
102
103        #[test]
104        fn converts_to_jiff() {
105            let time = Time::new(52_191_123_000_000, TimePrecision::MILLIS).unwrap();
106            assert_eq!(
107                civil::Time::from(time),
108                civil::time(14, 29, 51, 123_000_000)
109            );
110        }
111
112        #[test]
113        fn converts_from_jiff() {
114            let time = Time::from(civil::time(14, 29, 51, 123_000_000));
115            assert_eq!(time.nanos_since_midnight(), 52_191_123_000_000);
116        }
117
118        #[test]
119        fn round_trips_the_first_and_last_nanoseconds_of_the_day() {
120            for jiff in [civil::Time::MIN, civil::Time::MAX] {
121                assert_eq!(civil::Time::from(Time::from(jiff)), jiff);
122            }
123        }
124
125        #[test]
126        fn clamps_a_leap_second_to_the_end_of_the_day() {
127            let leap = Time::new(86_400_500_000_000, TimePrecision::MILLIS).unwrap();
128            assert_eq!(civil::Time::from(leap), civil::Time::MAX);
129        }
130
131        #[test]
132        fn takes_the_coarsest_exact_precision_from_jiff() {
133            for (subsec_nanosecond, precision) in [
134                (0, TimePrecision::SECONDS),
135                (100_000_000, TimePrecision::MILLIS),
136                (123_000_000, TimePrecision::MILLIS),
137                (123_456_000, TimePrecision::MICROS),
138                (123_456_789, TimePrecision::NANOS),
139            ] {
140                let time = Time::from(civil::time(14, 29, 51, subsec_nanosecond));
141                assert_eq!(time.precision(), precision, "{subsec_nanosecond}");
142            }
143        }
144    }
145
146    mod timestamp {
147        use super::*;
148
149        #[test]
150        fn converts_to_jiff() {
151            let timestamp = UtcTimestamp::new(1_791_451_800_123_000_000, TimePrecision::MILLIS);
152            assert_eq!(
153                Timestamp::from(timestamp),
154                Timestamp::new(1_791_451_800, 123_000_000).unwrap()
155            );
156        }
157
158        #[test]
159        fn converts_from_jiff() {
160            let jiff = Timestamp::new(1_791_451_800, 123_000_000).unwrap();
161            let timestamp = UtcTimestamp::try_from(jiff).unwrap();
162            assert_eq!(timestamp.unix_nanos(), 1_791_451_800_123_000_000);
163            assert_eq!(timestamp.precision(), TimePrecision::MILLIS);
164        }
165
166        #[test]
167        fn round_trips_the_ends_of_an_i64() {
168            for nanos in [i64::MIN, i64::MAX] {
169                let timestamp = UtcTimestamp::new(nanos, TimePrecision::NANOS);
170                assert_eq!(
171                    UtcTimestamp::try_from(Timestamp::from(timestamp)),
172                    Ok(timestamp)
173                );
174            }
175        }
176
177        #[test]
178        fn rejects_a_timestamp_beyond_an_i64_of_nanoseconds() {
179            let past_the_end = Timestamp::from_nanosecond(i128::from(i64::MAX) + 1).unwrap();
180            for jiff in [past_the_end, Timestamp::MIN, Timestamp::MAX] {
181                assert_eq!(UtcTimestamp::try_from(jiff), Err(OutOfRange));
182            }
183        }
184
185        #[test]
186        fn takes_the_coarsest_exact_precision_before_the_epoch() {
187            let jiff = Timestamp::from_nanosecond(-500_000_000).unwrap();
188            let timestamp = UtcTimestamp::try_from(jiff).unwrap();
189            assert_eq!(timestamp.precision(), TimePrecision::MILLIS);
190        }
191    }
192}