-
Notifications
You must be signed in to change notification settings - Fork 182
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
Add Ord and PartialOrd to icu_calendar types #3468
Merged
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ use core::convert::TryInto; | |
use core::marker::PhantomData; | ||
use tinystr::tinystr; | ||
|
||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] | ||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] | ||
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. I would ask you not to make this require 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. Agree on both counts. |
||
#[allow(clippy::exhaustive_structs)] // this type is stable | ||
pub struct ArithmeticDate<C: CalendarArithmetic> { | ||
pub year: i32, | ||
|
@@ -250,3 +250,38 @@ pub fn ordinal_solar_month_from_code(code: types::MonthCode) -> Option<u8> { | |
} | ||
None | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::Iso; | ||
|
||
#[test] | ||
fn test_ord() { | ||
let dates_in_order = [ | ||
ArithmeticDate::<Iso>::new(-10, 1, 1), | ||
ArithmeticDate::<Iso>::new(-10, 1, 2), | ||
ArithmeticDate::<Iso>::new(-10, 2, 1), | ||
ArithmeticDate::<Iso>::new(-1, 1, 1), | ||
ArithmeticDate::<Iso>::new(-1, 1, 2), | ||
ArithmeticDate::<Iso>::new(-1, 2, 1), | ||
ArithmeticDate::<Iso>::new(0, 1, 1), | ||
ArithmeticDate::<Iso>::new(0, 1, 2), | ||
ArithmeticDate::<Iso>::new(0, 2, 1), | ||
ArithmeticDate::<Iso>::new(1, 1, 1), | ||
ArithmeticDate::<Iso>::new(1, 1, 2), | ||
ArithmeticDate::<Iso>::new(1, 2, 1), | ||
ArithmeticDate::<Iso>::new(10, 1, 1), | ||
ArithmeticDate::<Iso>::new(10, 1, 2), | ||
ArithmeticDate::<Iso>::new(10, 2, 1), | ||
]; | ||
for (i, i_date) in dates_in_order.iter().enumerate() { | ||
for (j, j_date) in dates_in_order.iter().enumerate() { | ||
let result1 = i_date.cmp(j_date); | ||
let result2 = j_date.cmp(i_date); | ||
assert_eq!(result1.reverse(), result2); | ||
assert_eq!(i.cmp(&j), i_date.cmp(j_date)); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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.
I guess this does
Self::Gregorian(x)
<Self::Buddhist(y)
? This will makeDate<AnyCalendar>
ordered non-chronologically.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.
Hmm, yeah I guess it will put all Gregorian before all Buddhist. I can leave this out for now. I'll make a follow-up.
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.
It should be fairly straightforward to implement this manually, as all calendars can convert to ISO.
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.
I'll include this in my follow-up
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.
So this is not as straightforward as it initially seems, because the calendars have different ranges. We can't convert them to
fixed_date
(number of days since 0000-1-1) becausefixed_date
is an i32, but the calendar objects support dates outside that range; a calendar with a year field ofi32::MAX
can't necessarily be converted to another one. (@sotam1069 is working on finding these edge cases and making sure they are at least covered in unit tests)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.
Do we need
Ord
?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.
#3469