Skip to content

Commit 635dba5

Browse files
committed
fix: deprecation in postgres::types::chrono
1 parent a2b89d7 commit 635dba5

File tree

1 file changed

+11
-3
lines changed
  • sqlx-postgres/src/types/chrono

1 file changed

+11
-3
lines changed

sqlx-postgres/src/types/chrono/date.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::mem;
2+
3+
use chrono::{NaiveDate, TimeDelta};
4+
15
use crate::decode::Decode;
26
use crate::encode::{Encode, IsNull};
37
use crate::error::BoxDynError;
48
use crate::types::Type;
59
use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
6-
use chrono::{Duration, NaiveDate};
7-
use std::mem;
810

911
impl Type<Postgres> for NaiveDate {
1012
fn type_info() -> PgTypeInfo {
@@ -36,7 +38,13 @@ impl<'r> Decode<'r, Postgres> for NaiveDate {
3638
PgValueFormat::Binary => {
3739
// DATE is encoded as the days since epoch
3840
let days: i32 = Decode::<Postgres>::decode(value)?;
39-
postgres_epoch_date() + Duration::days(days.into())
41+
42+
let days = TimeDelta::try_days(days.into())
43+
.unwrap_or_else(|| {
44+
unreachable!("BUG: days ({days}) as `i32` multiplied into seconds should not overflow `i64`")
45+
});
46+
47+
postgres_epoch_date() + days
4048
}
4149

4250
PgValueFormat::Text => NaiveDate::parse_from_str(value.as_str()?, "%Y-%m-%d")?,

0 commit comments

Comments
 (0)