Skip to content

Commit 5b26b5d

Browse files
committed
Fix chrono::DateTime<Utc> tests
`test_date_time_params` and `test_with_special_date_time_params` could fail when run with a non-UTC system time zone. In Postgres, if no time zone is stated in the input string, then it is assumed to be in system time. This means that in these tests, a correctly parsed `DateTime<Utc>` could be compared to an incorrectly offset time. The offset component is now included in the test strings of these tests. This component is already present in the test strings for `time::OffsetDateTime`.
1 parent d2634a4 commit 5b26b5d

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use chrono_04::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
1+
use chrono_04::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
22
use std::fmt;
33
use tokio_postgres::types::{Date, FromSqlOwned, Timestamp};
44
use tokio_postgres::Client;
@@ -53,18 +53,20 @@ async fn test_with_special_naive_date_time_params() {
5353
async fn test_date_time_params() {
5454
fn make_check(time: &str) -> (Option<DateTime<Utc>>, &str) {
5555
(
56-
Some(Utc.from_utc_datetime(
57-
&NaiveDateTime::parse_from_str(time, "'%Y-%m-%d %H:%M:%S.%f'").unwrap(),
58-
)),
56+
Some(
57+
DateTime::parse_from_str(time, "'%Y-%m-%d %H:%M:%S.%f%#z'")
58+
.unwrap()
59+
.to_utc(),
60+
),
5961
time,
6062
)
6163
}
6264
test_type(
6365
"TIMESTAMP WITH TIME ZONE",
6466
&[
65-
make_check("'1970-01-01 00:00:00.010000000'"),
66-
make_check("'1965-09-25 11:19:33.100314000'"),
67-
make_check("'2010-02-09 23:11:45.120200000'"),
67+
make_check("'1970-01-01 00:00:00.010000000Z'"),
68+
make_check("'1965-09-25 11:19:33.100314000Z'"),
69+
make_check("'2010-02-09 23:11:45.120200000Z'"),
6870
(None, "NULL"),
6971
],
7072
)
@@ -75,18 +77,20 @@ async fn test_date_time_params() {
7577
async fn test_with_special_date_time_params() {
7678
fn make_check(time: &str) -> (Timestamp<DateTime<Utc>>, &str) {
7779
(
78-
Timestamp::Value(Utc.from_utc_datetime(
79-
&NaiveDateTime::parse_from_str(time, "'%Y-%m-%d %H:%M:%S.%f'").unwrap(),
80-
)),
80+
Timestamp::Value(
81+
DateTime::parse_from_str(time, "'%Y-%m-%d %H:%M:%S.%f%#z'")
82+
.unwrap()
83+
.to_utc(),
84+
),
8185
time,
8286
)
8387
}
8488
test_type(
8589
"TIMESTAMP WITH TIME ZONE",
8690
&[
87-
make_check("'1970-01-01 00:00:00.010000000'"),
88-
make_check("'1965-09-25 11:19:33.100314000'"),
89-
make_check("'2010-02-09 23:11:45.120200000'"),
91+
make_check("'1970-01-01 00:00:00.010000000Z'"),
92+
make_check("'1965-09-25 11:19:33.100314000Z'"),
93+
make_check("'2010-02-09 23:11:45.120200000Z'"),
9094
(Timestamp::PosInfinity, "'infinity'"),
9195
(Timestamp::NegInfinity, "'-infinity'"),
9296
],

0 commit comments

Comments
 (0)