use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::Type; use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; use chrono::{ DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime, Offset, TimeZone, Utc, }; use std::mem; impl Type for NaiveDateTime { fn type_info() -> PgTypeInfo { PgTypeInfo::TIMESTAMP } } impl Type for DateTime { fn type_info() -> PgTypeInfo { PgTypeInfo::TIMESTAMPTZ } } impl PgHasArrayType for NaiveDateTime { fn array_type_info() -> PgTypeInfo { PgTypeInfo::TIMESTAMP_ARRAY } } impl PgHasArrayType for DateTime { fn array_type_info() -> PgTypeInfo { PgTypeInfo::TIMESTAMPTZ_ARRAY } } impl Encode<'_, Postgres> for NaiveDateTime { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { // TIMESTAMP is encoded as the microseconds since the epoch let micros = (*self - postgres_epoch_datetime()) .num_microseconds() .ok_or_else(|| format!("NaiveDateTime out of range for Postgres: {self:?}"))?; Encode::::encode(micros, buf) } fn size_hint(&self) -> usize { mem::size_of::() } } impl<'r> Decode<'r, Postgres> for NaiveDateTime { fn decode(value: PgValueRef<'r>) -> Result { Ok(match value.format() { PgValueFormat::Binary => { // TIMESTAMP is encoded as the microseconds since the epoch let us = Decode::::decode(value)?; postgres_epoch_datetime() + Duration::microseconds(us) } PgValueFormat::Text => { let s = value.as_str()?; NaiveDateTime::parse_from_str( s, if s.contains('+') { // Contains a time-zone specifier // This is given for timestamptz for some reason // Postgres already guarantees this to always be UTC "%Y-%m-%d %H:%M:%S%.f%#z" } else { "%Y-%m-%d %H:%M:%S%.f" }, )? } }) } } impl Encode<'_, Postgres> for DateTime { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { Encode::::encode(self.naive_utc(), buf) } fn size_hint(&self) -> usize { mem::size_of::() } } impl<'r> Decode<'r, Postgres> for DateTime { fn decode(value: PgValueRef<'r>) -> Result { let fixed = as Decode>::decode(value)?; Ok(Local.from_utc_datetime(&fixed.naive_utc())) } } impl<'r> Decode<'r, Postgres> for DateTime { fn decode(value: PgValueRef<'r>) -> Result { let fixed = as Decode>::decode(value)?; Ok(Utc.from_utc_datetime(&fixed.naive_utc())) } } impl<'r> Decode<'r, Postgres> for DateTime { fn decode(value: PgValueRef<'r>) -> Result { Ok(match value.format() { PgValueFormat::Binary => { let naive = >::decode(value)?; Utc.fix().from_utc_datetime(&naive) } PgValueFormat::Text => { let s = value.as_str()?; DateTime::parse_from_str( s, if s.contains('+') || s.contains('-') { // Contains a time-zone specifier // This is given for timestamptz for some reason // Postgres already guarantees this to always be UTC "%Y-%m-%d %H:%M:%S%.f%#z" } else { "%Y-%m-%d %H:%M:%S%.f" }, )? } }) } } #[inline] fn postgres_epoch_datetime() -> NaiveDateTime { NaiveDate::from_ymd_opt(2000, 1, 1) .expect("expected 2000-01-01 to be a valid NaiveDate") .and_hms_opt(0, 0, 0) .expect("expected 2000-01-01T00:00:00 to be a valid NaiveDateTime") }