From 79376a4ca96f4c645c3fe0aabff12525eff83c0e Mon Sep 17 00:00:00 2001 From: gwenn Date: Wed, 4 May 2016 21:57:16 +0200 Subject: [PATCH 1/6] Partially fix the bug reported by @iwinux. If microseconds are specified but no timezone indicator, parsing fails... --- src/types/chrono.rs | 73 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 15 deletions(-) diff --git a/src/types/chrono.rs b/src/types/chrono.rs index 19f3372..41871d2 100644 --- a/src/types/chrono.rs +++ b/src/types/chrono.rs @@ -179,15 +179,14 @@ impl FromSql for DateTime { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result> { match sqlite3_column_type(stmt, col) { ffi::SQLITE_TEXT => { - let s = try!(String::column_result(stmt, col)); + let mut s = try!(String::column_result(stmt, col)); if s.len() > 23 { - let fmt = if s.as_bytes()[10] == b'T' { - "%Y-%m-%dT%H:%M:%S%.f%:z" - } else { - "%Y-%m-%d %H:%M:%S%.f%:z" + if s.as_bytes()[10] == b' ' { + s.as_mut_vec()[10] = b'T'; }; - match UTC.datetime_from_str(&s, fmt) { - Ok(dt) => Ok(dt), + // It cannot be used when the offset can be missing. + match DateTime::parse_from_rfc3339(&s) { + Ok(dt) => Ok(dt.with_timezone(&UTC)), Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), } } else { @@ -220,20 +219,19 @@ impl ToSql for DateTime { /// "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DD HH:MM"/"YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS"/ Julian Day / Unix Time => ISO 8601 date and time with time zone. /// ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DDTHH:MM"/"YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported) -/// When the timezone is not specified, Local is used. +/// When the timezone is not specified, UTC is used. impl FromSql for DateTime { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result> { match sqlite3_column_type(stmt, col) { ffi::SQLITE_TEXT => { - let s = try!(String::column_result(stmt, col)); + let mut s = try!(String::column_result(stmt, col)); if s.len() > 23 { - let fmt = if s.as_bytes()[10] == b'T' { - "%Y-%m-%dT%H:%M:%S%.f%:z" - } else { - "%Y-%m-%d %H:%M:%S%.f%:z" + if s.as_bytes()[10] == b' ' { + s.as_mut_vec()[10] = b'T'; }; - match Local.datetime_from_str(&s, fmt) { - Ok(dt) => Ok(dt), + // It cannot be used when the offset can be missing. + match DateTime::parse_from_rfc3339(&s) { + Ok(dt) => Ok(dt.with_timezone(&Local)), Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), } } else { @@ -371,4 +369,49 @@ mod test { let i: DateTime = db.query_row("SELECT i FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(local, i); } + + #[test] + fn test_timezone() { + // Worst + UTC.datetime_from_str("2016-05-04 17:00:42.862471+08:00", + "%Y-%m-%d %H:%M:%S%.f%:z") + .unwrap_err(); + UTC.datetime_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%:z") + .unwrap_err(); + UTC.datetime_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%:z").unwrap_err(); + + UTC.datetime_from_str("2016-05-04 17:00:42.862471+08:00", "%Y-%m-%d %H:%M:%S%.f%z") + .unwrap_err(); + UTC.datetime_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%z").unwrap_err(); + UTC.datetime_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%z").unwrap_err(); + + // Better but... + DateTime::parse_from_str("2016-05-04 17:00:42.862471+08:00", "%Y-%m-%d %H:%M:%S%.f%z") + .unwrap() + .with_timezone(&UTC); + DateTime::parse_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%z") + .unwrap_err(); // Invalid + DateTime::parse_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%z") + .unwrap_err(); // TooShort + + DateTime::parse_from_str("2016-05-04 17:00:42.862471+08:00", + "%Y-%m-%d %H:%M:%S%.f%:z") + .unwrap() + .with_timezone(&UTC); + DateTime::parse_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%:z") + .unwrap_err(); // Invalid + DateTime::parse_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%:z") + .unwrap_err(); // TooShort + + // Best but in SQLite, the timezone indicator is optional + DateTime::parse_from_rfc3339("2016-05-04T17:00:42.862471+08:00") + .unwrap() + .with_timezone(&UTC); + DateTime::parse_from_rfc3339("2016-05-04T17:00:42.862471Z").unwrap().with_timezone(&UTC); + DateTime::parse_from_rfc3339("2016-05-04T17:00:42.862471").unwrap_err(); // TooShort + + "2016-05-04T17:00:42.862471+08:00".parse::>().unwrap(); + "2016-05-04T17:00:42.862471Z".parse::>().unwrap(); + "2016-05-04T17:00:42.862471".parse::>().unwrap_err(); // TooShort + } } From 350dc59bb964095c7040b39e58a4c5b1d5d5b2a4 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 May 2016 15:32:59 -0400 Subject: [PATCH 2/6] Remove unnecessary column_has_valid_sqlite_type impls --- src/types/chrono.rs | 16 ---------------- src/types/mod.rs | 4 ---- src/types/time.rs | 4 ---- 3 files changed, 24 deletions(-) diff --git a/src/types/chrono.rs b/src/types/chrono.rs index 41871d2..bd23f34 100644 --- a/src/types/chrono.rs +++ b/src/types/chrono.rs @@ -54,10 +54,6 @@ impl FromSql for NaiveDate { _ => Err(Error::InvalidColumnType), } } - - unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool { - true // to avoid double check - } } /// ISO 8601 time without timezone => "HH:MM:SS.SSS" @@ -158,10 +154,6 @@ impl FromSql for NaiveDateTime { _ => Err(Error::InvalidColumnType), } } - - unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool { - true // to avoid double check - } } /// ISO 8601 date and time with time zone => "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM" @@ -202,10 +194,6 @@ impl FromSql for DateTime { _ => Err(Error::InvalidColumnType), } } - - unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool { - true // to avoid double check - } } @@ -247,10 +235,6 @@ impl FromSql for DateTime { _ => Err(Error::InvalidColumnType), } } - - unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool { - true // to avoid double check - } } // struct UnixTime(NaiveDateTime); diff --git a/src/types/mod.rs b/src/types/mod.rs index 4cffd50..46a5c54 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -298,10 +298,6 @@ impl FromSql for Value { _ => Err(Error::InvalidColumnType), } } - - unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool { - true - } } #[cfg(test)] diff --git a/src/types/time.rs b/src/types/time.rs index 30886a4..2fe19c5 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -42,10 +42,6 @@ impl FromSql for time::Timespec { _ => Err(Error::InvalidColumnType), } } - - unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool { - true // to avoid double check - } } #[cfg(test)] From 42bc173009082eb403089970b72a4a26ad3e5173 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 May 2016 19:56:57 -0500 Subject: [PATCH 3/6] Only use strings for NaiveDate's ToSql/FromSql impl --- src/types/chrono.rs | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/src/types/chrono.rs b/src/types/chrono.rs index bd23f34..023dfed 100644 --- a/src/types/chrono.rs +++ b/src/types/chrono.rs @@ -14,7 +14,6 @@ use ffi::sqlite3_column_type; const JULIAN_DAY: f64 = 2440587.5; // 1970-01-01 00:00:00 is JD 2440587.5 const DAY_IN_SECONDS: f64 = 86400.0; -const JULIAN_DAY_GREGORIAN: f64 = 1721424.5; // Jan 1, 1 proleptic Gregorian calendar /// ISO 8601 calendar date without timezone => "YYYY-MM-DD" impl ToSql for NaiveDate { @@ -24,36 +23,19 @@ impl ToSql for NaiveDate { } } -/// "YYYY-MM-DD" or Julian Day => ISO 8601 calendar date without timezone. +/// "YYYY-MM-DD" => ISO 8601 calendar date without timezone. impl FromSql for NaiveDate { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result { - match sqlite3_column_type(stmt, col) { - ffi::SQLITE_TEXT => { - let s = try!(String::column_result(stmt, col)); - match NaiveDate::parse_from_str(&s, "%Y-%m-%d") { - Ok(dt) => Ok(dt), - Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), - } - } - ffi::SQLITE_FLOAT => { - // if column affinity is REAL and an integer/unix timestamp is inserted => unexpected result - let mut jd = ffi::sqlite3_column_double(stmt, col); - jd -= JULIAN_DAY_GREGORIAN; - if jd < i32::min_value() as f64 || jd > i32::max_value() as f64 { - let err: Box = "out-of-range date".into(); - return Err(Error::FromSqlConversionFailure(err)); - } - match NaiveDate::from_num_days_from_ce_opt(jd as i32) { - Some(dt) => Ok(dt), - None => { - let err: Box = "out-of-range date".into(); - Err(Error::FromSqlConversionFailure(err)) - } - } - } - _ => Err(Error::InvalidColumnType), + let s = try!(String::column_result(stmt, col)); + match NaiveDate::parse_from_str(&s, "%Y-%m-%d") { + Ok(dt) => Ok(dt), + Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), } } + + unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool { + String::column_has_valid_sqlite_type(stmt, col) + } } /// ISO 8601 time without timezone => "HH:MM:SS.SSS" @@ -80,7 +62,7 @@ impl FromSql for NaiveTime { } unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool { - sqlite3_column_type(stmt, col) == ffi::SQLITE_TEXT + String::column_has_valid_sqlite_type(stmt, col) } } @@ -256,14 +238,11 @@ mod test { let db = checked_memory_handle(); let date = NaiveDate::from_ymd(2016, 2, 23); db.execute("INSERT INTO foo (t) VALUES (?)", &[&date]).unwrap(); - db.execute("UPDATE foo SET f = julianday(t)", &[]).unwrap(); let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!("2016-02-23", s); let t: NaiveDate = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(date, t); - let f: NaiveDate = db.query_row("SELECT f FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!(date, f); } #[test] From 07801ca3709d6baa92155bb1ff2c12de1d548db7 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 May 2016 22:23:02 -0500 Subject: [PATCH 4/6] Greatly reduce implementation of chrono types. Limit ToSql/FromSql to string representations, and limit allowed string representations to RFC3339 (if a timezone is expected) and SQLite's `datetime()` format (if not). --- src/types/chrono.rs | 254 ++++++++------------------------------------ 1 file changed, 44 insertions(+), 210 deletions(-) diff --git a/src/types/chrono.rs b/src/types/chrono.rs index 023dfed..98ed77e 100644 --- a/src/types/chrono.rs +++ b/src/types/chrono.rs @@ -1,19 +1,13 @@ //! Convert most of the [Time Strings](http://sqlite.org/lang_datefunc.html) to chrono types. extern crate chrono; -use std::error; use self::chrono::{NaiveDate, NaiveTime, NaiveDateTime, DateTime, TimeZone, UTC, Local}; use libc::c_int; use {Error, Result}; use types::{FromSql, ToSql}; -use ffi; use ffi::sqlite3_stmt; -use ffi::sqlite3_column_type; - -const JULIAN_DAY: f64 = 2440587.5; // 1970-01-01 00:00:00 is JD 2440587.5 -const DAY_IN_SECONDS: f64 = 86400.0; /// ISO 8601 calendar date without timezone => "YYYY-MM-DD" impl ToSql for NaiveDate { @@ -69,159 +63,68 @@ impl FromSql for NaiveTime { /// ISO 8601 combined date and time without timezone => "YYYY-MM-DD HH:MM:SS.SSS" impl ToSql for NaiveDateTime { unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { - let date_str = self.format("%Y-%m-%d %H:%M:%S%.f").to_string(); + let date_str = self.format("%Y-%m-%dT%H:%M:%S%.f").to_string(); date_str.bind_parameter(stmt, col) } } -/// "YYYY-MM-DD HH:MM"/"YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS"/ Julian Day / Unix Time => ISO 8601 combined date and time without timezone. -/// ("YYYY-MM-DDTHH:MM"/"YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported) +/// "YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS" => ISO 8601 combined date and time without timezone. +/// ("YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported) impl FromSql for NaiveDateTime { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result { - match sqlite3_column_type(stmt, col) { - ffi::SQLITE_TEXT => { - let s = try!(String::column_result(stmt, col)); - let fmt = match s.len() { - 16 => { - match s.as_bytes()[10] { - b'T' => "%Y-%m-%dT%H:%M", - _ => "%Y-%m-%d %H:%M", - } - } - 19 => { - match s.as_bytes()[10] { - b'T' => "%Y-%m-%dT%H:%M:%S", - _ => "%Y-%m-%d %H:%M:%S", - } - } - _ => { - match s.as_bytes()[10] { - b'T' => "%Y-%m-%dT%H:%M:%S%.f", - _ => "%Y-%m-%d %H:%M:%S%.f", - } - } - }; - match NaiveDateTime::parse_from_str(&s, fmt) { - Ok(dt) => Ok(dt), - Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), - } - } - ffi::SQLITE_INTEGER => { - match NaiveDateTime::from_timestamp_opt(ffi::sqlite3_column_int64(stmt, col), 0) { - Some(dt) => Ok(dt), - None => { - let err: Box = "out-of-range number of seconds" - .into(); - Err(Error::FromSqlConversionFailure(err)) - } - } - } - ffi::SQLITE_FLOAT => { - // if column affinity is REAL and an integer/unix timestamp is inserted => unexpected result - let mut jd = ffi::sqlite3_column_double(stmt, col); - jd -= JULIAN_DAY; - jd *= DAY_IN_SECONDS; - let ns = jd.fract() * 10f64.powi(9); - match NaiveDateTime::from_timestamp_opt(jd as i64, ns as u32) { - Some(dt) => Ok(dt), - None => { - let err: Box = "out-of-range number of \ - seconds and/or invalid \ - nanosecond" - .into(); - Err(Error::FromSqlConversionFailure(err)) - } - } - } - _ => Err(Error::InvalidColumnType), + let s = try!(String::column_result(stmt, col)); + + let fmt = if s.len() >= 11 && s.as_bytes()[10] == b'T' { + "%Y-%m-%dT%H:%M:%S%.f" + } else { + "%Y-%m-%d %H:%M:%S%.f" + }; + + match NaiveDateTime::parse_from_str(&s, fmt) { + Ok(dt) => Ok(dt), + Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), } } -} -/// ISO 8601 date and time with time zone => "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM" -impl ToSql for DateTime { - unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { - let date_str = self.format("%Y-%m-%d %H:%M:%S%.f%:z").to_string(); - date_str.bind_parameter(stmt, col) + unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool { + String::column_has_valid_sqlite_type(stmt, col) } } -/// "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DD HH:MM"/"YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS"/ Julian Day / Unix Time => ISO 8601 date and time with time zone. -/// ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DDTHH:MM"/"YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported) -/// When the timezone is not specified, UTC is used. +/// Date and time with time zone => RFC3339 timestamp ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM"). +impl ToSql for DateTime where Tz::Offset: ::std::fmt::Display { + unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { + self.to_rfc3339().bind_parameter(stmt, col) + } +} + +/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime. impl FromSql for DateTime { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result> { - match sqlite3_column_type(stmt, col) { - ffi::SQLITE_TEXT => { - let mut s = try!(String::column_result(stmt, col)); - if s.len() > 23 { - if s.as_bytes()[10] == b' ' { - s.as_mut_vec()[10] = b'T'; - }; - // It cannot be used when the offset can be missing. - match DateTime::parse_from_rfc3339(&s) { - Ok(dt) => Ok(dt.with_timezone(&UTC)), - Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), - } - } else { - NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt)) - } - } - ffi::SQLITE_INTEGER => { - NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt)) - } - ffi::SQLITE_FLOAT => { - NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt)) - } - _ => Err(Error::InvalidColumnType), + let s = try!(String::column_result(stmt, col)); + match DateTime::parse_from_rfc3339(&s) { + Ok(dt) => Ok(dt.with_timezone(&UTC)), + Err(_) => NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt)), } } -} - -/// ISO 8601 date and time with time zone => "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM" -impl ToSql for DateTime { - unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { - let date_str = self.format("%Y-%m-%d %H:%M:%S%.f%:z").to_string(); - date_str.bind_parameter(stmt, col) + unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool { + String::column_has_valid_sqlite_type(stmt, col) } } -/// "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DD HH:MM"/"YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS"/ Julian Day / Unix Time => ISO 8601 date and time with time zone. -/// ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DDTHH:MM"/"YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported) -/// When the timezone is not specified, UTC is used. +/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime. impl FromSql for DateTime { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result> { - match sqlite3_column_type(stmt, col) { - ffi::SQLITE_TEXT => { - let mut s = try!(String::column_result(stmt, col)); - if s.len() > 23 { - if s.as_bytes()[10] == b' ' { - s.as_mut_vec()[10] = b'T'; - }; - // It cannot be used when the offset can be missing. - match DateTime::parse_from_rfc3339(&s) { - Ok(dt) => Ok(dt.with_timezone(&Local)), - Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), - } - } else { - NaiveDateTime::column_result(stmt, col).map(|dt| Local.from_utc_datetime(&dt)) - } - } - ffi::SQLITE_INTEGER => { - NaiveDateTime::column_result(stmt, col).map(|dt| Local.from_utc_datetime(&dt)) - } - ffi::SQLITE_FLOAT => { - NaiveDateTime::column_result(stmt, col).map(|dt| Local.from_utc_datetime(&dt)) - } - _ => Err(Error::InvalidColumnType), - } + let utc_dt = try!(DateTime::::column_result(stmt, col)); + Ok(utc_dt.with_timezone(&Local)) + } + + unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool { + DateTime::::column_has_valid_sqlite_type(stmt, col) } } -// struct UnixTime(NaiveDateTime); -// struct JulianTime(NaiveDateTime) - #[cfg(test)] mod test { use Connection; @@ -264,117 +167,48 @@ mod test { let time = NaiveTime::from_hms(23, 56, 4); let dt = NaiveDateTime::new(date, time); - let di = NaiveDateTime::new(date, NaiveTime::from_hms(23, 56, 3)); - let ds = NaiveDateTime::new(date, NaiveTime::from_hms(23, 56, 5)); - db.execute("INSERT INTO foo (t) VALUES (?)", &[&dt]).unwrap(); - db.execute("UPDATE foo SET f = julianday(t), i = strftime('%s', t)", - &[]) - .unwrap(); let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!("2016-02-23 23:56:04", s); + assert_eq!("2016-02-23T23:56:04", s); let v: NaiveDateTime = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(dt, v); - let f: NaiveDateTime = db.query_row("SELECT f FROM foo", &[], |r| r.get(0)).unwrap(); - // `2016-02-23T23:56:04` vs `2016-02-23T23:56:03.999992609` - assert!(f.ge(&di) && f.le(&ds)); - let i: NaiveDateTime = db.query_row("SELECT i FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!(dt, i); db.execute("UPDATE foo set b = datetime(t)", &[]).unwrap(); // "YYYY-MM-DD HH:MM:SS" let hms: NaiveDateTime = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(dt, hms); - - db.execute("UPDATE foo set b = strftime('%Y-%m-%dT%H:%M', t)", &[]).unwrap(); - let hm: NaiveDateTime = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!(NaiveDateTime::new(date, NaiveTime::from_hms(23, 56, 0)), hm); } #[test] fn test_date_time_utc() { let db = checked_memory_handle(); let date = NaiveDate::from_ymd(2016, 2, 23); - let time = NaiveTime::from_hms(23, 56, 4); + let time = NaiveTime::from_hms_milli(23, 56, 4, 789); let dt = NaiveDateTime::new(date, time); let utc = UTC.from_utc_datetime(&dt); db.execute("INSERT INTO foo (t) VALUES (?)", &[&utc]).unwrap(); - db.execute("UPDATE foo SET f = julianday(t), i = strftime('%s', t)", - &[]) - .unwrap(); let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!("2016-02-23 23:56:04+00:00", s); + assert_eq!("2016-02-23T23:56:04.789+00:00", s); let v: DateTime = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(utc, v); - let i: DateTime = db.query_row("SELECT i FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!(utc, i); } #[test] fn test_date_time_local() { let db = checked_memory_handle(); let date = NaiveDate::from_ymd(2016, 2, 23); - let time = NaiveTime::from_hms(23, 56, 4); + let time = NaiveTime::from_hms_milli(23, 56, 4, 789); let dt = NaiveDateTime::new(date, time); let local = Local.from_local_datetime(&dt).single().unwrap(); db.execute("INSERT INTO foo (t) VALUES (?)", &[&local]).unwrap(); - db.execute("UPDATE foo SET f = julianday(t), i = strftime('%s', t)", - &[]) - .unwrap(); - // let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); - // assert_eq!("2016-02-23 23:56:04.000+01:00", s); + let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); + let offset = Local.offset_from_utc_datetime(&dt); + assert_eq!(format!("2016-02-23T23:56:04.789{:}", offset), s); let v: DateTime = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(local, v); - let i: DateTime = db.query_row("SELECT i FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!(local, i); - } - - #[test] - fn test_timezone() { - // Worst - UTC.datetime_from_str("2016-05-04 17:00:42.862471+08:00", - "%Y-%m-%d %H:%M:%S%.f%:z") - .unwrap_err(); - UTC.datetime_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%:z") - .unwrap_err(); - UTC.datetime_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%:z").unwrap_err(); - - UTC.datetime_from_str("2016-05-04 17:00:42.862471+08:00", "%Y-%m-%d %H:%M:%S%.f%z") - .unwrap_err(); - UTC.datetime_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%z").unwrap_err(); - UTC.datetime_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%z").unwrap_err(); - - // Better but... - DateTime::parse_from_str("2016-05-04 17:00:42.862471+08:00", "%Y-%m-%d %H:%M:%S%.f%z") - .unwrap() - .with_timezone(&UTC); - DateTime::parse_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%z") - .unwrap_err(); // Invalid - DateTime::parse_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%z") - .unwrap_err(); // TooShort - - DateTime::parse_from_str("2016-05-04 17:00:42.862471+08:00", - "%Y-%m-%d %H:%M:%S%.f%:z") - .unwrap() - .with_timezone(&UTC); - DateTime::parse_from_str("2016-05-04 17:00:42.862471Z", "%Y-%m-%d %H:%M:%S%.f%:z") - .unwrap_err(); // Invalid - DateTime::parse_from_str("2016-05-04 17:00:42.862471", "%Y-%m-%d %H:%M:%S%.f%:z") - .unwrap_err(); // TooShort - - // Best but in SQLite, the timezone indicator is optional - DateTime::parse_from_rfc3339("2016-05-04T17:00:42.862471+08:00") - .unwrap() - .with_timezone(&UTC); - DateTime::parse_from_rfc3339("2016-05-04T17:00:42.862471Z").unwrap().with_timezone(&UTC); - DateTime::parse_from_rfc3339("2016-05-04T17:00:42.862471").unwrap_err(); // TooShort - - "2016-05-04T17:00:42.862471+08:00".parse::>().unwrap(); - "2016-05-04T17:00:42.862471Z".parse::>().unwrap(); - "2016-05-04T17:00:42.862471".parse::>().unwrap_err(); // TooShort } } From 88fb175b40e529ad31c8878d139e198e2c9edcf4 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 May 2016 22:30:11 -0500 Subject: [PATCH 5/6] Limit Timespec representation to strings. --- src/types/time.rs | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/src/types/time.rs b/src/types/time.rs index 2fe19c5..fe77956 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -4,13 +4,9 @@ use libc::c_int; use {Error, Result}; use types::{FromSql, ToSql}; -use ffi; use ffi::sqlite3_stmt; -use ffi::sqlite3_column_type; const SQLITE_DATETIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S"; -const JULIAN_DAY: f64 = 2440587.5; // 1970-01-01 00:00:00 is JD 2440587.5 -const DAY_IN_SECONDS: f64 = 86400.0; impl ToSql for time::Timespec { unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { @@ -21,27 +17,16 @@ impl ToSql for time::Timespec { impl FromSql for time::Timespec { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result { - match sqlite3_column_type(stmt, col) { - ffi::SQLITE_TEXT => { - let col_str = FromSql::column_result(stmt, col); - col_str.and_then(|txt: String| { - match time::strptime(&txt, SQLITE_DATETIME_FMT) { - Ok(tm) => Ok(tm.to_timespec()), - Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), - } - }) - } - ffi::SQLITE_INTEGER => Ok(time::Timespec::new(ffi::sqlite3_column_int64(stmt, col), 0)), - ffi::SQLITE_FLOAT => { - let mut jd = ffi::sqlite3_column_double(stmt, col); - jd -= JULIAN_DAY; - jd *= DAY_IN_SECONDS; - let ns = jd.fract() * 10f64.powi(9); - Ok(time::Timespec::new(jd as i64, ns as i32)) - } - _ => Err(Error::InvalidColumnType), + let s = try!(String::column_result(stmt, col)); + match time::strptime(&s, SQLITE_DATETIME_FMT) { + Ok(tm) => Ok(tm.to_timespec()), + Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), } } + + unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool { + String::column_has_valid_sqlite_type(stmt, col) + } } #[cfg(test)] @@ -63,15 +48,9 @@ mod test { sec: 10_000, nsec: 0, }; - db.execute("INSERT INTO foo(t, i) VALUES (?, ?)", &[&ts, &ts.sec]).unwrap(); - db.execute("UPDATE foo SET f = julianday(t)", &[]).unwrap(); + db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap(); let from: time::Timespec = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(from, ts); - let from: time::Timespec = db.query_row("SELECT i FROM foo", &[], |r| r.get(0)).unwrap(); - assert_eq!(from, ts); - // `Timespec { sec: 9999, nsec: 999994039 }` vs `Timespec{ sec: 10000, nsec: 0 }` - let from: time::Timespec = db.query_row("SELECT f FROM foo", &[], |r| r.get(0)).unwrap(); - assert!((from.sec - ts.sec).abs() <= 1); } } From aa2b3b26bd11492572a9d63369df8e6cd3300333 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 May 2016 22:39:18 -0500 Subject: [PATCH 6/6] Add chrono note to Changelog --- Changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Changelog.md b/Changelog.md index ad5ca25..e000549 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,6 @@ # Version UPCOMING (...) +* Adds support for serializing types from the `chrono` crate. Requires the `chrono` feature. * Removes `load_extension` feature from `libsqlite3-sys`. `load_extension` is still available on rusqlite itself. * Fixes crash on nightly Rust when using the `trace` feature.