Skip to main content

refix_message/interop/
time.rs

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