From 685338f44f01eaefdbdb40fc7a636d113e693916 Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Wed, 22 May 2024 23:26:52 +0200 Subject: [PATCH] Replace future deprecated functions from chrono --- serde_with/src/chrono_0_4.rs | 57 +++++++++++++--------------------- serde_with/tests/chrono_0_4.rs | 24 +++++++++----- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/serde_with/src/chrono_0_4.rs b/serde_with/src/chrono_0_4.rs index 43fa322e..250f029b 100644 --- a/serde_with/src/chrono_0_4.rs +++ b/serde_with/src/chrono_0_4.rs @@ -25,7 +25,7 @@ fn unix_epoch_local() -> DateTime { /// Create a [`NaiveDateTime`] for the Unix Epoch fn unix_epoch_naive() -> NaiveDateTime { - NaiveDateTime::from_timestamp_opt(0, 0).unwrap() + DateTime::from_timestamp(0, 0).unwrap().naive_utc() } /// Deserialize a Unix timestamp with optional sub-second precision into a `DateTime`. @@ -76,28 +76,22 @@ pub mod datetime_utc_ts_seconds_from_any { where E: DeError, { - let ndt = NaiveDateTime::from_timestamp_opt(value, 0); - if let Some(ndt) = ndt { - Ok(Utc.from_utc_datetime(&ndt)) - } else { - Err(DeError::custom(format_args!( + DateTime::from_timestamp(value, 0).ok_or_else(|| { + DeError::custom(format_args!( "a timestamp which can be represented in a DateTime but received '{value}'" - ))) - } + )) + }) } fn visit_u64(self, value: u64) -> Result where E: DeError, { - let ndt = NaiveDateTime::from_timestamp_opt(value as i64, 0); - if let Some(ndt) = ndt { - Ok(Utc.from_utc_datetime(&ndt)) - } else { - Err(DeError::custom(format_args!( + DateTime::from_timestamp(value as i64, 0).ok_or_else(|| { + DeError::custom(format_args!( "a timestamp which can be represented in a DateTime but received '{value}'" - ))) - } + )) + }) } fn visit_f64(self, value: f64) -> Result @@ -106,14 +100,11 @@ pub mod datetime_utc_ts_seconds_from_any { { let seconds = value.trunc() as i64; let nsecs = (value.fract() * 1_000_000_000_f64).abs() as u32; - let ndt = NaiveDateTime::from_timestamp_opt(seconds, nsecs); - if let Some(ndt) = ndt { - Ok(Utc.from_utc_datetime(&ndt)) - } else { - Err(DeError::custom(format_args!( + DateTime::from_timestamp(seconds, nsecs).ok_or_else(|| { + DeError::custom(format_args!( "a timestamp which can be represented in a DateTime but received '{value}'" - ))) - } + )) + }) } fn visit_str(self, value: &str) -> Result @@ -125,14 +116,11 @@ pub mod datetime_utc_ts_seconds_from_any { match *parts.as_slice() { [seconds] => { if let Ok(seconds) = seconds.parse() { - let ndt = NaiveDateTime::from_timestamp_opt(seconds, 0); - if let Some(ndt) = ndt { - Ok(Utc.from_utc_datetime(&ndt)) - } else { - Err(DeError::custom(format_args!( + DateTime::from_timestamp(seconds, 0).ok_or_else(|| { + DeError::custom(format_args!( "a timestamp which can be represented in a DateTime but received '{value}'" - ))) - } + )) + }) } else { Err(DeError::invalid_value(Unexpected::Str(value), &self)) } @@ -149,14 +137,11 @@ pub mod datetime_utc_ts_seconds_from_any { if let Ok(mut subseconds) = subseconds.parse() { // convert subseconds to nanoseconds (10^-9), require 9 places for nanoseconds subseconds *= 10u32.pow(9 - subseclen); - let ndt = NaiveDateTime::from_timestamp_opt(seconds, subseconds); - if let Some(ndt) = ndt { - Ok(Utc.from_utc_datetime(&ndt)) - } else { - Err(DeError::custom(format_args!( + DateTime::from_timestamp(seconds, subseconds).ok_or_else(|| { + DeError::custom(format_args!( "a timestamp which can be represented in a DateTime but received '{value}'" - ))) - } + )) + }) } else { Err(DeError::invalid_value(Unexpected::Str(value), &self)) } diff --git a/serde_with/tests/chrono_0_4.rs b/serde_with/tests/chrono_0_4.rs index f1c5ee40..5dd72b43 100644 --- a/serde_with/tests/chrono_0_4.rs +++ b/serde_with/tests/chrono_0_4.rs @@ -6,7 +6,7 @@ use crate::utils::{ check_deserialization, check_error_deserialization, check_serialization, is_equal, }; use alloc::collections::BTreeMap; -use chrono_0_4::{DateTime, Duration, Local, NaiveDateTime, TimeZone, Utc}; +use chrono_0_4::{DateTime, Duration, Local, NaiveDateTime, Utc}; use core::str::FromStr; use expect_test::expect; use serde::{Deserialize, Serialize}; @@ -19,7 +19,17 @@ use serde_with::{ }; fn new_datetime(secs: i64, nsecs: u32) -> DateTime { - Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(secs, nsecs).unwrap()) + DateTime::from_timestamp(secs, nsecs).unwrap() +} + +fn unix_epoch_local() -> DateTime { + DateTime::from_timestamp(0, 0) + .unwrap() + .with_timezone(&Local) +} + +fn unix_epoch_naive() -> NaiveDateTime { + DateTime::from_timestamp(0, 0).unwrap().naive_utc() } #[test] @@ -352,7 +362,7 @@ fn test_chrono_duration_seconds_with_frac() { #[test] fn test_chrono_timestamp_seconds() { - let zero = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap()); + let zero = new_datetime(0, 0); let one_second = zero + Duration::seconds(1); let half_second = zero + Duration::nanoseconds(500_000_000); let minus_one_second = zero - Duration::seconds(1); @@ -490,7 +500,7 @@ fn test_chrono_timestamp_seconds() { #[test] fn test_chrono_timestamp_seconds_with_frac() { - let zero = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap()); + let zero = new_datetime(0, 0); let one_second = zero + Duration::seconds(1); let half_second = zero + Duration::nanoseconds(500_000_000); let minus_one_second = zero - Duration::seconds(1); @@ -626,7 +636,7 @@ fn test_duration_smoketest() { #[test] fn test_datetime_utc_smoketest() { - let zero = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap()); + let zero = new_datetime(0, 0); let one_second = zero + Duration::seconds(1); smoketest! { @@ -662,7 +672,7 @@ fn test_datetime_utc_smoketest() { #[test] fn test_datetime_local_smoketest() { - let zero = Local.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap()); + let zero = unix_epoch_local(); let one_second = zero + Duration::seconds(1); smoketest! { @@ -698,7 +708,7 @@ fn test_datetime_local_smoketest() { #[test] fn test_naive_datetime_smoketest() { - let zero = NaiveDateTime::from_timestamp_opt(0, 0).unwrap(); + let zero = unix_epoch_naive(); let one_second = zero + Duration::seconds(1); smoketest! {