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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
pub struct Clock {
hours: i64,
minutes: i64,
seconds: i64,
}
impl Clock {
pub fn new() -> Clock {
Clock {
hours: 0,
minutes: 0,
seconds: 0,
}
}
pub fn set_time_ms(&mut self, ms: i64) {
self.seconds = (ms / 1000) % 60;
self.minutes = (ms / (1000 * 60)) % 60;
self.hours = (ms / (1000 * 60 * 60)) % 24;
}
pub fn set_time_secs(&mut self, seconds: i64) {
self.set_time_ms(seconds * 1000);
}
pub fn get_time(&self) -> String {
format!("{:02}:{:02}:{:02}", self.hours, self.minutes, self.seconds)
}
}
#[cfg(test)]
#[test]
fn test_clock_ms() {
let mut clock = Clock::new();
clock.set_time_ms(60000);
assert_eq!(clock.get_time(), "00:01:00");
}
#[test]
fn test_clock_secs() {
let mut clock = Clock::new();
clock.set_time_secs(330);
assert_eq!(clock.get_time(), "00:05:30");
}