From 9abad1acf05fbf43d5755c11a48c5d470b12186c Mon Sep 17 00:00:00 2001 From: 82marbag <69267416+82marbag@users.noreply.github.com> Date: Mon, 14 Nov 2022 12:27:04 -0500 Subject: [PATCH] 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: Daniele Ahmed * Update changelog Signed-off-by: Daniele Ahmed * Fix error Signed-off-by: Daniele Ahmed * Use with_ymd_and_hms Signed-off-by: Daniele Ahmed Signed-off-by: Daniele Ahmed --- CHANGELOG.next.toml | 7 ++- .../aws-smithy-types-convert/src/date_time.rs | 61 +++++++++++++------ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index 98e726c6cc..164719bb6d 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -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" - diff --git a/rust-runtime/aws-smithy-types-convert/src/date_time.rs b/rust-runtime/aws-smithy-types-convert/src/date_time.rs index 3b91be3d32..32984b3d78 100644 --- a/rust-runtime/aws-smithy-types-convert/src/date_time.rs +++ b/rust-runtime/aws-smithy-types-convert/src/date_time.rs @@ -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 = DateTime::from_secs(5).to_chrono_utc(); +let chrono_date_time: chrono::DateTime = 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; + fn to_chrono_utc(&self) -> Result, 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::DateTime::::from_utc( - chrono::NaiveDateTime::from_timestamp(self.secs(), self.subsec_nanos()), - chrono::Utc, - ) + fn to_chrono_utc(&self) -> Result, Error> { + match chrono::NaiveDateTime::from_timestamp_opt(self.secs(), self.subsec_nanos()) { + None => { + let err: Box = format!( + "Out-of-range seconds {} or invalid nanoseconds {}", + self.secs(), + self.subsec_nanos() + ) + .into(); + Err(Error::OutOfRange(err)) + } + Some(dt) => Ok(chrono::DateTime::::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]