Skip to content

Commit

Permalink
Add Display impl for DateTime (#3235)
Browse files Browse the repository at this point in the history
## 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 -->
#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: Hakan Vardar <[email protected]>
  • Loading branch information
rcoh and HakanVardarr authored Nov 17, 2023
1 parent 9e09fa4 commit c862fb8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
10 changes: 9 additions & 1 deletion rust-runtime/aws-smithy-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
22 changes: 22 additions & 0 deletions rust-runtime/aws-smithy-types/src/date_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c862fb8

Please sign in to comment.