Merge pull request #375 from dlalic/354

Support comparison between dts with different timezones. Fixes #354
This commit is contained in:
Brandon W Maister 2019-12-30 17:16:06 -05:00 committed by GitHub
commit 9397ab2b47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -17,6 +17,7 @@ Versions with only mechanical changes will be omitted from the following list.
(@quodlibetor in #378) (@quodlibetor in #378)
* Support "negative UTC" in `parse_from_rfc2822` (@quodlibetor #368 reported in * Support "negative UTC" in `parse_from_rfc2822` (@quodlibetor #368 reported in
#102) #102)
* Support comparisons of DateTimes with different timezones (@dlalic in #375)
### Internal improvements ### Internal improvements

View File

@ -575,8 +575,23 @@ impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<DateTime<Tz2>> for DateTime<Tz> {
impl<Tz: TimeZone> Eq for DateTime<Tz> { impl<Tz: TimeZone> Eq for DateTime<Tz> {
} }
impl<Tz: TimeZone> PartialOrd for DateTime<Tz> { impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateTime<Tz2>> for DateTime<Tz> {
fn partial_cmp(&self, other: &DateTime<Tz>) -> Option<Ordering> { /// Compare two DateTimes based on their true time, ignoring time zones
///
/// # Example
///
/// ```
/// use chrono::prelude::*;
///
/// let earlier = Utc.ymd(2015, 5, 15).and_hms(2, 0, 0).with_timezone(&FixedOffset::west(1 * 3600));
/// let later = Utc.ymd(2015, 5, 15).and_hms(3, 0, 0).with_timezone(&FixedOffset::west(5 * 3600));
///
/// assert_eq!(earlier.to_string(), "2015-05-15 01:00:00 -01:00");
/// assert_eq!(later.to_string(), "2015-05-14 22:00:00 -05:00");
///
/// assert!(later > earlier);
/// ```
fn partial_cmp(&self, other: &DateTime<Tz2>) -> Option<Ordering> {
self.datetime.partial_cmp(&other.datetime) self.datetime.partial_cmp(&other.datetime)
} }
} }
@ -2005,6 +2020,9 @@ mod tests {
assert_eq!(d.date(), tz.ymd(2017, 8, 9)); assert_eq!(d.date(), tz.ymd(2017, 8, 9));
assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2017, 8, 9)); assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2017, 8, 9));
assert_eq!(d.date().and_time(d.time()), Some(d)); assert_eq!(d.date().and_time(d.time()), Some(d));
let utc_d = Utc.ymd(2017, 8, 9).and_hms(12, 34, 56);
assert!(utc_d < d);
} }
#[test] #[test]