From 635dba5b2682033101a1271e9fb4bf2516c0b840 Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Mon, 11 Mar 2024 21:14:18 -0700 Subject: [PATCH] fix: deprecation in `postgres::types::chrono` --- sqlx-postgres/src/types/chrono/date.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sqlx-postgres/src/types/chrono/date.rs b/sqlx-postgres/src/types/chrono/date.rs index 4e968e10..da10bd1a 100644 --- a/sqlx-postgres/src/types/chrono/date.rs +++ b/sqlx-postgres/src/types/chrono/date.rs @@ -1,10 +1,12 @@ +use std::mem; + +use chrono::{NaiveDate, TimeDelta}; + use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::Type; use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; -use chrono::{Duration, NaiveDate}; -use std::mem; impl Type for NaiveDate { fn type_info() -> PgTypeInfo { @@ -36,7 +38,13 @@ impl<'r> Decode<'r, Postgres> for NaiveDate { PgValueFormat::Binary => { // DATE is encoded as the days since epoch let days: i32 = Decode::::decode(value)?; - postgres_epoch_date() + Duration::days(days.into()) + + let days = TimeDelta::try_days(days.into()) + .unwrap_or_else(|| { + unreachable!("BUG: days ({days}) as `i32` multiplied into seconds should not overflow `i64`") + }); + + postgres_epoch_date() + days } PgValueFormat::Text => NaiveDate::parse_from_str(value.as_str()?, "%Y-%m-%d")?,