Repository: jonasbb/serde_with
Pull request #752 (2024-05-22 21:28:06)

Replace future deprecated functions from chrono


Patch:
diff --git a/serde_with/src/chrono_0_4.rs b/serde_with/src/chrono_0_4.rs
index 43fa322..250f029 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<Local> {
 
 /// 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<Utc>`.
@@ -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<E>(self, value: u64) -> Result<Self::Value, E>
             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<E>(self, value: f64) -> Result<Self::Value, E>
@@ -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<E>(self, value: &str) -> Result<Self::Value, E>
@@ -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))
                             }


Test patch:
diff --git a/serde_with/tests/chrono_0_4.rs b/serde_with/tests/chrono_0_4.rs
index f1c5ee4..5dd72b4 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> {
-    Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(secs, nsecs).unwrap())
+    DateTime::from_timestamp(secs, nsecs).unwrap()
+}
+
+fn unix_epoch_local() -> DateTime<Local> {
+    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! {