mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
implement FromSql for chrono::DateTime<FixedOffset>
This commit is contained in:
parent
53e74a3f09
commit
289b4e45b2
@ -145,13 +145,26 @@ impl FromSql for DateTime<Local> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// RFC3339 ("YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM") into `DateTime<FixedOffset>`.
|
||||||
|
impl FromSql for DateTime<FixedOffset> {
|
||||||
|
#[inline]
|
||||||
|
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||||
|
let s = String::column_result(value)?;
|
||||||
|
Self::parse_from_rfc3339(s.as_str())
|
||||||
|
.or_else(|_| Self::parse_from_str(s.as_str(), "%F %T%.f%:z"))
|
||||||
|
.map_err(|e| FromSqlError::Other(Box::new(e)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::{
|
use crate::{
|
||||||
types::{FromSql, ValueRef},
|
types::{FromSql, ValueRef},
|
||||||
Connection, Result,
|
Connection, Result,
|
||||||
};
|
};
|
||||||
use chrono::{DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
|
use chrono::{
|
||||||
|
DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc,
|
||||||
|
};
|
||||||
|
|
||||||
fn checked_memory_handle() -> Result<Connection> {
|
fn checked_memory_handle() -> Result<Connection> {
|
||||||
let db = Connection::open_in_memory()?;
|
let db = Connection::open_in_memory()?;
|
||||||
@ -264,7 +277,8 @@ mod test {
|
|||||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||||
assert!(s.ends_with("+04:00"));
|
assert!(s.ends_with("+04:00"));
|
||||||
|
|
||||||
let v: DateTime<Local> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let v: DateTime<FixedOffset> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||||
|
assert_eq!(time.offset(), v.offset());
|
||||||
assert_eq!(time, v);
|
assert_eq!(time, v);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user