Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5d6beda

Browse files
committedMar 29, 2024
Add FromSql/ToSql<Timestamp> to Pg backend for OffsetDateTime
1 parent a5c82df commit 5d6beda

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
 

‎diesel/src/pg/types/date_and_time/time.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ use crate::sql_types::{Date, Time, Timestamp, Timestamptz};
1717
// Postgres timestamps start from January 1st 2000.
1818
const PG_EPOCH: PrimitiveDateTime = datetime!(2000-1-1 0:00:00);
1919

20+
fn to_primitive_datetime(dt: OffsetDateTime) -> PrimitiveDateTime {
21+
let dt = dt.to_offset(UtcOffset::UTC);
22+
PrimitiveDateTime::new(dt.date(), dt.time())
23+
}
24+
2025
#[cfg(all(feature = "time", feature = "postgres_backend"))]
2126
impl FromSql<Timestamp, Pg> for PrimitiveDateTime {
2227
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
@@ -44,6 +49,23 @@ impl ToSql<Timestamp, Pg> for PrimitiveDateTime {
4449
}
4550
}
4651

52+
// Delegate offset datetimes in terms of UTC primitive datetimes; this stores everything in the DB as UTC
53+
#[cfg(all(feature = "time", feature = "postgres_backend"))]
54+
impl ToSql<Timestamp, Pg> for OffsetDateTime {
55+
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
56+
let prim = to_primitive_datetime(*self);
57+
<PrimitiveDateTime as ToSql<Timestamp, Pg>>::to_sql(&prim, &mut out.reborrow())
58+
}
59+
}
60+
61+
#[cfg(all(feature = "time", feature = "postgres_backend"))]
62+
impl FromSql<Timestamp, Pg> for OffsetDateTime {
63+
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
64+
let prim = <PrimitiveDateTime as FromSql<Timestamp, Pg>>::from_sql(bytes)?;
65+
Ok(prim.assume_utc())
66+
}
67+
}
68+
4769
#[cfg(all(feature = "time", feature = "postgres_backend"))]
4870
impl FromSql<Timestamptz, Pg> for PrimitiveDateTime {
4971
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {

‎diesel/src/sql_types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,14 @@ pub struct Time;
331331
/// - [`std::time::SystemTime`][SystemTime] (PG only)
332332
/// - [`chrono::NaiveDateTime`][NaiveDateTime] with `feature = "chrono"`
333333
/// - [`time::PrimitiveDateTime`] with `feature = "time"`
334-
/// - [`time::OffsetDateTime`] with `feature = "time"` (MySQL only)
334+
/// - [`time::OffsetDateTime`] with `feature = "time"`
335335
///
336336
/// ### [`FromSql`](crate::deserialize::FromSql) impls
337337
///
338338
/// - [`std::time::SystemTime`][SystemTime] (PG only)
339339
/// - [`chrono::NaiveDateTime`][NaiveDateTime] with `feature = "chrono"`
340340
/// - [`time::PrimitiveDateTime`] with `feature = "time"`
341-
/// - [`time::OffsetDateTime`] with `feature = "time"` (MySQL only)
341+
/// - [`time::OffsetDateTime`] with `feature = "time"`
342342
///
343343
/// [SystemTime]: std::time::SystemTime
344344
#[cfg_attr(

0 commit comments

Comments
 (0)
Please sign in to comment.