From 78e4036f9085bdb97f20bda0ad13e375faa55f7d Mon Sep 17 00:00:00 2001 From: gwenn Date: Fri, 11 Jun 2021 21:42:02 +0200 Subject: [PATCH 1/8] Fix FromSql impl for OffsetDateTime See 'Missing datetime format support' #968 --- src/types/time.rs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/types/time.rs b/src/types/time.rs index ac182a9..21068dc 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -4,13 +4,15 @@ use crate::Result; use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset}; const CURRENT_TIMESTAMP_FMT: &str = "%Y-%m-%d %H:%M:%S"; -const SQLITE_DATETIME_FMT: &str = "%Y-%m-%d %H:%M:%S.%NZ"; +const SQLITE_DATETIME_FMT: &str = "%Y-%m-%d %H:%M:%S.%N"; +const SQLITE_DATETIME_Z_FMT: &str = "%Y-%m-%d %H:%M:%S.%NZ"; const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%N %z"; impl ToSql for OffsetDateTime { #[inline] fn to_sql(&self) -> Result> { - let time_string = self.to_offset(UtcOffset::UTC).format(SQLITE_DATETIME_FMT); + // FIXME keep original offset + let time_string = self.to_offset(UtcOffset::UTC).format(SQLITE_DATETIME_Z_FMT); Ok(ToSqlOutput::from(time_string)) } } @@ -20,11 +22,18 @@ impl FromSql for OffsetDateTime { value.as_str().and_then(|s| { match s.len() { 19 => PrimitiveDateTime::parse(s, CURRENT_TIMESTAMP_FMT).map(|d| d.assume_utc()), - _ => PrimitiveDateTime::parse(s, SQLITE_DATETIME_FMT) - .map(|d| d.assume_utc()) - .or_else(|err| { - OffsetDateTime::parse(s, SQLITE_DATETIME_FMT_LEGACY).map_err(|_| err) - }), + _ => { + let format = if s.ends_with('Z') { + SQLITE_DATETIME_Z_FMT + } else { + SQLITE_DATETIME_FMT + }; + PrimitiveDateTime::parse(s, format) + .map(|d| d.assume_utc()) + .or_else(|err| { + OffsetDateTime::parse(s, SQLITE_DATETIME_FMT_LEGACY).map_err(|_| err) + }) + } } .map_err(|err| FromSqlError::Other(Box::new(err))) }) @@ -71,6 +80,21 @@ mod test { Ok(()) } + #[test] + fn test_string_values() -> Result<()> { + let db = checked_memory_handle()?; + for s in [ + "2013-10-07 08:23:19.120", + "2013-10-07 08:23:19.120Z", + //"2013-10-07T08:23:19.120Z", // TODO + "2013-10-07 04:23:19.120-04:00", // FIXME offset is lost!!! + ] { + let result: Result = db.query_row("SELECT ?", [s], |r| r.get(0)); + assert!(result.is_ok()); + } + Ok(()) + } + #[test] fn test_sqlite_functions() -> Result<()> { let db = checked_memory_handle()?; From 5baaa9a2247209d49708308a1bc912aa3e5312d9 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 12 Jun 2021 12:04:10 +0200 Subject: [PATCH 2/8] Fix FromSql impl for OffsetDateTime Reveal bugs --- src/types/time.rs | 90 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 24 deletions(-) diff --git a/src/types/time.rs b/src/types/time.rs index 21068dc..4a62893 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -1,18 +1,15 @@ //! [`ToSql`] and [`FromSql`] implementation for [`time::OffsetDateTime`]. use crate::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; use crate::Result; -use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset}; - -const CURRENT_TIMESTAMP_FMT: &str = "%Y-%m-%d %H:%M:%S"; -const SQLITE_DATETIME_FMT: &str = "%Y-%m-%d %H:%M:%S.%N"; -const SQLITE_DATETIME_Z_FMT: &str = "%Y-%m-%d %H:%M:%S.%NZ"; -const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%N %z"; +use time::{Format, OffsetDateTime, PrimitiveDateTime, UtcOffset}; impl ToSql for OffsetDateTime { #[inline] fn to_sql(&self) -> Result> { // FIXME keep original offset - let time_string = self.to_offset(UtcOffset::UTC).format(SQLITE_DATETIME_Z_FMT); + let time_string = self + .to_offset(UtcOffset::UTC) + .format("%Y-%m-%d %H:%M:%S.%NZ"); Ok(ToSqlOutput::from(time_string)) } } @@ -21,18 +18,33 @@ impl FromSql for OffsetDateTime { fn column_result(value: ValueRef<'_>) -> FromSqlResult { value.as_str().and_then(|s| { match s.len() { - 19 => PrimitiveDateTime::parse(s, CURRENT_TIMESTAMP_FMT).map(|d| d.assume_utc()), + len if len <= 10 => PrimitiveDateTime::parse(s, "%Y-%m-%d").map(|d| d.assume_utc()), + len if len <= 19 => { + // TODO YYYY-MM-DDTHH:MM:SS + PrimitiveDateTime::parse(s, "%Y-%m-%d %H:%M:%S").map(|d| d.assume_utc()) + } + _ if s.ends_with('Z') => { + // TODO YYYY-MM-DDTHH:MM:SS.SSS + // FIXME time bug: %N specifier doesn't parse millis correctly (https://github.com/time-rs/time/issues/329) + PrimitiveDateTime::parse(s, "%Y-%m-%d %H:%M:%S.%NZ").map(|d| d.assume_utc()) + } + _ if s.as_bytes()[10] == b'T' => { + // YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM + OffsetDateTime::parse(s, Format::Rfc3339) + } + _ if s.as_bytes()[19] == b':' => { + // legacy + // FIXME time bug: %N specifier doesn't parse millis correctly (https://github.com/time-rs/time/issues/329) + OffsetDateTime::parse(s, "%Y-%m-%d %H:%M:%S:%N %z") + } _ => { - let format = if s.ends_with('Z') { - SQLITE_DATETIME_Z_FMT - } else { - SQLITE_DATETIME_FMT - }; - PrimitiveDateTime::parse(s, format) - .map(|d| d.assume_utc()) - .or_else(|err| { - OffsetDateTime::parse(s, SQLITE_DATETIME_FMT_LEGACY).map_err(|_| err) - }) + // FIXME time bug: %N specifier doesn't parse millis correctly (https://github.com/time-rs/time/issues/329) + // FIXME time bug: %z does not support ':' (https://github.com/time-rs/time/issues/241) + OffsetDateTime::parse(s, "%Y-%m-%d %H:%M:%S.%N%z").or_else(|err| { + PrimitiveDateTime::parse(s, "%Y-%m-%d %H:%M:%S.%N") + .map(|d| d.assume_utc()) + .map_err(|_| err) + }) } } .map_err(|err| FromSqlError::Other(Box::new(err))) @@ -44,7 +56,7 @@ impl FromSql for OffsetDateTime { mod test { use crate::{Connection, Result}; use std::time::Duration; - use time::OffsetDateTime; + use time::{date, offset, OffsetDateTime, Time}; fn checked_memory_handle() -> Result { let db = Connection::open_in_memory()?; @@ -83,14 +95,44 @@ mod test { #[test] fn test_string_values() -> Result<()> { let db = checked_memory_handle()?; - for s in [ - "2013-10-07 08:23:19.120", - "2013-10-07 08:23:19.120Z", + for (s, t) in vec![ + ( + "2013-10-07 08:23:19.120", + Ok(date!(2013 - 10 - 07) + .with_time( + Time::/*FIXME time bug try_from_hms_milli*/try_from_hms_nano( + 8, 23, 19, 120, + ) + .unwrap(), + ) + .assume_utc()), + ), + ( + "2013-10-07 08:23:19.120Z", + Ok(date!(2013 - 10 - 07) + .with_time( + Time::/*FIXME time bug try_from_hms_milli*/try_from_hms_nano( + 8, 23, 19, 120, + ) + .unwrap(), + ) + .assume_utc()), + ), //"2013-10-07T08:23:19.120Z", // TODO - "2013-10-07 04:23:19.120-04:00", // FIXME offset is lost!!! + ( + "2013-10-07 04:23:19.120-04:00", + Ok(date!(2013 - 10 - 07) + .with_time( + Time::/*FIXME time bug try_from_hms_milli*/try_from_hms_nano( + 4, 23, 19, 120, + ) + .unwrap(), + ) + .assume_offset(offset!(-4))), + ), ] { let result: Result = db.query_row("SELECT ?", [s], |r| r.get(0)); - assert!(result.is_ok()); + assert_eq!(result, t); } Ok(()) } From 125f5dfbdeeaffd8b13f32f2a9ade1388b937ac9 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 12 Jun 2021 18:29:01 +0200 Subject: [PATCH 3/8] Fix FromSql impl for OffsetDateTime Use time 0.3.0-alpha-1 --- Cargo.toml | 2 +- src/types/time.rs | 176 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 131 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8653da6..9e48723 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ modern-full = [ bundled-full = ["modern-full", "bundled"] [dependencies] -time = { version = "0.2.23", optional = true } +time = { version = "0.3.0-alpha-1", features = ["formatting", "parsing"], optional = true } bitflags = "1.2" hashlink = "0.7" chrono = { version = "0.4", optional = true } diff --git a/src/types/time.rs b/src/types/time.rs index 4a62893..abeb733 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -1,7 +1,109 @@ //! [`ToSql`] and [`FromSql`] implementation for [`time::OffsetDateTime`]. use crate::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; -use crate::Result; -use time::{Format, OffsetDateTime, PrimitiveDateTime, UtcOffset}; +use crate::{Error, Result}; +use time::format_description::well_known::Rfc3339; +use time::format_description::{modifier, Component, FormatItem}; +use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset}; + +const DATE_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Component(Component::Year(modifier::Year { + repr: modifier::YearRepr::Full, + iso_week_based: false, + sign_is_mandatory: false, + padding: modifier::Padding::Zero, + })), + FormatItem::Literal(b"-"), + FormatItem::Component(Component::Month(modifier::Month { + repr: modifier::MonthRepr::Numerical, + padding: modifier::Padding::Zero, + })), + FormatItem::Literal(b"-"), + FormatItem::Component(Component::Day(modifier::Day { + padding: modifier::Padding::Zero, + })), +]; + +const SHORT_TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Component(Component::Hour(modifier::Hour { + padding: modifier::Padding::Zero, + is_12_hour_clock: false, + })), + FormatItem::Literal(b":"), + FormatItem::Component(Component::Minute(modifier::Minute { + padding: modifier::Padding::Zero, + })), + FormatItem::Literal(b":"), + FormatItem::Component(Component::Second(modifier::Second { + padding: modifier::Padding::Zero, + })), +]; +const TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(SHORT_TIME_FORMAT), + FormatItem::Literal(b"."), + FormatItem::Component(Component::Subsecond(modifier::Subsecond { + digits: modifier::SubsecondDigits::OneOrMore, // TODO SQLite supports ZeroOrMore + })), +]; +const LEGACY_TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(SHORT_TIME_FORMAT), + FormatItem::Literal(b":"), // legacy + FormatItem::Component(Component::Subsecond(modifier::Subsecond { + digits: modifier::SubsecondDigits::OneOrMore, + })), +]; + +const OFFSET_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Component(Component::OffsetHour(modifier::OffsetHour { + sign_is_mandatory: true, + padding: modifier::Padding::Zero, + })), + FormatItem::Literal(b":"), + FormatItem::Component(Component::OffsetMinute(modifier::OffsetMinute { + padding: modifier::Padding::Zero, + })), +]; + +const PRIMITIVE_SHORT_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(DATE_FORMAT), + FormatItem::Literal(b" "), // TODO "T" + FormatItem::Compound(SHORT_TIME_FORMAT), +]; + +const PRIMITIVE_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(DATE_FORMAT), + FormatItem::Literal(b" "), // TODO "T" + FormatItem::Compound(TIME_FORMAT), +]; +const PRIMITIVE_DATE_TIME_Z_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(DATE_FORMAT), + FormatItem::Literal(b" "), // TODO "T" + FormatItem::Compound(TIME_FORMAT), + FormatItem::Literal(b"Z"), // TODO "T" +]; + +const OFFSET_SHORT_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(DATE_FORMAT), + FormatItem::Literal(b" "), // TODO "T" + FormatItem::Compound(SHORT_TIME_FORMAT), + //FormatItem::Literal(b" "), optional + FormatItem::Compound(OFFSET_FORMAT), +]; + +const OFFSET_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(DATE_FORMAT), + FormatItem::Literal(b" "), // TODO "T" + FormatItem::Compound(TIME_FORMAT), + // FormatItem::Literal(b" "), optional + FormatItem::Compound(OFFSET_FORMAT), +]; + +const LEGACY_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ + FormatItem::Compound(DATE_FORMAT), + FormatItem::Literal(b" "), // TODO "T" + FormatItem::Compound(LEGACY_TIME_FORMAT), + FormatItem::Literal(b" "), + FormatItem::Compound(OFFSET_FORMAT), +]; impl ToSql for OffsetDateTime { #[inline] @@ -9,7 +111,8 @@ impl ToSql for OffsetDateTime { // FIXME keep original offset let time_string = self .to_offset(UtcOffset::UTC) - .format("%Y-%m-%d %H:%M:%S.%NZ"); + .format(&PRIMITIVE_DATE_TIME_Z_FORMAT) + .map_err(|err| Error::ToSqlConversionFailure(err.into()))?; Ok(ToSqlOutput::from(time_string)) } } @@ -17,35 +120,28 @@ impl ToSql for OffsetDateTime { impl FromSql for OffsetDateTime { fn column_result(value: ValueRef<'_>) -> FromSqlResult { value.as_str().and_then(|s| { + let s = s.strip_suffix('Z').unwrap_or(s); match s.len() { - len if len <= 10 => PrimitiveDateTime::parse(s, "%Y-%m-%d").map(|d| d.assume_utc()), len if len <= 19 => { // TODO YYYY-MM-DDTHH:MM:SS - PrimitiveDateTime::parse(s, "%Y-%m-%d %H:%M:%S").map(|d| d.assume_utc()) - } - _ if s.ends_with('Z') => { - // TODO YYYY-MM-DDTHH:MM:SS.SSS - // FIXME time bug: %N specifier doesn't parse millis correctly (https://github.com/time-rs/time/issues/329) - PrimitiveDateTime::parse(s, "%Y-%m-%d %H:%M:%S.%NZ").map(|d| d.assume_utc()) + PrimitiveDateTime::parse(s, &PRIMITIVE_SHORT_DATE_TIME_FORMAT) + .map(|d| d.assume_utc()) } _ if s.as_bytes()[10] == b'T' => { // YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM - OffsetDateTime::parse(s, Format::Rfc3339) + OffsetDateTime::parse(s, &Rfc3339) } _ if s.as_bytes()[19] == b':' => { // legacy - // FIXME time bug: %N specifier doesn't parse millis correctly (https://github.com/time-rs/time/issues/329) - OffsetDateTime::parse(s, "%Y-%m-%d %H:%M:%S:%N %z") + OffsetDateTime::parse(s, &LEGACY_DATE_TIME_FORMAT) } - _ => { - // FIXME time bug: %N specifier doesn't parse millis correctly (https://github.com/time-rs/time/issues/329) - // FIXME time bug: %z does not support ':' (https://github.com/time-rs/time/issues/241) - OffsetDateTime::parse(s, "%Y-%m-%d %H:%M:%S.%N%z").or_else(|err| { - PrimitiveDateTime::parse(s, "%Y-%m-%d %H:%M:%S.%N") + _ if s.as_bytes()[19] == b'.' => OffsetDateTime::parse(s, &OFFSET_DATE_TIME_FORMAT) + .or_else(|err| { + PrimitiveDateTime::parse(s, &PRIMITIVE_DATE_TIME_FORMAT) .map(|d| d.assume_utc()) .map_err(|_| err) - }) - } + }), + _ => OffsetDateTime::parse(s, &OFFSET_SHORT_DATE_TIME_FORMAT), } .map_err(|err| FromSqlError::Other(Box::new(err))) }) @@ -55,8 +151,7 @@ impl FromSql for OffsetDateTime { #[cfg(test)] mod test { use crate::{Connection, Result}; - use std::time::Duration; - use time::{date, offset, OffsetDateTime, Time}; + use time::{Date, Month, OffsetDateTime, Time, UtcOffset}; fn checked_memory_handle() -> Result { let db = Connection::open_in_memory()?; @@ -70,8 +165,9 @@ mod test { let mut ts_vec = vec![]; - let make_datetime = - |secs, nanos| OffsetDateTime::from_unix_timestamp(secs) + Duration::from_nanos(nanos); + let make_datetime = |secs: i128, nanos: i128| { + OffsetDateTime::from_unix_timestamp_nanos(1_000_000_000 * secs + nanos).unwrap() + }; ts_vec.push(make_datetime(10_000, 0)); //January 1, 1970 2:46:40 AM ts_vec.push(make_datetime(10_000, 1000)); //January 1, 1970 2:46:40 AM (and one microsecond) @@ -98,37 +194,25 @@ mod test { for (s, t) in vec![ ( "2013-10-07 08:23:19.120", - Ok(date!(2013 - 10 - 07) - .with_time( - Time::/*FIXME time bug try_from_hms_milli*/try_from_hms_nano( - 8, 23, 19, 120, - ) - .unwrap(), - ) + Ok(Date::from_calendar_date(2013, Month::October, 7) + .unwrap() + .with_time(Time::from_hms_milli(8, 23, 19, 120).unwrap()) .assume_utc()), ), ( "2013-10-07 08:23:19.120Z", - Ok(date!(2013 - 10 - 07) - .with_time( - Time::/*FIXME time bug try_from_hms_milli*/try_from_hms_nano( - 8, 23, 19, 120, - ) - .unwrap(), - ) + Ok(Date::from_calendar_date(2013, Month::October, 7) + .unwrap() + .with_time(Time::from_hms_milli(8, 23, 19, 120).unwrap()) .assume_utc()), ), //"2013-10-07T08:23:19.120Z", // TODO ( "2013-10-07 04:23:19.120-04:00", - Ok(date!(2013 - 10 - 07) - .with_time( - Time::/*FIXME time bug try_from_hms_milli*/try_from_hms_nano( - 4, 23, 19, 120, - ) - .unwrap(), - ) - .assume_offset(offset!(-4))), + Ok(Date::from_calendar_date(2013, Month::October, 7) + .unwrap() + .with_time(Time::from_hms_milli(4, 23, 19, 120).unwrap()) + .assume_offset(UtcOffset::from_hms(-4, 0, 0).unwrap())), ), ] { let result: Result = db.query_row("SELECT ?", [s], |r| r.get(0)); From bcf28fec350b0ba60d7843b430854ebd36bd14de Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 12 Jun 2021 18:54:29 +0200 Subject: [PATCH 4/8] Fix FromSql impl for OffsetDateTime Fix test doc --- src/types/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/types/mod.rs b/src/types/mod.rs index 706be0c..5951747 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -41,22 +41,24 @@ For example, to store datetimes as `i64`s counting the number of seconds since the Unix epoch: ``` -use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; +use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; use rusqlite::Result; pub struct DateTimeSql(pub time::OffsetDateTime); impl FromSql for DateTimeSql { fn column_result(value: ValueRef) -> FromSqlResult { - i64::column_result(value).map(|as_i64| { - DateTimeSql(time::OffsetDateTime::from_unix_timestamp(as_i64)) + i64::column_result(value).and_then(|as_i64| { + time::OffsetDateTime::from_unix_timestamp(as_i64) + .map(|odt| DateTimeSql(odt)) + .map_err(|err| FromSqlError::Other(Box::new(err))) }) } } impl ToSql for DateTimeSql { fn to_sql(&self) -> Result { - Ok(self.0.timestamp().into()) + Ok(self.0.unix_timestamp().into()) } } ``` From fc9f03c1ca125f2d88e569679a0865c71f3c0b77 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 14 Jun 2021 20:49:59 +0200 Subject: [PATCH 5/8] Make tests more succint by using Rfc3339 string --- src/types/time.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/types/time.rs b/src/types/time.rs index abeb733..2bfd89c 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -151,7 +151,8 @@ impl FromSql for OffsetDateTime { #[cfg(test)] mod test { use crate::{Connection, Result}; - use time::{Date, Month, OffsetDateTime, Time, UtcOffset}; + use time::OffsetDateTime; + use time::format_description::well_known::Rfc3339; fn checked_memory_handle() -> Result { let db = Connection::open_in_memory()?; @@ -194,25 +195,16 @@ mod test { for (s, t) in vec![ ( "2013-10-07 08:23:19.120", - Ok(Date::from_calendar_date(2013, Month::October, 7) - .unwrap() - .with_time(Time::from_hms_milli(8, 23, 19, 120).unwrap()) - .assume_utc()), + Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()) ), ( "2013-10-07 08:23:19.120Z", - Ok(Date::from_calendar_date(2013, Month::October, 7) - .unwrap() - .with_time(Time::from_hms_milli(8, 23, 19, 120).unwrap()) - .assume_utc()), + Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()) ), //"2013-10-07T08:23:19.120Z", // TODO ( "2013-10-07 04:23:19.120-04:00", - Ok(Date::from_calendar_date(2013, Month::October, 7) - .unwrap() - .with_time(Time::from_hms_milli(4, 23, 19, 120).unwrap()) - .assume_offset(UtcOffset::from_hms(-4, 0, 0).unwrap())), + Ok(OffsetDateTime::parse("2013-10-07T04:23:19.120-04:00", &Rfc3339).unwrap()) ), ] { let result: Result = db.query_row("SELECT ?", [s], |r| r.get(0)); From b14e32618101e143d3251532223041834bcdafcf Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 14 Jun 2021 21:29:24 +0200 Subject: [PATCH 6/8] Use format_description macro --- Cargo.toml | 2 +- src/types/time.rs | 125 ++++++++-------------------------------------- 2 files changed, 22 insertions(+), 105 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9e48723..c18b013 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ modern-full = [ bundled-full = ["modern-full", "bundled"] [dependencies] -time = { version = "0.3.0-alpha-1", features = ["formatting", "parsing"], optional = true } +time = { version = "0.3.0-alpha-1", features = ["formatting", "macros", "parsing"], optional = true } bitflags = "1.2" hashlink = "0.7" chrono = { version = "0.4", optional = true } diff --git a/src/types/time.rs b/src/types/time.rs index 2bfd89c..256a274 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -2,108 +2,25 @@ use crate::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; use crate::{Error, Result}; use time::format_description::well_known::Rfc3339; -use time::format_description::{modifier, Component, FormatItem}; +use time::format_description::FormatItem; +use time::macros::format_description; use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset}; -const DATE_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Component(Component::Year(modifier::Year { - repr: modifier::YearRepr::Full, - iso_week_based: false, - sign_is_mandatory: false, - padding: modifier::Padding::Zero, - })), - FormatItem::Literal(b"-"), - FormatItem::Component(Component::Month(modifier::Month { - repr: modifier::MonthRepr::Numerical, - padding: modifier::Padding::Zero, - })), - FormatItem::Literal(b"-"), - FormatItem::Component(Component::Day(modifier::Day { - padding: modifier::Padding::Zero, - })), -]; - -const SHORT_TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Component(Component::Hour(modifier::Hour { - padding: modifier::Padding::Zero, - is_12_hour_clock: false, - })), - FormatItem::Literal(b":"), - FormatItem::Component(Component::Minute(modifier::Minute { - padding: modifier::Padding::Zero, - })), - FormatItem::Literal(b":"), - FormatItem::Component(Component::Second(modifier::Second { - padding: modifier::Padding::Zero, - })), -]; -const TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(SHORT_TIME_FORMAT), - FormatItem::Literal(b"."), - FormatItem::Component(Component::Subsecond(modifier::Subsecond { - digits: modifier::SubsecondDigits::OneOrMore, // TODO SQLite supports ZeroOrMore - })), -]; -const LEGACY_TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(SHORT_TIME_FORMAT), - FormatItem::Literal(b":"), // legacy - FormatItem::Component(Component::Subsecond(modifier::Subsecond { - digits: modifier::SubsecondDigits::OneOrMore, - })), -]; - -const OFFSET_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Component(Component::OffsetHour(modifier::OffsetHour { - sign_is_mandatory: true, - padding: modifier::Padding::Zero, - })), - FormatItem::Literal(b":"), - FormatItem::Component(Component::OffsetMinute(modifier::OffsetMinute { - padding: modifier::Padding::Zero, - })), -]; - -const PRIMITIVE_SHORT_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(DATE_FORMAT), - FormatItem::Literal(b" "), // TODO "T" - FormatItem::Compound(SHORT_TIME_FORMAT), -]; - -const PRIMITIVE_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(DATE_FORMAT), - FormatItem::Literal(b" "), // TODO "T" - FormatItem::Compound(TIME_FORMAT), -]; -const PRIMITIVE_DATE_TIME_Z_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(DATE_FORMAT), - FormatItem::Literal(b" "), // TODO "T" - FormatItem::Compound(TIME_FORMAT), - FormatItem::Literal(b"Z"), // TODO "T" -]; - -const OFFSET_SHORT_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(DATE_FORMAT), - FormatItem::Literal(b" "), // TODO "T" - FormatItem::Compound(SHORT_TIME_FORMAT), - //FormatItem::Literal(b" "), optional - FormatItem::Compound(OFFSET_FORMAT), -]; - -const OFFSET_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(DATE_FORMAT), - FormatItem::Literal(b" "), // TODO "T" - FormatItem::Compound(TIME_FORMAT), - // FormatItem::Literal(b" "), optional - FormatItem::Compound(OFFSET_FORMAT), -]; - -const LEGACY_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[ - FormatItem::Compound(DATE_FORMAT), - FormatItem::Literal(b" "), // TODO "T" - FormatItem::Compound(LEGACY_TIME_FORMAT), - FormatItem::Literal(b" "), - FormatItem::Compound(OFFSET_FORMAT), -]; +const PRIMITIVE_SHORT_DATE_TIME_FORMAT: &[FormatItem<'_>] = + format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); +const PRIMITIVE_DATE_TIME_FORMAT: &[FormatItem<'_>] = + format_description!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"); +const PRIMITIVE_DATE_TIME_Z_FORMAT: &[FormatItem<'_>] = + format_description!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]Z"); +const OFFSET_SHORT_DATE_TIME_FORMAT: &[FormatItem<'_>] = format_description!( + "[year]-[month]-[day] [hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]" +); +const OFFSET_DATE_TIME_FORMAT: &[FormatItem<'_>] = format_description!( + "[year]-[month]-[day] [hour]:[minute]:[second].[subsecond][offset_hour sign:mandatory]:[offset_minute]" +); +const LEGACY_DATE_TIME_FORMAT: &[FormatItem<'_>] = format_description!( + "[year]-[month]-[day] [hour]:[minute]:[second]:[subsecond] [offset_hour sign:mandatory]:[offset_minute]" +); impl ToSql for OffsetDateTime { #[inline] @@ -151,8 +68,8 @@ impl FromSql for OffsetDateTime { #[cfg(test)] mod test { use crate::{Connection, Result}; - use time::OffsetDateTime; use time::format_description::well_known::Rfc3339; + use time::OffsetDateTime; fn checked_memory_handle() -> Result { let db = Connection::open_in_memory()?; @@ -195,16 +112,16 @@ mod test { for (s, t) in vec![ ( "2013-10-07 08:23:19.120", - Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()) + Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()), ), ( "2013-10-07 08:23:19.120Z", - Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()) + Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()), ), //"2013-10-07T08:23:19.120Z", // TODO ( "2013-10-07 04:23:19.120-04:00", - Ok(OffsetDateTime::parse("2013-10-07T04:23:19.120-04:00", &Rfc3339).unwrap()) + Ok(OffsetDateTime::parse("2013-10-07T04:23:19.120-04:00", &Rfc3339).unwrap()), ), ] { let result: Result = db.query_row("SELECT ?", [s], |r| r.get(0)); From 0075671b0907715707fe8cc79fdbe9b518a25c82 Mon Sep 17 00:00:00 2001 From: gwenn Date: Fri, 30 Jul 2021 21:31:52 +0200 Subject: [PATCH 7/8] Upgrade time crate to version 0.3.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 16a61ec..e7940f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ modern-full = [ bundled-full = ["modern-full", "bundled"] [dependencies] -time = { version = "0.3.0-alpha-1", features = ["formatting", "macros", "parsing"], optional = true } +time = { version = "0.3.0", features = ["formatting", "macros", "parsing"], optional = true } bitflags = "1.2" hashlink = "0.7" chrono = { version = "0.4", optional = true } From 0241350af30dbf850010c2793a098cb9be252cbe Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 31 Jul 2021 11:05:42 +0200 Subject: [PATCH 8/8] Check Rfc3339 before stripping Z So that "2013-10-07T08:23:19Z" and "2013-10-07T08:23:19.120Z" are correctly parsed. --- src/types/time.rs | 49 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/types/time.rs b/src/types/time.rs index 256a274..bc600a9 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -37,6 +37,11 @@ impl ToSql for OffsetDateTime { impl FromSql for OffsetDateTime { fn column_result(value: ValueRef<'_>) -> FromSqlResult { value.as_str().and_then(|s| { + if s.len() > 10 && s.as_bytes()[10] == b'T' { + // YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM + return OffsetDateTime::parse(s, &Rfc3339) + .map_err(|err| FromSqlError::Other(Box::new(err))); + } let s = s.strip_suffix('Z').unwrap_or(s); match s.len() { len if len <= 19 => { @@ -44,10 +49,6 @@ impl FromSql for OffsetDateTime { PrimitiveDateTime::parse(s, &PRIMITIVE_SHORT_DATE_TIME_FORMAT) .map(|d| d.assume_utc()) } - _ if s.as_bytes()[10] == b'T' => { - // YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM - OffsetDateTime::parse(s, &Rfc3339) - } _ if s.as_bytes()[19] == b':' => { // legacy OffsetDateTime::parse(s, &LEGACY_DATE_TIME_FORMAT) @@ -71,15 +72,10 @@ mod test { use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; - fn checked_memory_handle() -> Result { - let db = Connection::open_in_memory()?; - db.execute_batch("CREATE TABLE foo (t TEXT, i INTEGER, f FLOAT)")?; - Ok(db) - } - #[test] fn test_offset_date_time() -> Result<()> { - let db = checked_memory_handle()?; + let db = Connection::open_in_memory()?; + db.execute_batch("CREATE TABLE foo (t TEXT, i INTEGER, f FLOAT)")?; let mut ts_vec = vec![]; @@ -108,8 +104,20 @@ mod test { #[test] fn test_string_values() -> Result<()> { - let db = checked_memory_handle()?; + let db = Connection::open_in_memory()?; for (s, t) in vec![ + ( + "2013-10-07 08:23:19", + Ok(OffsetDateTime::parse("2013-10-07T08:23:19Z", &Rfc3339).unwrap()), + ), + ( + "2013-10-07 08:23:19Z", + Ok(OffsetDateTime::parse("2013-10-07T08:23:19Z", &Rfc3339).unwrap()), + ), + ( + "2013-10-07T08:23:19Z", + Ok(OffsetDateTime::parse("2013-10-07T08:23:19Z", &Rfc3339).unwrap()), + ), ( "2013-10-07 08:23:19.120", Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()), @@ -118,11 +126,22 @@ mod test { "2013-10-07 08:23:19.120Z", Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()), ), - //"2013-10-07T08:23:19.120Z", // TODO + ( + "2013-10-07T08:23:19.120Z", + Ok(OffsetDateTime::parse("2013-10-07T08:23:19.120Z", &Rfc3339).unwrap()), + ), + ( + "2013-10-07 04:23:19-04:00", + Ok(OffsetDateTime::parse("2013-10-07T04:23:19-04:00", &Rfc3339).unwrap()), + ), ( "2013-10-07 04:23:19.120-04:00", Ok(OffsetDateTime::parse("2013-10-07T04:23:19.120-04:00", &Rfc3339).unwrap()), ), + ( + "2013-10-07T04:23:19.120-04:00", + Ok(OffsetDateTime::parse("2013-10-07T04:23:19.120-04:00", &Rfc3339).unwrap()), + ), ] { let result: Result = db.query_row("SELECT ?", [s], |r| r.get(0)); assert_eq!(result, t); @@ -132,7 +151,7 @@ mod test { #[test] fn test_sqlite_functions() -> Result<()> { - let db = checked_memory_handle()?; + let db = Connection::open_in_memory()?; let result: Result = db.query_row("SELECT CURRENT_TIMESTAMP", [], |r| r.get(0)); assert!(result.is_ok()); @@ -141,7 +160,7 @@ mod test { #[test] fn test_param() -> Result<()> { - let db = checked_memory_handle()?; + let db = Connection::open_in_memory()?; let result: Result = db.query_row("SELECT 1 WHERE ? BETWEEN datetime('now', '-1 minute') AND datetime('now', '+1 minute')", [OffsetDateTime::now_utc()], |r| r.get(0)); assert!(result.is_ok()); Ok(())