-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement Ord
and PartialOrd
for DateTime
#2653
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for submitting this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great! couple of small changes
Ordering::Equal => self.subsecond_nanos.cmp(&other.subsecond_nanos), | ||
ordering => ordering, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you delegate to as_nanos
instead? I'm 99% sure this is correct but the interaction of subsecond nanos negative dates is non trivial and if we use as_nanos
it will definitely be correct
|
||
#[test] | ||
fn ord() { | ||
let first = DateTime::from_secs_and_nanos(-1, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you have -1, 1
tested as well?
Another nice proptest would be to format the date as RFC3339 where the string comparison is valid and assert that the ordering matches
## Motivation and Context This PR adresses on [feedback from my previous PR](#2653 (review)). ## Description - use `as_nanos` in `Ord` impl for `DateTime` instead of manual impl. ## Testing - add proptest that checks that `Ord` impl matches RFC 3339 comparison. ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or 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._
## Motivation and Context This change will allow easy sorting or comparing anything that contains a DateTime. My example is wanting to sort a list of S3 objects by last modified. This PR fixes #2406 ## Description It's a pretty small PR, it implements the `Ord` trait for `DateTime` by comparing the `seconds` property of `self` and `other`, if they are equal it compares the `subsec_nanos` properties instead. The `PartialOrd` trait is implemented by calling the `Ord` trait. ## Testing I added a unit test that compares a number of `DateTime` values with different combinations of positive/zero/negative `seconds`/`subsec_nanos`. ## Checklist - [x] 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._
## Motivation and Context This PR adresses on [feedback from my previous PR](#2653 (review)). ## Description - use `as_nanos` in `Ord` impl for `DateTime` instead of manual impl. ## Testing - add proptest that checks that `Ord` impl matches RFC 3339 comparison. ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or 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._
## Motivation and Context This change will allow easy sorting or comparing anything that contains a DateTime. My example is wanting to sort a list of S3 objects by last modified. This PR fixes #2406 ## Description It's a pretty small PR, it implements the `Ord` trait for `DateTime` by comparing the `seconds` property of `self` and `other`, if they are equal it compares the `subsec_nanos` properties instead. The `PartialOrd` trait is implemented by calling the `Ord` trait. ## Testing I added a unit test that compares a number of `DateTime` values with different combinations of positive/zero/negative `seconds`/`subsec_nanos`. ## Checklist - [x] 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._
## Motivation and Context This PR adresses on [feedback from my previous PR](#2653 (review)). ## Description - use `as_nanos` in `Ord` impl for `DateTime` instead of manual impl. ## Testing - add proptest that checks that `Ord` impl matches RFC 3339 comparison. ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or 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._
## Motivation and Context This change will allow easy sorting or comparing anything that contains a DateTime. My example is wanting to sort a list of S3 objects by last modified. This PR fixes #2406 ## Description It's a pretty small PR, it implements the `Ord` trait for `DateTime` by comparing the `seconds` property of `self` and `other`, if they are equal it compares the `subsec_nanos` properties instead. The `PartialOrd` trait is implemented by calling the `Ord` trait. ## Testing I added a unit test that compares a number of `DateTime` values with different combinations of positive/zero/negative `seconds`/`subsec_nanos`. ## Checklist - [x] 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._
## Motivation and Context This PR adresses on [feedback from my previous PR](#2653 (review)). ## Description - use `as_nanos` in `Ord` impl for `DateTime` instead of manual impl. ## Testing - add proptest that checks that `Ord` impl matches RFC 3339 comparison. ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or 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._
## Motivation and Context This PR adresses on [feedback from my previous PR](smithy-lang/smithy-rs#2653 (review)). ## Description - use `as_nanos` in `Ord` impl for `DateTime` instead of manual impl. ## Testing - add proptest that checks that `Ord` impl matches RFC 3339 comparison. ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or 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._
Motivation and Context
This change will allow easy sorting or comparing anything that contains a DateTime. My example is wanting to sort a list of S3 objects by last modified.
This PR fixes #2406
Description
It's a pretty small PR, it implements the
Ord
trait forDateTime
by comparing theseconds
property ofself
andother
, if they are equal it compares thesubsec_nanos
properties instead.The
PartialOrd
trait is implemented by calling theOrd
trait.Testing
I added a unit test that compares a number of
DateTime
values with different combinations of positive/zero/negativeseconds
/subsec_nanos
.Checklist
CHANGELOG.next.toml
if I made changes to the AWS SDK, generated SDK code, or SDK runtime cratesBy submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.