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
69
70
71
72
73
74
75
76
77
78
79
use std::fmt::Write;
use std::time::Duration;
pub fn format_duration(duration: Duration) -> String {
let sec = duration.as_secs();
let days = sec / 86400;
let sec = sec % 86400;
let hours = sec / 3600;
let sec = sec % 3600;
let minutes = sec / 60;
let seconds = sec % 60;
let mut s = String::with_capacity(10);
if days > 0 {
s.write_fmt(format_args!("{} day", days)).unwrap();
if days > 1 {
s.push('s');
}
s.push_str(", ");
}
if hours > 0 || (days > 0) && (minutes > 0 || seconds > 0) {
s.write_fmt(format_args!("{} hour", hours)).unwrap();
if hours > 1 {
s.push('s');
}
s.push_str(", ");
}
if minutes > 0 || (hours > 0 && seconds > 0) {
s.write_fmt(format_args!("{} minute", minutes)).unwrap();
if minutes > 1 {
s.push('s');
}
s.push_str(", ");
}
if seconds > 0 {
s.write_fmt(format_args!("{} second", seconds)).unwrap();
if seconds > 1 {
s.push('s');
}
s.push_str(", ");
}
debug_assert!(s.len() >= 2);
if let Some(index) = s.as_str()[..(s.len() - 2)].rfind(", ") {
s.insert_str(index + 2, "and ");
}
let mut v = s.into_bytes();
unsafe {
v.set_len(v.len() - 2);
String::from_utf8_unchecked(v)
}
}