Skip to content
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

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ author = "rcoh"
references = ["smithy-rs#2612"]
meta = { "breaking" = false, "tada" = false, "bug" = false }


[[smithy-rs]]
message = "Implement `Ord` and `PartialOrd` for `DateTime`."
author = "henriiik"
references = ["smithy-rs#2653"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
44 changes: 44 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 @@ -9,6 +9,7 @@ use crate::date_time::format::rfc3339::AllowOffsets;
use crate::date_time::format::DateTimeParseErrorKind;
use num_integer::div_mod_floor;
use num_integer::Integer;
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::error::Error as StdError;
use std::fmt;
Expand Down Expand Up @@ -301,6 +302,21 @@ impl From<SystemTime> for DateTime {
}
}

impl PartialOrd for DateTime {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for DateTime {
fn cmp(&self, other: &Self) -> Ordering {
match self.seconds.cmp(&other.seconds) {
Ordering::Equal => self.subsecond_nanos.cmp(&other.subsecond_nanos),
ordering => ordering,
Comment on lines +314 to +315
Copy link
Collaborator

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

}
}
}

/// Failure to convert a `DateTime` to or from another type.
#[derive(Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -552,4 +568,32 @@ mod test {
SystemTime::try_from(date_time).unwrap()
);
}

#[test]
fn ord() {
let first = DateTime::from_secs_and_nanos(-1, 0);
Copy link
Collaborator

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

let second = DateTime::from_secs_and_nanos(0, 0);
let third = DateTime::from_secs_and_nanos(0, 1);
let fourth = DateTime::from_secs_and_nanos(1, 0);

assert!(first == first);
assert!(first < second);
assert!(first < third);
assert!(first < fourth);

assert!(second > first);
assert!(second == second);
assert!(second < third);
assert!(second < fourth);

assert!(third > first);
assert!(third > second);
assert!(third == third);
assert!(third < fourth);

assert!(fourth > first);
assert!(fourth > second);
assert!(fourth > third);
assert!(fourth == fourth);
}
}