Skip to content

Commit

Permalink
Fix FromSql impl for OffsetDateTime
Browse files Browse the repository at this point in the history
Fix test doc
  • Loading branch information
gwenn committed Jun 12, 2021
1 parent 125f5df commit bcf28fe
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/types/mod.rs
Expand Up @@ -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())
}
}
```
Expand Down

0 comments on commit bcf28fe

Please sign in to comment.