Skip to content

Commit c73ad24

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

File tree

9 files changed

+279
-3
lines changed

9 files changed

+279
-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: 2 additions & 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,4 @@ 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-
74+
time-03 = { version = "0.3", package = "time", features = ["parsing"] }

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: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
use time_03::{format_description, 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+
let format = format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second]'").unwrap();
13+
(
14+
Some(PrimitiveDateTime::parse(time, &format).unwrap()),
15+
time,
16+
)
17+
}
18+
test_type(
19+
"TIMESTAMP",
20+
&[
21+
make_check("'1970-01-01 00:00:00'"), // .010000000
22+
make_check("'1965-09-25 11:19:33'"), // .100314000
23+
make_check("'2010-02-09 23:11:45'"), // .120200000
24+
(None, "NULL"),
25+
],
26+
)
27+
.await;
28+
}
29+
30+
#[tokio::test]
31+
async fn test_with_special_primitive_date_time_params() {
32+
fn make_check(time: &str) -> (Timestamp<PrimitiveDateTime>, &str) {
33+
let format = format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second]'").unwrap();
34+
(
35+
Timestamp::Value(PrimitiveDateTime::parse(time, &format).unwrap()),
36+
time,
37+
)
38+
}
39+
test_type(
40+
"TIMESTAMP",
41+
&[
42+
make_check("'1970-01-01 00:00:00'"), // .010000000
43+
make_check("'1965-09-25 11:19:33'"), // .100314000
44+
make_check("'2010-02-09 23:11:45'"), // .120200000
45+
(Timestamp::PosInfinity, "'infinity'"),
46+
(Timestamp::NegInfinity, "'-infinity'"),
47+
],
48+
)
49+
.await;
50+
}
51+
52+
#[tokio::test]
53+
async fn test_offset_date_time_params() {
54+
fn make_check(time: &str) -> (Option<OffsetDateTime>, &str) {
55+
let format = format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second]'").unwrap();
56+
(
57+
Some(OffsetDateTime::parse(time, &format).unwrap()),
58+
time,
59+
)
60+
}
61+
test_type(
62+
"TIMESTAMP WITH TIME ZONE",
63+
&[
64+
make_check("'1970-01-01 00:00:00 +0000'"), // .010000000
65+
make_check("'1965-09-25 11:19:33 +0000'"), // .100314000
66+
make_check("'2010-02-09 23:11:45 +0000'"), // .120200000
67+
(None, "NULL"),
68+
],
69+
)
70+
.await;
71+
}
72+
73+
#[tokio::test]
74+
async fn test_with_special_offset_date_time_params() {
75+
fn make_check(time: &str) -> (Timestamp<OffsetDateTime>, &str) {
76+
let format = format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second]'").unwrap();
77+
(
78+
Timestamp::Value(OffsetDateTime::parse(time, &format).unwrap()),
79+
time,
80+
)
81+
}
82+
test_type(
83+
"TIMESTAMP WITH TIME ZONE",
84+
&[
85+
make_check("'1970-01-01 00:00:00 +0000'"), // .010000000
86+
make_check("'1965-09-25 11:19:33 +0000'"), // .100314000
87+
make_check("'2010-02-09 23:11:45 +0000'"), // .120200000
88+
(Timestamp::PosInfinity, "'infinity'"),
89+
(Timestamp::NegInfinity, "'-infinity'"),
90+
],
91+
)
92+
.await;
93+
}
94+
95+
#[tokio::test]
96+
async fn test_date_params() {
97+
fn make_check(date: &str) -> (Option<time_03::Date>, &str) {
98+
let format = format_description::parse("'[year]-[month]-[day]'").unwrap();
99+
(
100+
Some(time_03::Date::parse(date, &format).unwrap()),
101+
date,
102+
)
103+
}
104+
test_type(
105+
"DATE",
106+
&[
107+
make_check("'1970-01-01'"),
108+
make_check("'1965-09-25'"),
109+
make_check("'2010-02-09'"),
110+
(None, "NULL"),
111+
],
112+
)
113+
.await;
114+
}
115+
116+
#[tokio::test]
117+
async fn test_with_special_date_params() {
118+
fn make_check(date: &str) -> (Date<time_03::Date>, &str) {
119+
let format = format_description::parse("'[year]-[month]-[day]'").unwrap();
120+
(
121+
Date::Value(time_03::Date::parse(date, &format).unwrap()),
122+
date,
123+
)
124+
}
125+
test_type(
126+
"DATE",
127+
&[
128+
make_check("'1970-01-01'"),
129+
make_check("'1965-09-25'"),
130+
make_check("'2010-02-09'"),
131+
(Date::PosInfinity, "'infinity'"),
132+
(Date::NegInfinity, "'-infinity'"),
133+
],
134+
)
135+
.await;
136+
}
137+
138+
#[tokio::test]
139+
async fn test_time_params() {
140+
fn make_check(time: &str) -> (Option<time_03::Time>, &str) {
141+
let format = format_description::parse("'[hour]:[minute]:[second]'").unwrap();
142+
(
143+
Some(time_03::Time::parse(time, &format).unwrap()),
144+
time,
145+
)
146+
}
147+
test_type(
148+
"TIME",
149+
&[
150+
make_check("'00:00:00'"), // .010000000
151+
make_check("'11:19:33'"), // .100314000
152+
make_check("'23:11:45'"), // .120200000
153+
(None, "NULL"),
154+
],
155+
)
156+
.await;
157+
}

0 commit comments

Comments
 (0)