Sqlite chrono::DateTime<FixedOffset> timezone fix (#1618)

This commit is contained in:
05storm26 2022-04-15 00:06:55 +02:00 committed by GitHub
parent 342d157a50
commit f328cc15d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 10 deletions

View file

@ -8,7 +8,9 @@ use crate::{
types::Type, types::Type,
}; };
use bitflags::_core::fmt::Display; 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<Tz: TimeZone> Type<Sqlite> for DateTime<Tz> { impl<Tz: TimeZone> Type<Sqlite> for DateTime<Tz> {
fn type_info() -> SqliteTypeInfo { fn type_info() -> SqliteTypeInfo {
@ -58,7 +60,7 @@ where
Tz::Offset: Display, Tz::Offset: Display,
{ {
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull { fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull {
Encode::<Sqlite>::encode(self.naive_utc().format("%F %T%.f").to_string(), buf) Encode::<Sqlite>::encode(self.to_rfc3339_opts(SecondsFormat::AutoSi, false), buf)
} }
} }
@ -115,6 +117,10 @@ fn decode_datetime(value: SqliteValueRef<'_>) -> Result<DateTime<FixedOffset>, B
} }
fn decode_datetime_from_text(value: &str) -> Option<DateTime<FixedOffset>> { fn decode_datetime_from_text(value: &str) -> Option<DateTime<FixedOffset>> {
if let Ok(dt) = DateTime::parse_from_rfc3339(value) {
return Some(dt);
}
// Loop over common date time patterns, inspired by Diesel // 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 // https://github.com/diesel-rs/diesel/blob/93ab183bcb06c69c0aee4a7557b6798fd52dd0d8/diesel/src/sqlite/types/date_and_time/chrono.rs#L56-L97
let sqlite_datetime_formats = &[ let sqlite_datetime_formats = &[

View file

@ -105,7 +105,7 @@ macro_rules! test_unprepared_type {
let row = s.try_next().await?.unwrap(); let row = s.try_next().await?.unwrap();
let rec = row.try_get::<$ty, _>(0)?; let rec = row.try_get::<$ty, _>(0)?;
assert!($value == rec); assert_eq!($value, rec);
drop(s); drop(s);
)+ )+
@ -136,7 +136,7 @@ macro_rules! __test_prepared_decode_type {
let rec: $ty = row.try_get(0)?; let rec: $ty = row.try_get(0)?;
assert!($value == rec); assert_eq!($value, rec);
)+ )+
Ok(()) Ok(())

View file

@ -94,16 +94,16 @@ mod chrono {
use super::*; use super::*;
use sqlx::types::chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc}; use sqlx::types::chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};
test_type!(chrono_naive_date_time<NaiveDateTime>(Sqlite, test_type!(chrono_naive_date_time<NaiveDateTime>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?",
"datetime('2019-01-02 05:10:20')" == NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20) "'2019-01-02 05:10:20'" == NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20)
)); ));
test_type!(chrono_date_time_utc<DateTime::<Utc>>(Sqlite, test_type!(chrono_date_time_utc<DateTime::<Utc>>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?",
"datetime('1996-12-20T00:39:57+00:00')" == Utc.ymd(1996, 12, 20).and_hms(0, 39, 57) "'1996-12-20T00:39:57+00:00'" == Utc.ymd(1996, 12, 20).and_hms(0, 39, 57)
)); ));
test_type!(chrono_date_time_fixed_offset<DateTime::<FixedOffset>>(Sqlite, test_type!(chrono_date_time_fixed_offset<DateTime::<FixedOffset>>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?",
"datetime('2016-11-08T03:50:23-05:00')" == FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23) "'2016-11-08T03:50:23-05:00'" == DateTime::<Utc>::from(FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23))
)); ));
} }