Skip to content

Commit 2082d4f

Browse files
committed
Add support for time-0.2 types
1 parent 89ea051 commit 2082d4f

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

postgres-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ with-eui48-0_4 = ["eui48-04"]
1818
with-geo-types-0_4 = ["geo-types-04"]
1919
with-serde_json-1 = ["serde-1", "serde_json-1"]
2020
with-uuid-0_8 = ["uuid-08"]
21+
with-time-0_2 = ["time-02"]
2122

2223
[dependencies]
2324
bytes = "0.5"
@@ -32,3 +33,4 @@ geo-types-04 = { version = "0.4", package = "geo-types", optional = true }
3233
serde-1 = { version = "1.0", package = "serde", optional = true }
3334
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
3435
uuid-08 = { version = "0.8", package = "uuid", optional = true }
36+
time-02 = { version = "0.2", package = "time", optional = true }

postgres-types/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,15 @@ mod eui48_04;
198198
mod geo_types_04;
199199
#[cfg(feature = "with-serde_json-1")]
200200
mod serde_json_1;
201+
#[cfg(feature = "with-time-0_2")]
202+
mod time_02;
201203
#[cfg(feature = "with-uuid-0_8")]
202204
mod uuid_08;
203205

206+
// The time::{date, time} macros produce compile errors if the crate package is renamed.
207+
#[cfg(feature = "with-time-0_2")]
208+
extern crate time_02 as time;
209+
204210
#[doc(hidden)]
205211
pub mod private;
206212
mod special;
@@ -391,6 +397,10 @@ impl WrongType {
391397
/// | `chrono::DateTime<FixedOffset>` | TIMESTAMP WITH TIME ZONE |
392398
/// | `chrono::NaiveDate` | DATE |
393399
/// | `chrono::NaiveTime` | TIME |
400+
/// | `time::PrimitiveDateTime` | TIMESTAMP |
401+
/// | `time::OffsetDateTime` | TIMESTAMP, TIMESTAMP WITH TIME ZONE |
402+
/// | `time::Date` | DATE |
403+
/// | `time::Time` | TIME |
394404
/// | `eui48::MacAddress` | MACADDR |
395405
/// | `geo_types::Point<f64>` | POINT |
396406
/// | `geo_types::Rect<f64>` | BOX |
@@ -650,6 +660,10 @@ pub enum IsNull {
650660
/// | `chrono::DateTime<FixedOffset>` | TIMESTAMP WITH TIME ZONE |
651661
/// | `chrono::NaiveDate` | DATE |
652662
/// | `chrono::NaiveTime` | TIME |
663+
/// | `time::PrimitiveDateTime` | TIMESTAMP |
664+
/// | `time::OffsetDateTime` | TIMESTAMP, TIMESTAMP WITH TIME ZONE |
665+
/// | `time::Date` | DATE |
666+
/// | `time::Time` | TIME |
653667
/// | `eui48::MacAddress` | MACADDR |
654668
/// | `geo_types::Point<f64>` | POINT |
655669
/// | `geo_types::Rect<f64>` | BOX |

postgres-types/src/time_02.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use bytes::BytesMut;
2+
use postgres_protocol::types;
3+
use std::convert::TryFrom;
4+
use std::error::Error;
5+
use time_02::{date, time, Date, Duration, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
6+
7+
use crate::{FromSql, IsNull, ToSql, Type};
8+
9+
#[rustfmt::skip]
10+
const fn base() -> PrimitiveDateTime {
11+
PrimitiveDateTime::new(date!(2000-01-01), time!(00:00:00))
12+
}
13+
14+
impl<'a> FromSql<'a> for PrimitiveDateTime {
15+
fn from_sql(_: &Type, raw: &[u8]) -> Result<PrimitiveDateTime, Box<dyn Error + Sync + Send>> {
16+
let t = types::timestamp_from_sql(raw)?;
17+
Ok(base() + Duration::microseconds(t))
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!(TIMESTAMP, 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!(TIMESTAMP, 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().date() + Duration::days(i64::from(jd)))
67+
}
68+
69+
accepts!(DATE);
70+
}
71+
72+
impl ToSql for Date {
73+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
74+
let jd = (*self - base().date()).whole_days();
75+
if jd > i64::from(i32::max_value()) || jd < i64::from(i32::min_value()) {
76+
return Err("value too large to transmit".into());
77+
}
78+
79+
types::date_to_sql(jd as i32, w);
80+
Ok(IsNull::No)
81+
}
82+
83+
accepts!(DATE);
84+
to_sql_checked!();
85+
}
86+
87+
impl<'a> FromSql<'a> for Time {
88+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Time, Box<dyn Error + Sync + Send>> {
89+
let usec = types::time_from_sql(raw)?;
90+
Ok(time!(00:00:00) + Duration::microseconds(usec))
91+
}
92+
93+
accepts!(TIME);
94+
}
95+
96+
impl ToSql for Time {
97+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
98+
let delta = *self - time!(00:00:00);
99+
let time = match i64::try_from(delta.whole_microseconds()) {
100+
Ok(time) => time,
101+
Err(_) => return Err("value too large to transmit".into()),
102+
};
103+
types::time_to_sql(time, w);
104+
Ok(IsNull::No)
105+
}
106+
107+
accepts!(TIME);
108+
to_sql_checked!();
109+
}

postgres/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
2727
with-geo-types-0_4 = ["tokio-postgres/with-geo-types-0_4"]
2828
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
2929
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]
30+
with-time-0_2 = ["tokio-postgres/with-time-0_2"]
3031

3132
[dependencies]
3233
bytes = "0.5"

tokio-postgres/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ with-eui48-0_4 = ["postgres-types/with-eui48-0_4"]
3333
with-geo-types-0_4 = ["postgres-types/with-geo-types-0_4"]
3434
with-serde_json-1 = ["postgres-types/with-serde_json-1"]
3535
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
36+
with-time-0_2 = ["postgres-types/with-time-0_2"]
3637

3738
[dependencies]
3839
async-trait = "0.1"

0 commit comments

Comments
 (0)