Skip to content

Commit

Permalink
Do not use deprecated from_timestamp from chrono (#1980)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update changelog

Signed-off-by: Daniele Ahmed <[email protected]>

* Fix error

Signed-off-by: Daniele Ahmed <[email protected]>

* Use with_ymd_and_hms

Signed-off-by: Daniele Ahmed <[email protected]>

Signed-off-by: Daniele Ahmed <[email protected]>
  • Loading branch information
82marbag authored Nov 14, 2022
1 parent 7477cfb commit 9abad1a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"

61 changes: 44 additions & 17 deletions rust-runtime/aws-smithy-types-convert/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ 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);
```
"##
)]
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")]
Expand All @@ -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")]
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
Expand All @@ -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]
Expand Down

0 comments on commit 9abad1a

Please sign in to comment.