sqlx_postgres/types/chrono/
datetime.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 chrono::{
7    DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime, Offset, TimeZone, Utc,
8};
9use std::mem;
10
11impl Type<Postgres> for NaiveDateTime {
12    fn type_info() -> PgTypeInfo {
13        PgTypeInfo::TIMESTAMP
14    }
15}
16
17impl<Tz: TimeZone> Type<Postgres> for DateTime<Tz> {
18    fn type_info() -> PgTypeInfo {
19        PgTypeInfo::TIMESTAMPTZ
20    }
21}
22
23impl PgHasArrayType for NaiveDateTime {
24    fn array_type_info() -> PgTypeInfo {
25        PgTypeInfo::TIMESTAMP_ARRAY
26    }
27}
28
29impl<Tz: TimeZone> PgHasArrayType for DateTime<Tz> {
30    fn array_type_info() -> PgTypeInfo {
31        PgTypeInfo::TIMESTAMPTZ_ARRAY
32    }
33}
34
35impl Encode<'_, Postgres> for NaiveDateTime {
36    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
37        // FIXME: We should *really* be returning an error, Encode needs to be fallible
38        // TIMESTAMP is encoded as the microseconds since the epoch
39        let us = (*self - postgres_epoch_datetime())
40            .num_microseconds()
41            .unwrap_or_else(|| panic!("NaiveDateTime out of range for Postgres: {self:?}"));
42
43        Encode::<Postgres>::encode(&us, buf)
44    }
45
46    fn size_hint(&self) -> usize {
47        mem::size_of::<i64>()
48    }
49}
50
51impl<'r> Decode<'r, Postgres> for NaiveDateTime {
52    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
53        Ok(match value.format() {
54            PgValueFormat::Binary => {
55                // TIMESTAMP is encoded as the microseconds since the epoch
56                let us = Decode::<Postgres>::decode(value)?;
57                postgres_epoch_datetime() + Duration::microseconds(us)
58            }
59
60            PgValueFormat::Text => {
61                let s = value.as_str()?;
62                NaiveDateTime::parse_from_str(
63                    s,
64                    if s.contains('+') {
65                        // Contains a time-zone specifier
66                        // This is given for timestamptz for some reason
67                        // Postgres already guarantees this to always be UTC
68                        "%Y-%m-%d %H:%M:%S%.f%#z"
69                    } else {
70                        "%Y-%m-%d %H:%M:%S%.f"
71                    },
72                )?
73            }
74        })
75    }
76}
77
78impl<Tz: TimeZone> Encode<'_, Postgres> for DateTime<Tz> {
79    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
80        Encode::<Postgres>::encode(self.naive_utc(), buf)
81    }
82
83    fn size_hint(&self) -> usize {
84        mem::size_of::<i64>()
85    }
86}
87
88impl<'r> Decode<'r, Postgres> for DateTime<Local> {
89    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
90        let naive = <NaiveDateTime as Decode<Postgres>>::decode(value)?;
91        Ok(Local.from_utc_datetime(&naive))
92    }
93}
94
95impl<'r> Decode<'r, Postgres> for DateTime<Utc> {
96    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
97        let naive = <NaiveDateTime as Decode<Postgres>>::decode(value)?;
98        Ok(Utc.from_utc_datetime(&naive))
99    }
100}
101
102impl<'r> Decode<'r, Postgres> for DateTime<FixedOffset> {
103    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
104        let naive = <NaiveDateTime as Decode<Postgres>>::decode(value)?;
105        Ok(Utc.fix().from_utc_datetime(&naive))
106    }
107}
108
109#[inline]
110fn postgres_epoch_datetime() -> NaiveDateTime {
111    NaiveDate::from_ymd_opt(2000, 1, 1)
112        .expect("expected 2000-01-01 to be a valid NaiveDate")
113        .and_hms_opt(0, 0, 0)
114        .expect("expected 2000-01-01T00:00:00 to be a valid NaiveDateTime")
115}