sqlx_postgres/types/
time_tz.rs

1use crate::decode::Decode;
2use crate::encode::{Encode, IsNull};
3use crate::error::BoxDynError;
4use crate::types::Type;
5use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
6use byteorder::{BigEndian, ReadBytesExt};
7use std::io::Cursor;
8use std::mem;
9
10#[cfg(feature = "time")]
11type DefaultTime = ::time::Time;
12
13#[cfg(all(not(feature = "time"), feature = "chrono"))]
14type DefaultTime = ::chrono::NaiveTime;
15
16#[cfg(feature = "time")]
17type DefaultOffset = ::time::UtcOffset;
18
19#[cfg(all(not(feature = "time"), feature = "chrono"))]
20type DefaultOffset = ::chrono::FixedOffset;
21
22/// Represents a moment of time, in a specified timezone.
23///
24/// # Warning
25///
26/// `PgTimeTz` provides `TIMETZ` and is supported only for reading from legacy databases.
27/// [PostgreSQL recommends] to use `TIMESTAMPTZ` instead.
28///
29/// [PostgreSQL recommends]: https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_timetz
30#[derive(Debug, PartialEq, Clone, Copy)]
31pub struct PgTimeTz<Time = DefaultTime, Offset = DefaultOffset> {
32    pub time: Time,
33    pub offset: Offset,
34}
35
36impl<Time, Offset> PgHasArrayType for PgTimeTz<Time, Offset> {
37    fn array_type_info() -> PgTypeInfo {
38        PgTypeInfo::TIMETZ_ARRAY
39    }
40}
41
42#[cfg(feature = "chrono")]
43mod chrono {
44    use super::*;
45    use ::chrono::{DateTime, Duration, FixedOffset, NaiveTime};
46
47    impl Type<Postgres> for PgTimeTz<NaiveTime, FixedOffset> {
48        fn type_info() -> PgTypeInfo {
49            PgTypeInfo::TIMETZ
50        }
51    }
52
53    impl Encode<'_, Postgres> for PgTimeTz<NaiveTime, FixedOffset> {
54        fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
55            let _ = <NaiveTime as Encode<'_, Postgres>>::encode(self.time, buf);
56            let _ = <i32 as Encode<'_, Postgres>>::encode(self.offset.utc_minus_local(), buf);
57
58            IsNull::No
59        }
60
61        fn size_hint(&self) -> usize {
62            mem::size_of::<i64>() + mem::size_of::<i32>()
63        }
64    }
65
66    impl<'r> Decode<'r, Postgres> for PgTimeTz<NaiveTime, FixedOffset> {
67        fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
68            match value.format() {
69                PgValueFormat::Binary => {
70                    let mut buf = Cursor::new(value.as_bytes()?);
71
72                    // TIME is encoded as the microseconds since midnight
73                    let us = buf.read_i64::<BigEndian>()?;
74                    // default is midnight, there is a canary test for this
75                    // in `sqlx-postgres/src/types/chrono/time.rs`
76                    let time = NaiveTime::default() + Duration::microseconds(us);
77
78                    // OFFSET is encoded as seconds from UTC
79                    let offset_seconds = buf.read_i32::<BigEndian>()?;
80
81                    let offset = FixedOffset::west_opt(offset_seconds).ok_or_else(|| {
82                        format!(
83                            "server returned out-of-range offset for `TIMETZ`: {offset_seconds} seconds"
84                        )
85                    })?;
86
87                    Ok(PgTimeTz { time, offset })
88                }
89
90                PgValueFormat::Text => {
91                    let s = value.as_str()?;
92
93                    let mut tmp = String::with_capacity(11 + s.len());
94                    tmp.push_str("2001-07-08 ");
95                    tmp.push_str(s);
96
97                    let dt = 'out: loop {
98                        let mut err = None;
99
100                        for fmt in &["%Y-%m-%d %H:%M:%S%.f%#z", "%Y-%m-%d %H:%M:%S%.f"] {
101                            match DateTime::parse_from_str(&tmp, fmt) {
102                                Ok(dt) => {
103                                    break 'out dt;
104                                }
105
106                                Err(error) => {
107                                    err = Some(error);
108                                }
109                            }
110                        }
111
112                        return Err(err.unwrap().into());
113                    };
114
115                    let time = dt.time();
116                    let offset = *dt.offset();
117
118                    Ok(PgTimeTz { time, offset })
119                }
120            }
121        }
122    }
123}
124
125#[cfg(feature = "time")]
126mod time {
127    use super::*;
128    use ::time::{Duration, Time, UtcOffset};
129
130    impl Type<Postgres> for PgTimeTz<Time, UtcOffset> {
131        fn type_info() -> PgTypeInfo {
132            PgTypeInfo::TIMETZ
133        }
134    }
135
136    impl Encode<'_, Postgres> for PgTimeTz<Time, UtcOffset> {
137        fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
138            let _ = <Time as Encode<'_, Postgres>>::encode(self.time, buf);
139            let _ = <i32 as Encode<'_, Postgres>>::encode(-self.offset.whole_seconds(), buf);
140
141            IsNull::No
142        }
143
144        fn size_hint(&self) -> usize {
145            mem::size_of::<i64>() + mem::size_of::<i32>()
146        }
147    }
148
149    impl<'r> Decode<'r, Postgres> for PgTimeTz<Time, UtcOffset> {
150        fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
151            match value.format() {
152                PgValueFormat::Binary => {
153                    let mut buf = Cursor::new(value.as_bytes()?);
154
155                    // TIME is encoded as the microseconds since midnight
156                    let us = buf.read_i64::<BigEndian>()?;
157                    let time = Time::MIDNIGHT + Duration::microseconds(us);
158
159                    // OFFSET is encoded as seconds from UTC
160                    let seconds = buf.read_i32::<BigEndian>()?;
161
162                    Ok(PgTimeTz {
163                        time,
164                        offset: -UtcOffset::from_whole_seconds(seconds)?,
165                    })
166                }
167
168                PgValueFormat::Text => {
169                    // the `time` crate has a limited ability to parse and can't parse the
170                    // timezone format
171                    Err("reading a `TIMETZ` value in text format is not supported.".into())
172                }
173            }
174        }
175    }
176}