sql_values/datetime/
jiff_conversions.rs1use jiff::{
6 Timestamp,
7 civil::{Date, DateTime, Time},
8};
9
10use super::{
11 DateTimeError, PgDate, PgTime, PgTimestamp, PgTimestampTz, USECS_PER_DAY, USECS_PER_HOUR,
12 USECS_PER_MINUTE, USECS_PER_SEC, is_valid_timestamp, split_clock, tm2timestamp,
13};
14
15const UNIX_TO_POSTGRES_MICROS: i64 = 946_684_800_000_000;
17
18fn round_nanos(nanos: i128) -> i128 {
20 let (micros, remainder) = (nanos.div_euclid(1_000), nanos.rem_euclid(1_000));
21 if remainder > 500 || (remainder == 500 && micros % 2 != 0) {
22 micros + 1
23 } else {
24 micros
25 }
26}
27
28fn time_micros(time: Time) -> i64 {
30 let nanos = i128::from(time.hour()) * i128::from(USECS_PER_HOUR) * 1_000
31 + i128::from(time.minute()) * i128::from(USECS_PER_MINUTE) * 1_000
32 + i128::from(time.second()) * i128::from(USECS_PER_SEC) * 1_000
33 + i128::from(time.subsec_nanosecond());
34 i64::try_from(round_nanos(nanos)).expect("a day's microseconds fit i64")
35}
36
37fn jiff_year(year: i32) -> Option<i16> {
39 i16::try_from(year)
40 .ok()
41 .filter(|year| (-9999..=9999).contains(year))
42}
43
44fn jiff_date(year: i32, month: i32, day: i32) -> Result<Date, DateTimeError> {
46 let year = jiff_year(year).ok_or(DateTimeError::OutsideJiffRange)?;
47 let month = i8::try_from(month).expect("a month fits i8");
48 let day = i8::try_from(day).expect("a day fits i8");
49 Date::new(year, month, day).map_err(|_| DateTimeError::OutsideJiffRange)
50}
51
52fn jiff_time(micros: i64) -> Time {
54 let (hour, minute, second, micro) = split_clock(micros);
55 let narrow = |field: i64| i8::try_from(field).expect("a clock field below 24:00:00 fits i8");
56 let nanos = i32::try_from(micro * 1_000).expect("a second's nanoseconds fit i32");
57 Time::new(narrow(hour), narrow(minute), narrow(second), nanos)
58 .expect("a clock below 24:00:00 is a jiff time")
59}
60
61impl TryFrom<PgDate> for Date {
63 type Error = DateTimeError;
64
65 fn try_from(date: PgDate) -> Result<Self, Self::Error> {
66 let (year, month, day) = date.ymd().ok_or(DateTimeError::OutsideJiffRange)?;
67 jiff_date(year, month.into(), day.into())
68 }
69}
70
71impl TryFrom<Date> for PgDate {
73 type Error = DateTimeError;
74
75 fn try_from(date: Date) -> Result<Self, Self::Error> {
76 let month = u8::try_from(date.month()).expect("a jiff month is positive");
77 let day = u8::try_from(date.day()).expect("a jiff day is positive");
78 Self::from_ymd(date.year().into(), month, day).ok_or(DateTimeError::DateOutOfRange)
79 }
80}
81
82impl TryFrom<PgTime> for Time {
84 type Error = DateTimeError;
85
86 fn try_from(time: PgTime) -> Result<Self, Self::Error> {
87 if time.0 == USECS_PER_DAY {
88 return Err(DateTimeError::FieldValueOutOfRange);
89 }
90 Ok(jiff_time(time.0))
91 }
92}
93
94impl From<Time> for PgTime {
96 fn from(time: Time) -> Self {
97 Self(time_micros(time))
98 }
99}
100
101impl TryFrom<PgTimestamp> for DateTime {
103 type Error = DateTimeError;
104
105 fn try_from(at: PgTimestamp) -> Result<Self, Self::Error> {
106 let date = Date::try_from(at.date())?;
107 let time = at.time().ok_or(DateTimeError::OutsideJiffRange)?;
108 Ok(Self::from_parts(date, jiff_time(time.0)))
109 }
110}
111
112impl TryFrom<DateTime> for PgTimestamp {
114 type Error = DateTimeError;
115
116 fn try_from(at: DateTime) -> Result<Self, Self::Error> {
117 let (year, month, day) = (
118 i32::from(at.year()),
119 i32::from(at.month()),
120 i32::from(at.day()),
121 );
122 tm2timestamp(year, month, day, time_micros(at.time()), 0)
123 .map(Self)
124 .ok_or(DateTimeError::TimestampOutOfRange)
125 }
126}
127
128impl TryFrom<PgTimestampTz> for Timestamp {
130 type Error = DateTimeError;
131
132 fn try_from(at: PgTimestampTz) -> Result<Self, Self::Error> {
133 if !at.is_finite() {
134 return Err(DateTimeError::OutsideJiffRange);
135 }
136 at.0.checked_add(UNIX_TO_POSTGRES_MICROS)
138 .and_then(|micros| Self::from_microsecond(micros).ok())
139 .ok_or(DateTimeError::OutsideJiffRange)
140 }
141}
142
143impl TryFrom<Timestamp> for PgTimestampTz {
145 type Error = DateTimeError;
146
147 fn try_from(at: Timestamp) -> Result<Self, Self::Error> {
148 let micros = i64::try_from(round_nanos(at.as_nanosecond()))
149 .expect("jiff's instants lie within i64 microseconds")
150 - UNIX_TO_POSTGRES_MICROS;
151 if is_valid_timestamp(micros) {
152 Ok(Self(micros))
153 } else {
154 Err(DateTimeError::TimestampOutOfRange)
155 }
156 }
157}