From bcf28fec350b0ba60d7843b430854ebd36bd14de Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 12 Jun 2021 18:54:29 +0200 Subject: [PATCH] Fix FromSql impl for OffsetDateTime Fix test doc --- src/types/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/types/mod.rs b/src/types/mod.rs index 706be0c..5951747 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -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 { - 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 { - Ok(self.0.timestamp().into()) + Ok(self.0.unix_timestamp().into()) } } ```