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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
pub fn ms_to_readable(mut ms: u128, round: bool) -> String {
if round {
ms = round_ms_30(ms);
}
if ms >= 1000 {
let remain_ms = ms % 1000;
ms -= remain_ms;
let mut s = ms / 1000;
if s >= 60 {
let remain_s = s % 60;
s -= remain_s;
let mut min = s / 60;
if min >= 60 {
let remain_min = min % 60;
min -= remain_min;
let hr = min / 60;
return format!("{}:{:02}:{:02}.{:03}", hr, remain_min, remain_s, remain_ms);
} else {
return format!("{}:{:02}.{:03}", min, remain_s, remain_ms);
}
} else {
return format!("{}.{:03}", s, remain_ms);
}
}
return format!("0.{:03}", ms);
}
pub fn diff_text(mut ms: i128) -> String {
let pre: char;
if ms < 0 {
ms *= -1;
pre = '-';
} else {
pre = '+';
}
let mut tenths = ms / 100;
let mut full_s: i128;
full_s = tenths / 10;
tenths -= full_s * 10;
if full_s >= 60 {
let mut min = full_s / 60;
full_s -= min * 60;
if min >= 60 {
let hr = min / 60;
min -= hr * 60;
return format!("{}{}:{:02}:{:02}.{}", pre, hr, min, full_s, tenths);
} else {
return format!("{}{}:{:02}.{}", pre, min, full_s, tenths);
}
} else {
return format!("{}{}.{}", pre, full_s, tenths);
}
}
pub fn split_time_text(ms: u128) -> String {
let mut tenths = ms / 100;
let mut full_s: u128;
if tenths > 10 {
full_s = tenths / 10;
tenths -= full_s * 10;
if full_s >= 60 {
let mut min = full_s / 60;
full_s -= min * 60;
if min >= 60 {
let hr = min / 60;
min -= hr * 60;
return format!("{}:{:02}:{:02}.{}", hr, min, full_s, tenths);
} else {
return format!("{}:{:02}.{}", min, full_s, tenths);
}
} else {
return format!("{}.{}", full_s, tenths);
}
} else {
return format!("0.{}", tenths);
}
}
pub fn split_time_sum(ms_vec: &Vec<u128>) -> Vec<u128> {
let mut total = 0;
let mut vec = vec![];
for num in ms_vec {
total += num;
vec.push(total);
}
vec
}
fn round_ms_30(ms: u128) -> u128 {
let hundreds = ms / 100;
let mut rounded = ms % 100;
rounded = match rounded {
0..=32 => 0,
33..=66 => 33,
67..=99 => 67,
_ => 0,
};
rounded + (hundreds * 100)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_readable() {
assert_eq!(ms_to_readable(3_661_111, false), "1:01:01.111");
}
#[test]
fn test_rounding_30() {
assert_eq!(round_ms_30(500), 500);
assert_eq!(round_ms_30(710), 700);
assert_eq!(round_ms_30(645), 633);
assert_eq!(round_ms_30(384), 367);
assert_eq!(round_ms_30(399), 367);
}
#[test]
fn test_sum() {
assert_eq!(split_time_sum(&vec![6, 7, 8]), vec![6, 13, 21]);
assert_eq!(split_time_sum(&vec![0]), vec![0]);
}
#[test]
fn test_diff() {
assert_eq!(diff_text(3_661_111), "+1:01:01.1");
assert_eq!(diff_text(-3_661_111), "-1:01:01.1");
}
#[test]
fn test_split() {
assert_eq!(split_time_text(3_661_111), "1:01:01.1");
}
}