Unverified Commit 9abad1ac authored by 82marbag's avatar 82marbag Committed by GitHub
Browse files

Do not use deprecated from_timestamp from chrono (#1980)

* Do not use deprecated from_timestamp from chrono

CI is failing because
[from_timestamp](https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp

)
is now deprecated.

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>

* Update changelog

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>

* Fix error

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>

* Use with_ymd_and_hms

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>
parent 7477cfbc
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -74,6 +74,12 @@ references = ["smithy-rs#1929"]
meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "all"}
author = "Velfi"

[[smithy-rs]]
message = "aws_smithy_types_convert::date_time::DateTimeExt::to_chrono_utc returns a Result<>"
references = ["smithy-rs#1980"]
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "all" }
author = "82marbag"

[[smithy-rs]]
message = "Fix cargo audit issue on chrono."
references = ["smithy-rs#1907"]
@@ -115,4 +121,3 @@ message = "Several breaking changes have been made to errors. See [the upgrade g
references = ["smithy-rs#1926", "smithy-rs#1819"]
meta = { "breaking" = true, "tada" = false, "bug" = false }
author = "jdisanti"
+44 −17
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ Then import [`DateTimeExt`] to use the conversions:
use aws_smithy_types_convert::date_time::DateTimeExt;
use chrono::{Utc};

let chrono_date_time: chrono::DateTime<Utc> = DateTime::from_secs(5).to_chrono_utc();
let chrono_date_time: chrono::DateTime<Utc> = DateTime::from_secs(5).to_chrono_utc().unwrap();
let date_time: DateTime = DateTime::from_chrono_utc(chrono_date_time);
```
"##
@@ -89,7 +89,7 @@ let date_time: DateTime = DateTime::from_chrono_utc(chrono_date_time);
pub trait DateTimeExt {
    /// Converts a [`DateTime`] to a [`chrono::DateTime`] with timezone UTC.
    #[cfg(feature = "convert-chrono")]
    fn to_chrono_utc(&self) -> chrono::DateTime<chrono::Utc>;
    fn to_chrono_utc(&self) -> Result<chrono::DateTime<chrono::Utc>, Error>;

    /// Converts a [`chrono::DateTime`] with timezone UTC to a [`DateTime`].
    #[cfg(feature = "convert-chrono")]
@@ -113,11 +113,19 @@ pub trait DateTimeExt {

impl DateTimeExt for DateTime {
    #[cfg(feature = "convert-chrono")]
    fn to_chrono_utc(&self) -> chrono::DateTime<chrono::Utc> {
        chrono::DateTime::<chrono::Utc>::from_utc(
            chrono::NaiveDateTime::from_timestamp(self.secs(), self.subsec_nanos()),
            chrono::Utc,
    fn to_chrono_utc(&self) -> Result<chrono::DateTime<chrono::Utc>, Error> {
        match chrono::NaiveDateTime::from_timestamp_opt(self.secs(), self.subsec_nanos()) {
            None => {
                let err: Box<dyn StdError + Send + Sync + 'static> = format!(
                    "Out-of-range seconds {} or invalid nanoseconds {}",
                    self.secs(),
                    self.subsec_nanos()
                )
                .into();
                Err(Error::OutOfRange(err))
            }
            Some(dt) => Ok(chrono::DateTime::<chrono::Utc>::from_utc(dt, chrono::Utc)),
        }
    }

    #[cfg(feature = "convert-chrono")]
@@ -147,6 +155,7 @@ impl DateTimeExt for DateTime {
mod test {
    use super::DateTimeExt;
    use aws_smithy_types::date_time::{DateTime, Format};
    use chrono::Timelike;

    #[cfg(feature = "convert-time")]
    use super::Error;
@@ -156,18 +165,28 @@ mod test {
    fn from_chrono() {
        use chrono::{FixedOffset, TimeZone, Utc};

        let chrono = Utc.ymd(2039, 7, 8).and_hms_nano(9, 3, 11, 123_000_000);
        let chrono = Utc
            .with_ymd_and_hms(2039, 7, 8, 9, 3, 11)
            .unwrap()
            .with_nanosecond(123_000_000)
            .unwrap();
        let expected = DateTime::from_str("2039-07-08T09:03:11.123Z", Format::DateTime).unwrap();
        assert_eq!(expected, DateTime::from_chrono_utc(chrono));

        let chrono = Utc.ymd(1000, 7, 8).and_hms_nano(9, 3, 11, 456_000_000);
        let chrono = Utc
            .with_ymd_and_hms(1000, 7, 8, 9, 3, 11)
            .unwrap()
            .with_nanosecond(456_000_000)
            .unwrap();
        let expected = DateTime::from_str("1000-07-08T09:03:11.456Z", Format::DateTime).unwrap();
        assert_eq!(expected, DateTime::from_chrono_utc(chrono));

        let chrono =
            FixedOffset::west(2 * 3600)
                .ymd(2039, 7, 8)
                .and_hms_nano(9, 3, 11, 123_000_000);
        let chrono = FixedOffset::west_opt(2 * 3600)
            .unwrap()
            .with_ymd_and_hms(2039, 7, 8, 9, 3, 11)
            .unwrap()
            .with_nanosecond(123_000_000)
            .unwrap();
        let expected = DateTime::from_str("2039-07-08T11:03:11.123Z", Format::DateTime).unwrap();
        assert_eq!(expected, DateTime::from_chrono_fixed(chrono));
    }
@@ -178,12 +197,20 @@ mod test {
        use chrono::{TimeZone, Utc};

        let date_time = DateTime::from_str("2039-07-08T09:03:11.123Z", Format::DateTime).unwrap();
        let expected = Utc.ymd(2039, 7, 8).and_hms_nano(9, 3, 11, 123_000_000);
        assert_eq!(expected, date_time.to_chrono_utc());
        let expected = Utc
            .with_ymd_and_hms(2039, 7, 8, 9, 3, 11)
            .unwrap()
            .with_nanosecond(123_000_000)
            .unwrap();
        assert_eq!(expected, date_time.to_chrono_utc().unwrap());

        let date_time = DateTime::from_str("1000-07-08T09:03:11.456Z", Format::DateTime).unwrap();
        let expected = Utc.ymd(1000, 7, 8).and_hms_nano(9, 3, 11, 456_000_000);
        assert_eq!(expected, date_time.to_chrono_utc());
        let expected = Utc
            .with_ymd_and_hms(1000, 7, 8, 9, 3, 11)
            .unwrap()
            .with_nanosecond(456_000_000)
            .unwrap();
        assert_eq!(expected, date_time.to_chrono_utc().unwrap());
    }

    #[test]