Skip to content

Commit

Permalink
Merge #642
Browse files Browse the repository at this point in the history
642: Avoid outdated chrono functions r=jonasbb a=jonasbb



Co-authored-by: Jonas Bushart <[email protected]>
  • Loading branch information
bors[bot] and jonasbb authored Sep 14, 2023
2 parents 160c70d + 22b6c2f commit ae1cc48
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ jobs:
# Extra toolchain to test no_std
target: thumbv7em-none-eabihf

# Test that the project also compiles with the newest dependencies.
# On nightly there is little cache reuse, so there is not much overhead
# in constantly updating the versions.
- name: Bump dependencies on nightly
if: matrix.rust == 'nightly'
run: cargo update
# # Test that the project also compiles with the newest dependencies.
# # On nightly there is little cache reuse, so there is not much overhead
# # in constantly updating the versions.
# - name: Bump dependencies on nightly
# if: matrix.rust == 'nightly'
# run: cargo update

# Build the project
- name: "Build (${{ matrix.os }} / ${{ matrix.rust }})"
Expand Down
16 changes: 8 additions & 8 deletions serde_with/src/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use ::chrono_0_4::{DateTime, Duration, NaiveDateTime, TimeZone, Utc};

/// Create a [`DateTime`] for the Unix Epoch using the [`Utc`] timezone
fn unix_epoch_utc() -> DateTime<Utc> {
DateTime::<Utc>::from_utc(unix_epoch_naive(), Utc)
Utc.from_utc_datetime(&unix_epoch_naive())
}

/// Create a [`DateTime`] for the Unix Epoch using the [`Local`] timezone
#[cfg(feature = "std")]
fn unix_epoch_local() -> DateTime<Local> {
unix_epoch_utc().with_timezone(&Local)
Local.from_utc_datetime(&unix_epoch_naive())
}

/// Create a [`NaiveDateTime`] for the Unix Epoch
Expand Down Expand Up @@ -76,7 +76,7 @@ pub mod datetime_utc_ts_seconds_from_any {
{
let ndt = NaiveDateTime::from_timestamp_opt(value, 0);
if let Some(ndt) = ndt {
Ok(DateTime::<Utc>::from_utc(ndt, Utc))
Ok(Utc.from_utc_datetime(&ndt))
} else {
Err(DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
Expand All @@ -90,7 +90,7 @@ pub mod datetime_utc_ts_seconds_from_any {
{
let ndt = NaiveDateTime::from_timestamp_opt(value as i64, 0);
if let Some(ndt) = ndt {
Ok(DateTime::<Utc>::from_utc(ndt, Utc))
Ok(Utc.from_utc_datetime(&ndt))
} else {
Err(DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
Expand All @@ -106,7 +106,7 @@ pub mod datetime_utc_ts_seconds_from_any {
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(DateTime::<Utc>::from_utc(ndt, Utc))
Ok(Utc.from_utc_datetime(&ndt))
} else {
Err(DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
Expand All @@ -125,7 +125,7 @@ pub mod datetime_utc_ts_seconds_from_any {
if let Ok(seconds) = seconds.parse() {
let ndt = NaiveDateTime::from_timestamp_opt(seconds, 0);
if let Some(ndt) = ndt {
Ok(DateTime::<Utc>::from_utc(ndt, Utc))
Ok(Utc.from_utc_datetime(&ndt))
} else {
Err(DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
Expand All @@ -149,7 +149,7 @@ pub mod datetime_utc_ts_seconds_from_any {
subseconds *= 10u32.pow(9 - subseclen);
let ndt = NaiveDateTime::from_timestamp_opt(seconds, subseconds);
if let Some(ndt) = ndt {
Ok(DateTime::<Utc>::from_utc(ndt, Utc))
Ok(Utc.from_utc_datetime(&ndt))
} else {
Err(DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
Expand Down Expand Up @@ -177,7 +177,7 @@ impl SerializeAs<NaiveDateTime> for DateTime<Utc> {
where
S: Serializer,
{
let datetime = DateTime::<Utc>::from_utc(*source, Utc);
let datetime = Utc.from_utc_datetime(source);
datetime.serialize(serializer)
}
}
Expand Down
13 changes: 6 additions & 7 deletions serde_with/tests/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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, Utc};
use chrono_0_4::{DateTime, Duration, Local, NaiveDateTime, TimeZone, Utc};
use core::{iter::FromIterator, str::FromStr};
use expect_test::expect;
use serde::{Deserialize, Serialize};
Expand All @@ -27,7 +27,7 @@ use serde_with::{
};

fn new_datetime(secs: i64, nsecs: u32) -> DateTime<Utc> {
DateTime::from_utc(NaiveDateTime::from_timestamp_opt(secs, nsecs).unwrap(), Utc)
Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(secs, nsecs).unwrap())
}

#[test]
Expand Down Expand Up @@ -360,7 +360,7 @@ fn test_chrono_duration_seconds_with_frac() {

#[test]
fn test_chrono_timestamp_seconds() {
let zero = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc);
let zero = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap());
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 @@ -498,7 +498,7 @@ fn test_chrono_timestamp_seconds() {

#[test]
fn test_chrono_timestamp_seconds_with_frac() {
let zero = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc);
let zero = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap());
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 @@ -634,7 +634,7 @@ fn test_duration_smoketest() {

#[test]
fn test_datetime_utc_smoketest() {
let zero = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc);
let zero = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap());
let one_second = zero + Duration::seconds(1);

smoketest! {
Expand Down Expand Up @@ -670,8 +670,7 @@ fn test_datetime_utc_smoketest() {

#[test]
fn test_datetime_local_smoketest() {
let zero = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc)
.with_timezone(&Local);
let zero = Local.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap());
let one_second = zero + Duration::seconds(1);

smoketest! {
Expand Down

0 comments on commit ae1cc48

Please sign in to comment.