use std::fmt; use std::ops::Add; #[derive(Debug, PartialEq, Clone, Copy)] pub struct Clock { hours: i32, minutes: i32, } impl fmt::Display for Clock { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:02}:{:02}", self.hours, self.minutes) } } impl Add for Clock { type Output = Self; fn add(self, minutes: i32) -> Self { Self::new(self.hours, self.minutes + minutes) } } /* impl From for String { * fn from(c: Clock) -> String { * format!("{}",c) * } * } */ impl Clock { pub fn new(hours: i32, minutes: i32) -> Self { let hours = hours + minutes / 60 - if minutes % 60 < 0 { 1 } else { 0 }; let hours = hours % 24; let hours = hours + if hours < 0 { 24 } else { 0 }; let minutes = minutes % 60; let minutes = minutes + if minutes < 0 { 60 } else { 0 }; Clock { hours, minutes } } pub fn add_minutes(self, minutes: i32) -> Self { self + minutes } }