From 072a2b926c9e837d84e8de471a2ca81dc28fb64d Mon Sep 17 00:00:00 2001 From: Adam Maroti Date: Mon, 10 Jan 2022 11:20:27 +0100 Subject: [PATCH] Sqlite chrono::DateTime timezone fix --- sqlx-core/src/sqlite/types/chrono.rs | 10 ++++++++-- sqlx-test/src/lib.rs | 4 ++-- tests/sqlite/sqlite.db | Bin 36864 -> 0 bytes tests/sqlite/types.rs | 12 ++++++------ 4 files changed, 16 insertions(+), 10 deletions(-) delete mode 100644 tests/sqlite/sqlite.db diff --git a/sqlx-core/src/sqlite/types/chrono.rs b/sqlx-core/src/sqlite/types/chrono.rs index 1ebb2c4f45..be4350f243 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 052e157271..4903862b0a 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/sqlite.db b/tests/sqlite/sqlite.db deleted file mode 100644 index a3d8d5cc2964ec37bc568d4e00340395edd8a5d2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 36864 zcmeI4TWnKh9KcVzOLuPHe;b3s#^8)Gwr*oE28=uFwz7?ti=z`s(57@}qhV{udIk(1 z%n%>c?GHZrvgLo$^K1L- z>Dl-F+AQfgO?TZDPB^yJof_ziIaZyLt9ZRigJmg-QYTkcu0FYXY`$a~rxc$NRA3f-A`XfxM$r7$JZapJiWe@x1-Jkg-~wEL3vdB0zy+pLft_l$ zzpBc!$BFIhwbKWC<)?{g+CDgFC*xD^s!biCU?gNkf{o#jHD%k1WUSAwmd^~pP2~^V z<*`zCpRss6HJEhL(XO7T)7L-cZqB5API*wP(9}eil}I{vk3FENYCM%p5Bm)t?ta+* ze|K*sQ;wA!?Cq6*r+*;P7aJI|4%kE0nu;^C9Q%lTcytpf6X|Fy?j#P`*1lA#*N!E% zz-~Qzc6oVD`cT3?JU$b0XLQ1Mw}p0FleV&AX)A5_+HuFKw7LgUed7-gtD4VWTs(Ek zW6#dMZ9}Uw7d2cq%?8u_bQ zAmGjRZ}i`3y6%9ZweD4h{1=mh|7J|Ndf=KOFT8L8F2Du202kl_T!0I30WQD=xBwU6 z0@Juap(jhNSJhHH)?y{n^6-s3pfO~{<#DGHP0q^$e=73A3m4!5T!0I30WQD=xBwU6 z0$hL#Z~-nb0}Etis4AZS4{LiB_z^C{hwwJM2#>=3upc_15thMxP{nWJs`yO2Ctedz ziR1G7`}c`C?$dtJzScg~&T6N%C$wW) zN)C({F2Du202kl_T!0I3foW49FPxzSoc;2Qlf1;_H=xr_UOQD=scNIDm8ur1wonzK zs+p=Lsv4;ZQq@4!W~w$(wUMd~RIR6K9aU?oT0>PmRja95Mb%2G>Zn>lRV`J^sai%= z4OL62s-~)ns!FN?R8>$_PSp~s%BWgQRVh`As4Ah#qG};k3#cllYCcs(R25P+kE*#; z`Kg*i)oiK?sG3DpK2>>C z4~vK7`G5EwZpfnm-@#S50+--(xCkG?d3Ybr!aMLLybiCxOYkf_4X5N0f`{QaJOD>w z5c=T&bVCgG!fxn*ZO{r$unE?}N_q6463W1WBA5etV1N(2;xBPi{49PF--~PFD{)zT zA&)G4EG~!-#5wV~1-Jkg-~wEL3vdB0zy-Jf7vKUj zr-1xC>j@*;5nB;$h*m@kVhbXKXht+48WBN617b5`6JjG`17ba59bzqF4Wb^g8nFto z5>ba(fv80+M=V3sAeJJk5mksvL;z8NC`T+olpz))N)d|?B?t?#5U~JJjF^uoLKGtA zA?70dh&hPahyuhcL_Q)9k&6I?K$r*vp(8YeipW9u5ZQ<-1Gk)xUR?x zFI<2NZ~-pB1-Jkg-~wEL3vdB0zy-L#3@DKA$y5Srt3A+fHA^It=~QxbTKHT3_(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)) )); }