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).
This commit is contained in:
John Gallagher 2016-05-15 22:23:02 -05:00
parent 42bc173009
commit 07801ca370

View File

@ -1,19 +1,13 @@
//! Convert most of the [Time Strings](http://sqlite.org/lang_datefunc.html) to chrono types. //! Convert most of the [Time Strings](http://sqlite.org/lang_datefunc.html) to chrono types.
extern crate chrono; extern crate chrono;
use std::error;
use self::chrono::{NaiveDate, NaiveTime, NaiveDateTime, DateTime, TimeZone, UTC, Local}; use self::chrono::{NaiveDate, NaiveTime, NaiveDateTime, DateTime, TimeZone, UTC, Local};
use libc::c_int; use libc::c_int;
use {Error, Result}; use {Error, Result};
use types::{FromSql, ToSql}; use types::{FromSql, ToSql};
use ffi;
use ffi::sqlite3_stmt; 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" /// ISO 8601 calendar date without timezone => "YYYY-MM-DD"
impl ToSql for NaiveDate { 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" /// ISO 8601 combined date and time without timezone => "YYYY-MM-DD HH:MM:SS.SSS"
impl ToSql for NaiveDateTime { impl ToSql for NaiveDateTime {
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { 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) 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-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS" => 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-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported)
impl FromSql for NaiveDateTime { impl FromSql for NaiveDateTime {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<NaiveDateTime> { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<NaiveDateTime> {
match sqlite3_column_type(stmt, col) {
ffi::SQLITE_TEXT => {
let s = try!(String::column_result(stmt, col)); let s = try!(String::column_result(stmt, col));
let fmt = match s.len() {
16 => { let fmt = if s.len() >= 11 && s.as_bytes()[10] == b'T' {
match s.as_bytes()[10] { "%Y-%m-%dT%H:%M:%S%.f"
b'T' => "%Y-%m-%dT%H:%M", } else {
_ => "%Y-%m-%d %H:%M", "%Y-%m-%d %H:%M:%S%.f"
}
}
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) { match NaiveDateTime::parse_from_str(&s, fmt) {
Ok(dt) => Ok(dt), Ok(dt) => Ok(dt),
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
} }
} }
ffi::SQLITE_INTEGER => {
match NaiveDateTime::from_timestamp_opt(ffi::sqlite3_column_int64(stmt, col), 0) { unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
Some(dt) => Ok(dt), String::column_has_valid_sqlite_type(stmt, col)
None => {
let err: Box<error::Error + Sync + Send> = "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<error::Error + Sync + Send> = "out-of-range number of \
seconds and/or invalid \
nanosecond"
.into();
Err(Error::FromSqlConversionFailure(err))
}
}
}
_ => Err(Error::InvalidColumnType),
}
} }
} }
/// ISO 8601 date and time with time zone => "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM" /// Date and time with time zone => RFC3339 timestamp ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM").
impl ToSql for DateTime<UTC> { impl<Tz: TimeZone> ToSql for DateTime<Tz> where Tz::Offset: ::std::fmt::Display {
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int { 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(); self.to_rfc3339().bind_parameter(stmt, col)
date_str.bind_parameter(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. /// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime<UTC>.
/// ("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.
impl FromSql for DateTime<UTC> { impl FromSql for DateTime<UTC> {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<UTC>> { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<UTC>> {
match sqlite3_column_type(stmt, col) { let s = try!(String::column_result(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) { match DateTime::parse_from_rfc3339(&s) {
Ok(dt) => Ok(dt.with_timezone(&UTC)), Ok(dt) => Ok(dt.with_timezone(&UTC)),
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))), Err(_) => NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt)),
}
} 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)) unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
} String::column_has_valid_sqlite_type(stmt, col)
ffi::SQLITE_FLOAT => {
NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt))
}
_ => Err(Error::InvalidColumnType),
}
} }
} }
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime<Local>.
/// ISO 8601 date and time with time zone => "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"
impl ToSql for DateTime<Local> {
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)
}
}
/// "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.
impl FromSql for DateTime<Local> { impl FromSql for DateTime<Local> {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<Local>> { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<Local>> {
match sqlite3_column_type(stmt, col) { let utc_dt = try!(DateTime::<UTC>::column_result(stmt, col));
ffi::SQLITE_TEXT => { Ok(utc_dt.with_timezone(&Local))
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),
} }
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
DateTime::<UTC>::column_has_valid_sqlite_type(stmt, col)
} }
} }
// struct UnixTime(NaiveDateTime);
// struct JulianTime(NaiveDateTime)
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use Connection; use Connection;
@ -264,117 +167,48 @@ mod test {
let time = NaiveTime::from_hms(23, 56, 4); let time = NaiveTime::from_hms(23, 56, 4);
let dt = NaiveDateTime::new(date, time); 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("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(); 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(); let v: NaiveDateTime = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
assert_eq!(dt, v); 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" 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(); let hms: NaiveDateTime = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap();
assert_eq!(dt, hms); 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] #[test]
fn test_date_time_utc() { fn test_date_time_utc() {
let db = checked_memory_handle(); let db = checked_memory_handle();
let date = NaiveDate::from_ymd(2016, 2, 23); 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 dt = NaiveDateTime::new(date, time);
let utc = UTC.from_utc_datetime(&dt); let utc = UTC.from_utc_datetime(&dt);
db.execute("INSERT INTO foo (t) VALUES (?)", &[&utc]).unwrap(); 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(); 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<UTC> = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); let v: DateTime<UTC> = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
assert_eq!(utc, v); assert_eq!(utc, v);
let i: DateTime<UTC> = db.query_row("SELECT i FROM foo", &[], |r| r.get(0)).unwrap();
assert_eq!(utc, i);
} }
#[test] #[test]
fn test_date_time_local() { fn test_date_time_local() {
let db = checked_memory_handle(); let db = checked_memory_handle();
let date = NaiveDate::from_ymd(2016, 2, 23); 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 dt = NaiveDateTime::new(date, time);
let local = Local.from_local_datetime(&dt).single().unwrap(); let local = Local.from_local_datetime(&dt).single().unwrap();
db.execute("INSERT INTO foo (t) VALUES (?)", &[&local]).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(); 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 offset = Local.offset_from_utc_datetime(&dt);
assert_eq!(format!("2016-02-23T23:56:04.789{:}", offset), s);
let v: DateTime<Local> = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); let v: DateTime<Local> = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
assert_eq!(local, v); assert_eq!(local, v);
let i: DateTime<Local> = 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::<DateTime<UTC>>().unwrap();
"2016-05-04T17:00:42.862471Z".parse::<DateTime<UTC>>().unwrap();
"2016-05-04T17:00:42.862471".parse::<DateTime<UTC>>().unwrap_err(); // TooShort
} }
} }