Skip to content

Commit be0d71f

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

File tree

9 files changed

+270
-3
lines changed

9 files changed

+270
-3
lines changed

postgres-types/Cargo.toml

+2
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

+2
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

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

postgres/Cargo.toml

+1
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

+2-1
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

+2-1
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

+2-1
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

+2
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

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 =
13+
format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second]'").unwrap();
14+
(Some(PrimitiveDateTime::parse(time, &format).unwrap()), time)
15+
}
16+
test_type(
17+
"TIMESTAMP",
18+
&[
19+
make_check("'1970-01-01 00:00:00'"), // .010000000
20+
make_check("'1965-09-25 11:19:33'"), // .100314000
21+
make_check("'2010-02-09 23:11:45'"), // .120200000
22+
(None, "NULL"),
23+
],
24+
)
25+
.await;
26+
}
27+
28+
#[tokio::test]
29+
async fn test_with_special_primitive_date_time_params() {
30+
fn make_check(time: &str) -> (Timestamp<PrimitiveDateTime>, &str) {
31+
let format =
32+
format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second]'").unwrap();
33+
(
34+
Timestamp::Value(PrimitiveDateTime::parse(time, &format).unwrap()),
35+
time,
36+
)
37+
}
38+
test_type(
39+
"TIMESTAMP",
40+
&[
41+
make_check("'1970-01-01 00:00:00'"), // .010000000
42+
make_check("'1965-09-25 11:19:33'"), // .100314000
43+
make_check("'2010-02-09 23:11:45'"), // .120200000
44+
(Timestamp::PosInfinity, "'infinity'"),
45+
(Timestamp::NegInfinity, "'-infinity'"),
46+
],
47+
)
48+
.await;
49+
}
50+
51+
#[tokio::test]
52+
async fn test_offset_date_time_params() {
53+
fn make_check(time: &str) -> (Option<OffsetDateTime>, &str) {
54+
let format =
55+
format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]'").unwrap();
56+
(Some(OffsetDateTime::parse(time, &format).unwrap()), time)
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+
let format =
74+
format_description::parse("'[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]'").unwrap();
75+
(
76+
Timestamp::Value(OffsetDateTime::parse(time, &format).unwrap()),
77+
time,
78+
)
79+
}
80+
test_type(
81+
"TIMESTAMP WITH TIME ZONE",
82+
&[
83+
make_check("'1970-01-01 00:00:00 +0000'"), // .010000000
84+
make_check("'1965-09-25 11:19:33 +0000'"), // .100314000
85+
make_check("'2010-02-09 23:11:45 +0000'"), // .120200000
86+
(Timestamp::PosInfinity, "'infinity'"),
87+
(Timestamp::NegInfinity, "'-infinity'"),
88+
],
89+
)
90+
.await;
91+
}
92+
93+
#[tokio::test]
94+
async fn test_date_params() {
95+
fn make_check(date: &str) -> (Option<time_03::Date>, &str) {
96+
let format = format_description::parse("'[year]-[month]-[day]'").unwrap();
97+
(Some(time_03::Date::parse(date, &format).unwrap()), date)
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_03::Date>, &str) {
114+
let format = format_description::parse("'[year]-[month]-[day]'").unwrap();
115+
(
116+
Date::Value(time_03::Date::parse(date, &format).unwrap()),
117+
date,
118+
)
119+
}
120+
test_type(
121+
"DATE",
122+
&[
123+
make_check("'1970-01-01'"),
124+
make_check("'1965-09-25'"),
125+
make_check("'2010-02-09'"),
126+
(Date::PosInfinity, "'infinity'"),
127+
(Date::NegInfinity, "'-infinity'"),
128+
],
129+
)
130+
.await;
131+
}
132+
133+
#[tokio::test]
134+
async fn test_time_params() {
135+
fn make_check(time: &str) -> (Option<time_03::Time>, &str) {
136+
let format = format_description::parse("'[hour]:[minute]:[second]'").unwrap();
137+
(Some(time_03::Time::parse(time, &format).unwrap()), time)
138+
}
139+
test_type(
140+
"TIME",
141+
&[
142+
make_check("'00:00:00'"), // .010000000
143+
make_check("'11:19:33'"), // .100314000
144+
make_check("'23:11:45'"), // .120200000
145+
(None, "NULL"),
146+
],
147+
)
148+
.await;
149+
}

0 commit comments

Comments
 (0)