summaryrefslogtreecommitdiff
path: root/src/time.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-01-31 09:42:47 -0500
committerBotahamec <botahamec@outlook.com>2022-01-31 09:42:47 -0500
commit2a057c1197d70d1d9b31a090c28dd7b929fdb6ae (patch)
treebf8323ac8ef1ddc88853301e718615e542c35e4b /src/time.rs
parent923e30b1bac5485075df893e8c5ef2d0346bf251 (diff)
Added ordering
Diffstat (limited to 'src/time.rs')
-rw-r--r--src/time.rs45
1 files changed, 45 insertions, 0 deletions
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<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 {
+ 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