Skip to content

Commit 5a8fff7

Browse files
authored
implement Ord and PartialOrd for DateTime (#2653)
## 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._
1 parent a85cef6 commit 5a8fff7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

CHANGELOG.next.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ author = "rcoh"
1717
references = ["smithy-rs#2612"]
1818
meta = { "breaking" = false, "tada" = false, "bug" = false }
1919

20+
21+
[[smithy-rs]]
22+
message = "Implement `Ord` and `PartialOrd` for `DateTime`."
23+
author = "henriiik"
24+
references = ["smithy-rs#2653"]
25+
meta = { "breaking" = false, "tada" = false, "bug" = false }

rust-runtime/aws-smithy-types/src/date_time/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::date_time::format::rfc3339::AllowOffsets;
99
use crate::date_time::format::DateTimeParseErrorKind;
1010
use num_integer::div_mod_floor;
1111
use num_integer::Integer;
12+
use std::cmp::Ordering;
1213
use std::convert::TryFrom;
1314
use std::error::Error as StdError;
1415
use std::fmt;
@@ -301,6 +302,21 @@ impl From<SystemTime> for DateTime {
301302
}
302303
}
303304

305+
impl PartialOrd for DateTime {
306+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
307+
Some(self.cmp(other))
308+
}
309+
}
310+
311+
impl Ord for DateTime {
312+
fn cmp(&self, other: &Self) -> Ordering {
313+
match self.seconds.cmp(&other.seconds) {
314+
Ordering::Equal => self.subsecond_nanos.cmp(&other.subsecond_nanos),
315+
ordering => ordering,
316+
}
317+
}
318+
}
319+
304320
/// Failure to convert a `DateTime` to or from another type.
305321
#[derive(Debug)]
306322
#[non_exhaustive]
@@ -552,4 +568,32 @@ mod test {
552568
SystemTime::try_from(date_time).unwrap()
553569
);
554570
}
571+
572+
#[test]
573+
fn ord() {
574+
let first = DateTime::from_secs_and_nanos(-1, 0);
575+
let second = DateTime::from_secs_and_nanos(0, 0);
576+
let third = DateTime::from_secs_and_nanos(0, 1);
577+
let fourth = DateTime::from_secs_and_nanos(1, 0);
578+
579+
assert!(first == first);
580+
assert!(first < second);
581+
assert!(first < third);
582+
assert!(first < fourth);
583+
584+
assert!(second > first);
585+
assert!(second == second);
586+
assert!(second < third);
587+
assert!(second < fourth);
588+
589+
assert!(third > first);
590+
assert!(third > second);
591+
assert!(third == third);
592+
assert!(third < fourth);
593+
594+
assert!(fourth > first);
595+
assert!(fourth > second);
596+
assert!(fourth > third);
597+
assert!(fourth == fourth);
598+
}
555599
}

0 commit comments

Comments
 (0)