Skip to content

Commit

Permalink
Replace future deprecated functions from chrono (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb authored May 22, 2024
2 parents 9593f93 + 685338f commit 729e8d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
57 changes: 21 additions & 36 deletions serde_with/src/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>`.
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand Down
24 changes: 17 additions & 7 deletions serde_with/tests/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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! {
Expand Down Expand Up @@ -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! {
Expand Down Expand Up @@ -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! {
Expand Down

0 comments on commit 729e8d2

Please sign in to comment.