1pub struct Clock {
11 hours: i64,
12 minutes: i64,
13 seconds: i64,
14}
15
16impl Clock {
17
18 pub fn new() -> Clock {
22 Clock {
23 hours: 0,
24 minutes: 0,
25 seconds: 0,
26 }
27 }
28
29 pub fn set_time_ms(&mut self, ms: i64) {
34 self.seconds = (ms / 1000) % 60;
35 self.minutes = (ms / (1000 * 60)) % 60;
36 self.hours = (ms / (1000 * 60 * 60)) % 24;
37 }
38
39 pub fn set_time_secs(&mut self, seconds: i64) {
44 self.set_time_ms(seconds * 1000);
45 }
46
47 pub fn get_time(&self) -> String {
51 format!("{:02}:{:02}:{:02}", self.hours, self.minutes, self.seconds)
52 }
53}
54
55#[cfg(test)]
56#[test]
57fn test_clock_ms() {
58 let mut clock = Clock::new();
59 clock.set_time_ms(60000);
60 assert_eq!(clock.get_time(), "00:01:00");
61}
62
63#[test]
64fn test_clock_secs() {
65 let mut clock = Clock::new();
66 clock.set_time_secs(330);
67 assert_eq!(clock.get_time(), "00:05:30");
68}