mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 11:31:37 +08:00
Fix FromSql impl for OffsetDateTime
Fix test doc
This commit is contained in:
parent
125f5dfbde
commit
bcf28fec35
@ -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<Self> {
|
||||
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<ToSqlOutput> {
|
||||
Ok(self.0.timestamp().into())
|
||||
Ok(self.0.unix_timestamp().into())
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user