diff --git a/solutions/rust/clock/1/src/lib.rs b/solutions/rust/clock/1/src/lib.rs new file mode 100644 index 0000000..c1cec13 --- /dev/null +++ b/solutions/rust/clock/1/src/lib.rs @@ -0,0 +1,39 @@ +use std::fmt; + +pub struct Clock { + hours: i32, + minutes: i32, +} + +impl Clock { + pub fn new(hours: i32, minutes: i32) -> Self { + let total_minutes = 60 * hours + minutes; + + Self { + hours: (total_minutes as f64 / 60.0).floor().rem_euclid(24.0) as i32, + minutes: (total_minutes.rem_euclid(60)), + } + } + + pub fn add_minutes(&self, minutes: i32) -> Self { + Clock::new(self.hours, self.minutes + minutes) + } +} + +impl PartialEq for Clock { + fn eq(&self, other: &Self) -> bool { + self.hours == other.hours && self.minutes == other.minutes + } +} + +impl fmt::Display for Clock { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:02}:{:02}", self.hours, self.minutes) + } +} + +impl fmt::Debug for Clock { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:02}:{:02}", self.hours, self.minutes) + } +}