Debian Codegraph @

src/time_03.rs

raw symbols SCIP
1 use bytes::BytesMut;
2 use postgres_protocol::types;
3 use std::error::Error;
4 use time_03::{Date, Duration, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
5
6 use crate::{FromSql, IsNull, ToSql, Type};
7
8 fn base() -> PrimitiveDateTime {
9 PrimitiveDateTime::new(Date::from_ordinal_date(2000, 1).unwrap(), Time::MIDNIGHT)
10 }
11
12 impl<'a> FromSql<'a> for PrimitiveDateTime {
13 fn from_sql(_: &Type, raw: &[u8]) -> Result<PrimitiveDateTime, Box<dyn Error + Sync + Send>> {
14 let t = types::timestamp_from_sql(raw)?;
15 Ok(base()
16 .checked_add(Duration::microseconds(t))
17 .ok_or("value too large to decode")?)
18 }
19
20 accepts!(TIMESTAMP);
21 }
22
23 impl ToSql for PrimitiveDateTime {
24 fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
25 let time = match i64::try_from((*self - base()).whole_microseconds()) {
26 Ok(time) => time,
27 Err(_) => return Err("value too large to transmit".into()),
28 };
29 types::timestamp_to_sql(time, w);
30 Ok(IsNull::No)
31 }
32
33 accepts!(TIMESTAMP);
34 to_sql_checked!();
35 }
36
37 impl<'a> FromSql<'a> for OffsetDateTime {
38 fn from_sql(type_: &Type, raw: &[u8]) -> Result<OffsetDateTime, Box<dyn Error + Sync + Send>> {
39 let primitive = PrimitiveDateTime::from_sql(type_, raw)?;
40 Ok(primitive.assume_utc())
41 }
42
43 accepts!(TIMESTAMPTZ);
44 }
45
46 impl ToSql for OffsetDateTime {
47 fn to_sql(
48 &self,
49 type_: &Type,
50 w: &mut BytesMut,
51 ) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
52 let utc_datetime = self.to_offset(UtcOffset::UTC);
53 let date = utc_datetime.date();
54 let time = utc_datetime.time();
55 let primitive = PrimitiveDateTime::new(date, time);
56 primitive.to_sql(type_, w)
57 }
58
59 accepts!(TIMESTAMPTZ);
60 to_sql_checked!();
61 }
62
63 impl<'a> FromSql<'a> for Date {
64 fn from_sql(_: &Type, raw: &[u8]) -> Result<Date, Box<dyn Error + Sync + Send>> {
65 let jd = types::date_from_sql(raw)?;
66 Ok(base()
67 .date()
68 .checked_add(Duration::days(i64::from(jd)))
69 .ok_or("value too large to decode")?)
70 }
71
72 accepts!(DATE);
73 }
74
75 impl ToSql for Date {
76 fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
77 let jd = (*self - base().date()).whole_days();
78 let jd = i32::try_from(jd).map_err(|_| "value too large to transmit")?;
79
80 types::date_to_sql(jd, w);
81 Ok(IsNull::No)
82 }
83
84 accepts!(DATE);
85 to_sql_checked!();
86 }
87
88 impl<'a> FromSql<'a> for Time {
89 fn from_sql(_: &Type, raw: &[u8]) -> Result<Time, Box<dyn Error + Sync + Send>> {
90 let usec = types::time_from_sql(raw)?;
91 Ok(Time::MIDNIGHT + Duration::microseconds(usec))
92 }
93
94 accepts!(TIME);
95 }
96
97 impl ToSql for Time {
98 fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
99 let delta = *self - Time::MIDNIGHT;
100 let time = match i64::try_from(delta.whole_microseconds()) {
101 Ok(time) => time,
102 Err(_) => return Err("value too large to transmit".into()),
103 };
104 types::time_to_sql(time, w);
105 Ok(IsNull::No)
106 }
107
108 accepts!(TIME);
109 to_sql_checked!();
110 }