diff --git a/sqlx-core/src/sqlite/types/chrono.rs b/sqlx-core/src/sqlite/types/chrono.rs index 7abf06ba..a258e1c8 100644 --- a/sqlx-core/src/sqlite/types/chrono.rs +++ b/sqlx-core/src/sqlite/types/chrono.rs @@ -61,9 +61,12 @@ impl Type for DateTime { } } -impl Encode<'_, Sqlite> for DateTime { +impl Encode<'_, Sqlite> for DateTime +where + ::Offset: std::fmt::Display, +{ fn encode_by_ref(&self, buf: &mut Vec>) -> IsNull { - let text = self.with_timezone(&Utc).to_rfc3339(); + let text = self.to_rfc3339(); Encode::::encode(text, buf) } } @@ -80,6 +83,13 @@ impl<'a> Decode<'a, Sqlite> for DateTime { } } +impl<'a> Decode<'a, Sqlite> for DateTime { + fn decode(value: SqliteValueRef<'a>) -> Result { + let text = value.text()?; + Ok(DateTime::parse_from_rfc3339(text)?) + } +} + impl<'a> Decode<'a, Sqlite> for DateTime { fn decode(value: SqliteValueRef<'a>) -> Result { let as_utc: DateTime = Decode::::decode(value)?; diff --git a/sqlx-core/src/types/mod.rs b/sqlx-core/src/types/mod.rs index 18e18423..1d658117 100644 --- a/sqlx-core/src/types/mod.rs +++ b/sqlx-core/src/types/mod.rs @@ -24,7 +24,9 @@ pub use uuid::Uuid; #[cfg(feature = "chrono")] #[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] pub mod chrono { - pub use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc}; + pub use chrono::{ + DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc, + }; } #[cfg(feature = "time")] diff --git a/tests/sqlite/types.rs b/tests/sqlite/types.rs index 312942fb..77306052 100644 --- a/tests/sqlite/types.rs +++ b/tests/sqlite/types.rs @@ -35,17 +35,23 @@ test_type!(bytes>(Sqlite, #[cfg(feature = "chrono")] mod chrono { use super::*; - use sqlx::types::chrono::{DateTime, NaiveDate, NaiveDateTime, Utc}; + use sqlx::types::chrono::{ + DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, TimeZone, Utc, + }; - test_type!(chrono_date_time(Sqlite, + test_type!(chrono_naive_date_time(Sqlite, "'2019-01-02 05:10:20'" == NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20) )); - test_type!(chrono_date_time_tz>(Sqlite, - "'1996-12-20T00:39:57+00:00'" - == DateTime::::from_utc( - NaiveDate::from_ymd(1996, 12, 20).and_hms(0, 39, 57), - Utc, - ) + test_type!(chrono_date_time_utc>(Sqlite, + "'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, + "'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_local>(Sqlite, + "'2016-11-08T03:50:23+00:00'" == Local.ymd(2016, 11, 08).and_hms(3, 50, 23) )); }