diff --git a/sqlx-core/src/sqlite/types/chrono.rs b/sqlx-core/src/sqlite/types/chrono.rs index 1ebb2c4f..be4350f2 100644 --- a/sqlx-core/src/sqlite/types/chrono.rs +++ b/sqlx-core/src/sqlite/types/chrono.rs @@ -8,7 +8,9 @@ use crate::{ types::Type, }; use bitflags::_core::fmt::Display; -use chrono::{DateTime, Local, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Utc}; +use chrono::{ + DateTime, Local, NaiveDate, NaiveDateTime, NaiveTime, Offset, SecondsFormat, TimeZone, Utc, +}; impl Type for DateTime { fn type_info() -> SqliteTypeInfo { @@ -58,7 +60,7 @@ where Tz::Offset: Display, { fn encode_by_ref(&self, buf: &mut Vec>) -> IsNull { - Encode::::encode(self.naive_utc().format("%F %T%.f").to_string(), buf) + Encode::::encode(self.to_rfc3339_opts(SecondsFormat::AutoSi, false), buf) } } @@ -115,6 +117,10 @@ fn decode_datetime(value: SqliteValueRef<'_>) -> Result, B } fn decode_datetime_from_text(value: &str) -> Option> { + if let Ok(dt) = DateTime::parse_from_rfc3339(value) { + return Some(dt); + } + // Loop over common date time patterns, inspired by Diesel // https://github.com/diesel-rs/diesel/blob/93ab183bcb06c69c0aee4a7557b6798fd52dd0d8/diesel/src/sqlite/types/date_and_time/chrono.rs#L56-L97 let sqlite_datetime_formats = &[ diff --git a/sqlx-test/src/lib.rs b/sqlx-test/src/lib.rs index 4efa9d29..118654d1 100644 --- a/sqlx-test/src/lib.rs +++ b/sqlx-test/src/lib.rs @@ -105,7 +105,7 @@ macro_rules! test_unprepared_type { let row = s.try_next().await?.unwrap(); let rec = row.try_get::<$ty, _>(0)?; - assert!($value == rec); + assert_eq!($value, rec); drop(s); )+ @@ -136,7 +136,7 @@ macro_rules! __test_prepared_decode_type { let rec: $ty = row.try_get(0)?; - assert!($value == rec); + assert_eq!($value, rec); )+ Ok(()) diff --git a/tests/sqlite/types.rs b/tests/sqlite/types.rs index 9b13083c..181fbb45 100644 --- a/tests/sqlite/types.rs +++ b/tests/sqlite/types.rs @@ -94,16 +94,16 @@ mod chrono { use super::*; use sqlx::types::chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc}; - test_type!(chrono_naive_date_time(Sqlite, - "datetime('2019-01-02 05:10:20')" == NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20) + test_type!(chrono_naive_date_time(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?", + "'2019-01-02 05:10:20'" == NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20) )); - test_type!(chrono_date_time_utc>(Sqlite, - "datetime('1996-12-20T00:39:57+00:00')" == Utc.ymd(1996, 12, 20).and_hms(0, 39, 57) + test_type!(chrono_date_time_utc>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?", + "'1996-12-20T00:39:57+00:00'" == Utc.ymd(1996, 12, 20).and_hms(0, 39, 57) )); - test_type!(chrono_date_time_fixed_offset>(Sqlite, - "datetime('2016-11-08T03:50:23-05:00')" == FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23) + test_type!(chrono_date_time_fixed_offset>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?", + "'2016-11-08T03:50:23-05:00'" == DateTime::::from(FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23)) )); }