fix: replace use of deprecated chrono APIs

This commit is contained in:
Austin Bonander
2023-03-16 16:35:03 -07:00
committed by Austin Bonander
parent 0f6c377c12
commit ec60b1d32d
5 changed files with 65 additions and 34 deletions

View File

@@ -21,7 +21,7 @@ impl PgHasArrayType for NaiveDate {
impl Encode<'_, Postgres> for NaiveDate {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
// DATE is encoded as the days since epoch
let days = (*self - NaiveDate::from_ymd(2000, 1, 1)).num_days() as i32;
let days = (*self - postgres_epoch_date()).num_days() as i32;
Encode::<Postgres>::encode(&days, buf)
}
@@ -36,10 +36,15 @@ impl<'r> Decode<'r, Postgres> for NaiveDate {
PgValueFormat::Binary => {
// DATE is encoded as the days since epoch
let days: i32 = Decode::<Postgres>::decode(value)?;
NaiveDate::from_ymd(2000, 1, 1) + Duration::days(days.into())
postgres_epoch_date() + Duration::days(days.into())
}
PgValueFormat::Text => NaiveDate::parse_from_str(value.as_str()?, "%Y-%m-%d")?,
})
}
}
#[inline]
fn postgres_epoch_date() -> NaiveDate {
NaiveDate::from_ymd_opt(2000, 1, 1).expect("expected 2000-01-01 to be a valid NaiveDate")
}

View File

@@ -36,8 +36,7 @@ impl Encode<'_, Postgres> for NaiveDateTime {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
// FIXME: We should *really* be returning an error, Encode needs to be fallible
// TIMESTAMP is encoded as the microseconds since the epoch
let epoch = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0);
let us = (*self - epoch)
let us = (*self - postgres_epoch_datetime())
.num_microseconds()
.unwrap_or_else(|| panic!("NaiveDateTime out of range for Postgres: {:?}", self));
@@ -54,9 +53,8 @@ impl<'r> Decode<'r, Postgres> for NaiveDateTime {
Ok(match value.format() {
PgValueFormat::Binary => {
// TIMESTAMP is encoded as the microseconds since the epoch
let epoch = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0);
let us = Decode::<Postgres>::decode(value)?;
epoch + Duration::microseconds(us)
postgres_epoch_datetime() + Duration::microseconds(us)
}
PgValueFormat::Text => {
@@ -107,3 +105,11 @@ impl<'r> Decode<'r, Postgres> for DateTime<FixedOffset> {
Ok(Utc.fix().from_utc_datetime(&naive))
}
}
#[inline]
fn postgres_epoch_datetime() -> NaiveDateTime {
NaiveDate::from_ymd_opt(2000, 1, 1)
.expect("expected 2000-01-01 to be a valid NaiveDate")
.and_hms_opt(0, 0, 0)
.expect("expected 2000-01-01T00:00:00 to be a valid NaiveDateTime")
}

View File

@@ -22,9 +22,7 @@ impl Encode<'_, Postgres> for NaiveTime {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
// TIME is encoded as the microseconds since midnight
// NOTE: panic! is on overflow and 1 day does not have enough micros to overflow
let us = (*self - NaiveTime::from_hms(0, 0, 0))
.num_microseconds()
.unwrap();
let us = (*self - NaiveTime::default()).num_microseconds().unwrap();
Encode::<Postgres>::encode(&us, buf)
}
@@ -40,10 +38,20 @@ impl<'r> Decode<'r, Postgres> for NaiveTime {
PgValueFormat::Binary => {
// TIME is encoded as the microseconds since midnight
let us: i64 = Decode::<Postgres>::decode(value)?;
NaiveTime::from_hms(0, 0, 0) + Duration::microseconds(us)
NaiveTime::default() + Duration::microseconds(us)
}
PgValueFormat::Text => NaiveTime::parse_from_str(value.as_str()?, "%H:%M:%S%.f")?,
})
}
}
#[test]
fn check_naive_time_default_is_midnight() {
// Just a canary in case this changes.
assert_eq!(
NaiveTime::from_hms_opt(0, 0, 0),
Some(NaiveTime::default()),
"implementation assumes `NaiveTime::default()` equals midnight"
);
}

View File

@@ -71,15 +71,20 @@ mod chrono {
// TIME is encoded as the microseconds since midnight
let us = buf.read_i64::<BigEndian>()?;
let time = NaiveTime::from_hms(0, 0, 0) + Duration::microseconds(us);
// default is midnight, there is a canary test for this
// in `sqlx-postgres/src/types/chrono/time.rs`
let time = NaiveTime::default() + Duration::microseconds(us);
// OFFSET is encoded as seconds from UTC
let seconds = buf.read_i32::<BigEndian>()?;
let offset_seconds = buf.read_i32::<BigEndian>()?;
Ok(PgTimeTz {
time,
offset: FixedOffset::west(seconds),
})
let offset = FixedOffset::west_opt(offset_seconds).ok_or_else(|| {
format!(
"server returned out-of-range offset for `TIMETZ`: {offset_seconds} seconds"
)
})?;
Ok(PgTimeTz { time, offset })
}
PgValueFormat::Text => {