Merge pull request #319 from traviscross/fix/datetime-format-rfc3339

Fix date/time format for SQLite, use RFC 3339
This commit is contained in:
gwenn 2018-04-03 19:45:44 +02:00 committed by GitHub
commit 7f6d7f3346
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View File

@ -10,12 +10,13 @@
//! * Strings (`String` and `&str`) //! * Strings (`String` and `&str`)
//! * Blobs (`Vec<u8>` and `&[u8]`) //! * Blobs (`Vec<u8>` and `&[u8]`)
//! //!
//! Additionally, because it is such a common data type, implementations are provided for //! Additionally, because it is such a common data type, implementations are
//! `time::Timespec` that use a string for storage (using the same format string, //! provided for `time::Timespec` that use the RFC 3339 date/time format,
//! `"%Y-%m-%d %H:%M:%S"`, as SQLite's builtin //! `"%Y-%m-%dT%H:%M:%S.%fZ"`, to store time values as strings. These values
//! [datetime](https://www.sqlite.org/lang_datefunc.html) function. Note that this storage //! can be parsed by SQLite's builtin
//! truncates timespecs to the nearest second. If you want different storage for timespecs, you can //! [datetime](https://www.sqlite.org/lang_datefunc.html) functions. If you
//! use a newtype. For example, to store timespecs as `f64`s: //! want different storage for timespecs, you can use a newtype. For example, to
//! store timespecs as `f64`s:
//! //!
//! ```rust //! ```rust
//! extern crate rusqlite; //! extern crate rusqlite;

View File

@ -3,7 +3,8 @@ extern crate time;
use Result; use Result;
use types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; use types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
const SQLITE_DATETIME_FMT: &str = "%Y-%m-%d %H:%M:%S:%f %Z"; const SQLITE_DATETIME_FMT: &str = "%Y-%m-%dT%H:%M:%S.%fZ";
const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%f %Z";
impl ToSql for time::Timespec { impl ToSql for time::Timespec {
fn to_sql(&self) -> Result<ToSqlOutput> { fn to_sql(&self) -> Result<ToSqlOutput> {
@ -19,10 +20,12 @@ impl FromSql for time::Timespec {
fn column_result(value: ValueRef) -> FromSqlResult<Self> { fn column_result(value: ValueRef) -> FromSqlResult<Self> {
value value
.as_str() .as_str()
.and_then(|s| match time::strptime(s, SQLITE_DATETIME_FMT) { .and_then(|s| {
Ok(tm) => Ok(tm.to_timespec()), time::strptime(s, SQLITE_DATETIME_FMT)
Err(err) => Err(FromSqlError::Other(Box::new(err))), .or_else(|err| {
}) time::strptime(s, SQLITE_DATETIME_FMT_LEGACY)
.or(Err(FromSqlError::Other(Box::new(err))))})})
.map(|tm| tm.to_timespec())
} }
} }