From ad8644ea57103f186587cd54719a9ecf511ee1b0 Mon Sep 17 00:00:00 2001 From: Dunja Lalic Date: Mon, 16 Dec 2019 12:53:36 +0100 Subject: [PATCH 1/2] Fixes #354 --- src/datetime.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 074b973..1e53753 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -575,8 +575,8 @@ impl PartialEq> for DateTime { impl Eq for DateTime { } -impl PartialOrd for DateTime { - fn partial_cmp(&self, other: &DateTime) -> Option { +impl PartialOrd> for DateTime { + fn partial_cmp(&self, other: &DateTime) -> Option { self.datetime.partial_cmp(&other.datetime) } } @@ -2032,6 +2032,9 @@ mod tests { 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().and_time(d.time()), Some(d)); + + let utc_d = Utc.ymd(2017, 8, 9).and_hms(12, 34, 56); + assert!(utc_d < d); } #[test] From 3b295ccb8fd1f54474dc797db4578054936ab390 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 27 Dec 2019 11:29:57 -0500 Subject: [PATCH 2/2] Add doctest for comparison between timestamps with different time_zones --- CHANGELOG.md | 1 + src/datetime.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7035c8c..18191a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Versions with only mechanical changes will be omitted from the following list. * Support "negative UTC" in `parse_from_rfc2822` (@quodlibetor #368 reported in #102) +* Support comparisons of DateTimes with different timezones (@dlalic in #375) ### Internal improvements diff --git a/src/datetime.rs b/src/datetime.rs index 1e53753..ae2e1dc 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -576,6 +576,21 @@ impl Eq for DateTime { } impl PartialOrd> for DateTime { + /// 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) -> Option { self.datetime.partial_cmp(&other.datetime) }