Commit caf86d3f authored by Russell Cohen's avatar Russell Cohen Committed by AWS SDK Rust Bot
Browse files

Add Display impl for DateTime (#3235)

## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
It implements Display trait For DateTime

<!--- If it fixes an open issue, please link to the issue here -->
https://github.com/smithy-lang/smithy-rs/issues/3161



## Description
<!--- Describe your changes in detail -->
I implemented Display trait for DateTime

## Testing
<!--- Please describe in detail how you tested your changes -->
<!--- Include details of your testing environment, and the tests you ran
to -->

I used this test
```rust
    fn test_display_formatting() {
        // Create a DateTime instance for testing
        let datetime = DateTime {
            seconds: 1636761600, // Example timestamp (replace with your actual timestamp)
            subsecond_nanos: 123456789, // Example subsecond nanos (replace with your actual value)
        };

        // Expected RFC-3339 formatted string
        let expected = "2021-11-13T00:00:00.123456789Z";

        // Format the DateTime using Display trait
        let formatted = format!("{}", datetime);

        // Assert that the formatted string matches the expected result
        assert_eq!(formatted, expected);
    }
```

<!--- see how your change affects other areas of the code, etc. -->

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: default avatarHakan Vardar <hakovardo123@gmail.com>
parent 04ede192
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -92,3 +92,15 @@ the [deprecations removal list](https://github.com/smithy-lang/smithy-rs/discuss
references = ["smithy-rs#3222"]
meta = { "breaking" = true, "tada" = false, "bug" = false }
author = "jdisanti"

[[aws-sdk-rust]]
message = "Add `Display` impl for `DateTime`."
references = ["smithy-rs#3183"]
meta = { "breaking" = false, "tada" = true, "bug" = false }
author = "HakanVardarr"

[[smithy-rs]]
message = "Add `Display` impl for `DateTime`."
references = ["smithy-rs#3183"]
meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "all" }
author = "HakanVardarr"
+9 −1
Original line number Diff line number Diff line
@@ -14,7 +14,15 @@ repository = "https://github.com/smithy-lang/smithy-rs"
byte-stream-poll-next = []
http-body-0-4-x = ["dep:http-body-0-4"]
hyper-0-14-x = ["dep:hyper-0-14"]
rt-tokio = ["dep:http-body-0-4", "dep:tokio-util", "dep:tokio", "tokio?/rt", "tokio?/fs", "tokio?/io-util", "tokio-util?/io"]
rt-tokio = [
    "dep:http-body-0-4",
    "dep:tokio-util",
    "dep:tokio",
    "tokio?/rt",
    "tokio?/fs",
    "tokio?/io-util",
    "tokio-util?/io",
]
test-util = []
serde-serialize = []
serde-deserialize = []
+22 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use std::cmp::Ordering;
use std::convert::TryFrom;
use std::error::Error as StdError;
use std::fmt;
use std::fmt::Display;
use std::time::Duration;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
@@ -329,6 +330,12 @@ impl Ord for DateTime {
    }
}

impl Display for DateTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let date = self.fmt(Format::DateTime).map_err(|_| fmt::Error)?;
        write!(f, "{}", date)
    }
}
/// Failure to convert a `DateTime` to or from another type.
#[derive(Debug)]
#[non_exhaustive]
@@ -372,6 +379,21 @@ mod test {
    use time::format_description::well_known::Rfc3339;
    use time::OffsetDateTime;

    #[test]
    fn test_display_date_time() {
        let date_time = DateTime::from_secs(1576540098);
        assert_eq!(format!("{}", date_time), "2019-12-16T23:48:18Z");

        let date_time = DateTime::from_fractional_secs(1576540098, 0.52);
        assert_eq!(format!("{}", date_time), "2019-12-16T23:48:18.52Z");

        let date_time = DateTime::from_secs(1699942527);
        assert_eq!(format!("{}", date_time), "2023-11-14T06:15:27Z");

        let date_time = DateTime::from_secs(16995123);
        assert_eq!(format!("{}", date_time), "1970-07-16T16:52:03Z");
    }

    #[test]
    fn test_fmt() {
        let date_time = DateTime::from_secs(1576540098);