Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions sqlx-core/src/sqlite/types/chrono.rs
Expand Up @@ -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<Tz: TimeZone> Type<Sqlite> for DateTime<Tz> {
fn type_info() -> SqliteTypeInfo {
Expand Down Expand Up @@ -58,7 +60,7 @@ where
Tz::Offset: Display,
{
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)
}
}

Expand Down Expand Up @@ -115,6 +117,10 @@ fn decode_datetime(value: SqliteValueRef<'_>) -> Result<DateTime<FixedOffset>, B
}

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
// https://github.com/diesel-rs/diesel/blob/93ab183bcb06c69c0aee4a7557b6798fd52dd0d8/diesel/src/sqlite/types/date_and_time/chrono.rs#L56-L97
let sqlite_datetime_formats = &[
Expand Down
4 changes: 2 additions & 2 deletions sqlx-test/src/lib.rs
Expand Up @@ -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);
)+
Expand Down Expand Up @@ -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(())
Expand Down
12 changes: 6 additions & 6 deletions tests/sqlite/types.rs
Expand Up @@ -94,16 +94,16 @@ mod chrono {
use super::*;
use sqlx::types::chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};

test_type!(chrono_naive_date_time<NaiveDateTime>(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<NaiveDateTime>(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<DateTime::<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<DateTime::<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<DateTime::<FixedOffset>>(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<DateTime::<FixedOffset>>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?",
"'2016-11-08T03:50:23-05:00'" == DateTime::<Utc>::from(FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23))
));
}

Expand Down