Skip to content

Commit 45b80e8

Browse files
authored
Merge pull request #580 from aloucks/time
Add support for time-0.2 types
2 parents 89ea051 + 6d8c403 commit 45b80e8

File tree

7 files changed

+280
-0
lines changed

7 files changed

+280
-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 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 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!(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().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: 2 additions & 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"
@@ -62,4 +63,5 @@ geo-types-04 = { version = "0.4", package = "geo-types" }
6263
serde-1 = { version = "1.0", package = "serde" }
6364
serde_json-1 = { version = "1.0", package = "serde_json" }
6465
uuid-08 = { version = "0.8", package = "uuid" }
66+
time-02 = { version = "0.2", package = "time" }
6567

tokio-postgres/tests/test/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ mod eui48_04;
2222
mod geo_010;
2323
#[cfg(feature = "with-serde_json-1")]
2424
mod serde_json_1;
25+
#[cfg(feature = "with-time-0_2")]
26+
mod time_02;
2527
#[cfg(feature = "with-uuid-0_8")]
2628
mod uuid_08;
2729

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
use time_02::{OffsetDateTime, PrimitiveDateTime};
2+
use tokio_postgres::types::{Date, Timestamp};
3+
4+
use crate::types::test_type;
5+
6+
// time 0.2 does not [yet?] support parsing fractional seconds
7+
// https://github.com/time-rs/time/issues/226
8+
9+
#[tokio::test]
10+
async fn test_primitive_date_time_params() {
11+
fn make_check(time: &str) -> (Option<PrimitiveDateTime>, &str) {
12+
(
13+
Some(PrimitiveDateTime::parse(time, "'%Y-%m-%d %H:%M:%S'").unwrap()),
14+
time,
15+
)
16+
}
17+
test_type(
18+
"TIMESTAMP",
19+
&[
20+
make_check("'1970-01-01 00:00:00'"), // .010000000
21+
make_check("'1965-09-25 11:19:33'"), // .100314000
22+
make_check("'2010-02-09 23:11:45'"), // .120200000
23+
(None, "NULL"),
24+
],
25+
)
26+
.await;
27+
}
28+
29+
#[tokio::test]
30+
async fn test_with_special_primitive_date_time_params() {
31+
fn make_check(time: &str) -> (Timestamp<PrimitiveDateTime>, &str) {
32+
(
33+
Timestamp::Value(PrimitiveDateTime::parse(time, "'%Y-%m-%d %H:%M:%S'").unwrap()),
34+
time,
35+
)
36+
}
37+
test_type(
38+
"TIMESTAMP",
39+
&[
40+
make_check("'1970-01-01 00:00:00'"), // .010000000
41+
make_check("'1965-09-25 11:19:33'"), // .100314000
42+
make_check("'2010-02-09 23:11:45'"), // .120200000
43+
(Timestamp::PosInfinity, "'infinity'"),
44+
(Timestamp::NegInfinity, "'-infinity'"),
45+
],
46+
)
47+
.await;
48+
}
49+
50+
#[tokio::test]
51+
async fn test_offset_date_time_params() {
52+
fn make_check(time: &str) -> (Option<OffsetDateTime>, &str) {
53+
(
54+
Some(OffsetDateTime::parse(time, "'%Y-%m-%d %H:%M:%S %z'").unwrap()),
55+
time,
56+
)
57+
}
58+
test_type(
59+
"TIMESTAMP WITH TIME ZONE",
60+
&[
61+
make_check("'1970-01-01 00:00:00 +0000'"), // .010000000
62+
make_check("'1965-09-25 11:19:33 +0000'"), // .100314000
63+
make_check("'2010-02-09 23:11:45 +0000'"), // .120200000
64+
(None, "NULL"),
65+
],
66+
)
67+
.await;
68+
}
69+
70+
#[tokio::test]
71+
async fn test_with_special_offset_date_time_params() {
72+
fn make_check(time: &str) -> (Timestamp<OffsetDateTime>, &str) {
73+
(
74+
Timestamp::Value(OffsetDateTime::parse(time, "'%Y-%m-%d %H:%M:%S %z'").unwrap()),
75+
time,
76+
)
77+
}
78+
test_type(
79+
"TIMESTAMP WITH TIME ZONE",
80+
&[
81+
make_check("'1970-01-01 00:00:00 +0000'"), // .010000000
82+
make_check("'1965-09-25 11:19:33 +0000'"), // .100314000
83+
make_check("'2010-02-09 23:11:45 +0000'"), // .120200000
84+
(Timestamp::PosInfinity, "'infinity'"),
85+
(Timestamp::NegInfinity, "'-infinity'"),
86+
],
87+
)
88+
.await;
89+
}
90+
91+
#[tokio::test]
92+
async fn test_date_params() {
93+
fn make_check(time: &str) -> (Option<time_02::Date>, &str) {
94+
(
95+
Some(time_02::Date::parse(time, "'%Y-%m-%d'").unwrap()),
96+
time,
97+
)
98+
}
99+
test_type(
100+
"DATE",
101+
&[
102+
make_check("'1970-01-01'"),
103+
make_check("'1965-09-25'"),
104+
make_check("'2010-02-09'"),
105+
(None, "NULL"),
106+
],
107+
)
108+
.await;
109+
}
110+
111+
#[tokio::test]
112+
async fn test_with_special_date_params() {
113+
fn make_check(date: &str) -> (Date<time_02::Date>, &str) {
114+
(
115+
Date::Value(time_02::Date::parse(date, "'%Y-%m-%d'").unwrap()),
116+
date,
117+
)
118+
}
119+
test_type(
120+
"DATE",
121+
&[
122+
make_check("'1970-01-01'"),
123+
make_check("'1965-09-25'"),
124+
make_check("'2010-02-09'"),
125+
(Date::PosInfinity, "'infinity'"),
126+
(Date::NegInfinity, "'-infinity'"),
127+
],
128+
)
129+
.await;
130+
}
131+
132+
#[tokio::test]
133+
async fn test_time_params() {
134+
fn make_check(time: &str) -> (Option<time_02::Time>, &str) {
135+
(
136+
Some(time_02::Time::parse(time, "'%H:%M:%S'").unwrap()),
137+
time,
138+
)
139+
}
140+
test_type(
141+
"TIME",
142+
&[
143+
make_check("'00:00:00'"), // .010000000
144+
make_check("'11:19:33'"), // .100314000
145+
make_check("'23:11:45'"), // .120200000
146+
(None, "NULL"),
147+
],
148+
)
149+
.await;
150+
}

0 commit comments

Comments
 (0)