From 2a057c1197d70d1d9b31a090c28dd7b929fdb6ae Mon Sep 17 00:00:00 2001 From: Botahamec Date: Mon, 31 Jan 2022 09:42:47 -0500 Subject: Added ordering --- src/time.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/time.rs') diff --git a/src/time.rs b/src/time.rs index 94f7cd8..e80e532 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,3 +1,6 @@ +use core::cmp::Ordering; +use core::fmt::Display; + #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] pub struct Time { hour: u8, @@ -122,4 +125,46 @@ impl Time { } } +impl PartialOrd for Time { + fn partial_cmp(&self, other: &Self) -> Option { + let hour_ordering = self.hour.cmp(&other.hour); + let minute_ordering = self.minute.cmp(&other.minute); + let second_ordering = self.second.cmp(&other.second); + let nano_ordering = self.nanosecond.cmp(&other.nanosecond); + + if hour_ordering != Ordering::Equal { + Some(hour_ordering) + } else if minute_ordering != Ordering::Equal { + Some(minute_ordering) + } else if second_ordering != Ordering::Equal { + Some(second_ordering) + } else if nano_ordering != Ordering::Equal { + Some(nano_ordering) + } else { + Some(Ordering::Equal) + } + } +} + +impl Ord for Time { + fn cmp(&self, other: &Self) -> Ordering { + let hour_ordering = self.hour.cmp(&other.hour); + let minute_ordering = self.minute.cmp(&other.minute); + let second_ordering = self.second.cmp(&other.second); + let nano_ordering = self.nanosecond.cmp(&other.nanosecond); + + if hour_ordering != Ordering::Equal { + hour_ordering + } else if minute_ordering != Ordering::Equal { + minute_ordering + } else if second_ordering != Ordering::Equal { + second_ordering + } else if nano_ordering != Ordering::Equal { + nano_ordering + } else { + Ordering::Equal + } + } +} + // TODO addition -- cgit v1.2.3