1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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<i32> for Clock {
type Output = Self;
fn add(self, minutes: i32) -> Self {
Self::new(self.hours, self.minutes + minutes)
}
}
/* impl From<Clock> 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
}
}
|