Debian Codegraph @

src/types/time/datetime.rs

raw symbols SCIP
1 use crate::decode::Decode;
2 use crate::encode::{Encode, IsNull};
3 use crate::error::BoxDynError;
4 use crate::types::time::PG_EPOCH;
5 use crate::types::Type;
6 use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
7 use std::borrow::Cow;
8 use std::mem;
9 use time::macros::format_description;
10 use time::macros::offset;
11 use time::{Duration, OffsetDateTime, PrimitiveDateTime};
12
13 impl Type<Postgres> for PrimitiveDateTime {
14 fn type_info() -> PgTypeInfo {
15 PgTypeInfo::TIMESTAMP
16 }
17 }
18
19 impl Type<Postgres> for OffsetDateTime {
20 fn type_info() -> PgTypeInfo {
21 PgTypeInfo::TIMESTAMPTZ
22 }
23 }
24
25 impl PgHasArrayType for PrimitiveDateTime {
26 fn array_type_info() -> PgTypeInfo {
27 PgTypeInfo::TIMESTAMP_ARRAY
28 }
29 }
30
31 impl PgHasArrayType for OffsetDateTime {
32 fn array_type_info() -> PgTypeInfo {
33 PgTypeInfo::TIMESTAMPTZ_ARRAY
34 }
35 }
36
37 impl Encode<'_, Postgres> for PrimitiveDateTime {
38 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
39 // TIMESTAMP is encoded as the microseconds since the epoch
40 let micros: i64 = (*self - PG_EPOCH.midnight())
41 .whole_microseconds()
42 .try_into()
43 .map_err(|_| {
44 format!("value {self:?} would overflow binary encoding for Postgres TIME")
45 })?;
46 Encode::<Postgres>::encode(micros, buf)
47 }
48
49 fn size_hint(&self) -> usize {
50 mem::size_of::<i64>()
51 }
52 }
53
54 impl<'r> Decode<'r, Postgres> for PrimitiveDateTime {
55 fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
56 Ok(match value.format() {
57 PgValueFormat::Binary => {
58 // TIMESTAMP is encoded as the microseconds since the epoch
59 let us = Decode::<Postgres>::decode(value)?;
60 PG_EPOCH.midnight() + Duration::microseconds(us)
61 }
62
63 PgValueFormat::Text => {
64 let s = value.as_str()?;
65
66 // If there is no decimal point we need to add one.
67 let s = if s.contains('.') {
68 Cow::Borrowed(s)
69 } else {
70 Cow::Owned(format!("{s}.0"))
71 };
72
73 // Contains a time-zone specifier
74 // This is given for timestamptz for some reason
75 // Postgres already guarantees this to always be UTC
76 if s.contains('+') {
77 PrimitiveDateTime::parse(&s, &format_description!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond][offset_hour]"))?
78 } else {
79 PrimitiveDateTime::parse(
80 &s,
81 &format_description!(
82 "[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"
83 ),
84 )?
85 }
86 }
87 })
88 }
89 }
90
91 impl Encode<'_, Postgres> for OffsetDateTime {
92 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
93 let utc = self.to_offset(offset!(UTC));
94 let primitive = PrimitiveDateTime::new(utc.date(), utc.time());
95
96 Encode::<Postgres>::encode(primitive, buf)
97 }
98
99 fn size_hint(&self) -> usize {
100 mem::size_of::<i64>()
101 }
102 }
103
104 impl<'r> Decode<'r, Postgres> for OffsetDateTime {
105 fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
106 Ok(<PrimitiveDateTime as Decode<Postgres>>::decode(value)?.assume_utc())
107 }
108 }