Skip to content

Commit 512f3db

Browse files
committed
Add support for time 0.3
1 parent 3e4be86 commit 512f3db

File tree

9 files changed

+271
-3
lines changed

9 files changed

+271
-3
lines changed

postgres-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ with-geo-types-0_7 = ["geo-types-0_7"]
2222
with-serde_json-1 = ["serde-1", "serde_json-1"]
2323
with-uuid-0_8 = ["uuid-08"]
2424
with-time-0_2 = ["time-02"]
25+
with-time-0_3 = ["time-03"]
2526

2627
[dependencies]
2728
bytes = "1.0"
@@ -40,3 +41,4 @@ serde-1 = { version = "1.0", package = "serde", optional = true }
4041
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
4142
uuid-08 = { version = "0.8", package = "uuid", optional = true }
4243
time-02 = { version = "0.2", package = "time", optional = true }
44+
time-03 = { version = "0.3", package = "time", default-features = false, optional = true }

postgres-types/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ mod geo_types_07;
209209
mod serde_json_1;
210210
#[cfg(feature = "with-time-0_2")]
211211
mod time_02;
212+
#[cfg(feature = "with-time-0_3")]
213+
mod time_03;
212214
#[cfg(feature = "with-uuid-0_8")]
213215
mod uuid_08;
214216

postgres-types/src/time_03.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_03::{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::__from_ordinal_date_unchecked(2000, 1), Time::MIDNIGHT)
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::MIDNIGHT + 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::MIDNIGHT;
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
@@ -31,6 +31,7 @@ with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"]
3131
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
3232
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]
3333
with-time-0_2 = ["tokio-postgres/with-time-0_2"]
34+
with-time-0_3 = ["tokio-postgres/with-time-0_3"]
3435

3536
[dependencies]
3637
bytes = "1.0"

postgres/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
//! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no |
6262
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no |
6363
//! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
64-
//! | `with-time-0_2` | Enable support for the `time` crate. | [time](https://crates.io/crates/time) 0.2 | no |
64+
//! | `with-time-0_2` | Enable support for the 0.2 version of the `time` crate. | [time](https://crates.io/crates/time/0.2.0) 0.2 | no |
65+
//! | `with-time-0_3` | Enable support for the 0.3 version of the `time` crate. | [time](https://crates.io/crates/time/0.3.0) 0.3 | no |
6566
#![warn(clippy::all, rust_2018_idioms, missing_docs)]
6667

6768
pub use fallible_iterator;

tokio-postgres/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ with-geo-types-0_7 = ["postgres-types/with-geo-types-0_7"]
3737
with-serde_json-1 = ["postgres-types/with-serde_json-1"]
3838
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
3939
with-time-0_2 = ["postgres-types/with-time-0_2"]
40+
with-time-0_3 = ["postgres-types/with-time-0_3"]
4041

4142
[dependencies]
4243
async-trait = "0.1"
@@ -70,4 +71,3 @@ serde-1 = { version = "1.0", package = "serde" }
7071
serde_json-1 = { version = "1.0", package = "serde_json" }
7172
uuid-08 = { version = "0.8", package = "uuid" }
7273
time-02 = { version = "0.2", package = "time" }
73-

tokio-postgres/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@
112112
//! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no |
113113
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no |
114114
//! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
115-
//! | `with-time-0_2` | Enable support for the `time` crate. | [time](https://crates.io/crates/time) 0.2 | no |
115+
//! | `with-time-0_2` | Enable support for the 0.2 version of the `time` crate. | [time](https://crates.io/crates/time/0.2.0) 0.2 | no |
116+
//! | `with-time-0_3` | Enable support for the 0.3 version of the `time` crate. | [time](https://crates.io/crates/time/0.3.0) 0.3 | no |
116117
#![doc(html_root_url = "https://docs.rs/tokio-postgres/0.7")]
117118
#![warn(rust_2018_idioms, clippy::all, missing_docs)]
118119

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ mod geo_types_07;
2929
mod serde_json_1;
3030
#[cfg(feature = "with-time-0_2")]
3131
mod time_02;
32+
#[cfg(feature = "with-time-0_3")]
33+
mod time_03;
3234
#[cfg(feature = "with-uuid-0_8")]
3335
mod uuid_08;
3436

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)