-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
} | ||
} | ||
} | ||
|
||
/// Failure to convert a `DateTime` to or from another type. | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
|
@@ -552,4 +568,32 @@ mod test { | |
SystemTime::try_from(date_time).unwrap() | ||
); | ||
} | ||
|
||
#[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 commentThe reason will be displayed to describe this comment to others. Learn more. can you have 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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 useas_nanos
it will definitely be correct