Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! Convert seconds to compound duration (week, days, hours, minutes, seconds)
2
3 use std::convert::{TryFrom, TryInto};
4 use std::fmt::Debug;
5 use std::ops::BitAnd;
6
7 pub const NS: usize = 1;
8 pub const US: usize = 1_000;
9 pub const MS: usize = 1_000_000;
10 pub const NANOS: usize = 1_000_000_000;
11 pub const SECOND: usize = 1;
12 pub const MINUTE: usize = 60;
13 pub const HOUR: usize = 3_600;
14 pub const DAY: usize = 86_400;
15 pub const WEEK: usize = 604_800;
16
17 /// Convert seconds to compound duration (days, hours, minutes, seconds)
18 ///
19 /// Example:
20 ///```
21 /// use compound_duration::format_dhms;
22 /// use std::time::Instant;
23 ///
24 /// let now = Instant::now();
25 /// // do something ...
26 /// println!("{}", format_dhms(now.elapsed().as_secs() as usize));
27 ///
28 /// // 69d10h40m
29 /// println!("{}", format_dhms(6000000));
30 ///```
31 #[must_use]
32 pub fn format_dhms<T: TryInto<usize> + TryFrom<usize> + BitAnd<Output = T>>(seconds: T) -> String
33 where
34 <T as TryFrom<usize>>::Error: Debug,
35 <T as TryInto<usize>>::Error: Debug,
36 {
37 let seconds: usize = if std::mem::size_of::<T>() <= std::mem::size_of::<usize>() {
38 seconds.try_into().unwrap()
39 } else {
40 (seconds & usize::MAX.try_into().unwrap())
41 .try_into()
42 .unwrap()
43 };
44 let mut compound_duration = String::new();
45 if seconds == 0 {
46 compound_duration.push_str("0s");
47 return compound_duration;
48 }
49
50 let mut sec = seconds % DAY;
51 let ds = seconds / DAY;
52 // days
53 if ds != 0 {
54 compound_duration.push_str(format!("{ds}d").as_str());
55 }
56
57 // hours
58 let hs = sec / HOUR;
59 sec %= HOUR;
60 if hs != 0 {
61 compound_duration.push_str(format!("{hs}h").as_str());
62 }
63
64 // minutes
65 let ms = sec / MINUTE;
66 sec %= MINUTE;
67 if ms != 0 {
68 compound_duration.push_str(format!("{ms}m").as_str());
69 }
70
71 // seconds
72 if sec != 0 {
73 compound_duration.push_str(format!("{sec}s").as_str());
74 }
75
76 compound_duration
77 }
78
79 /// Convert seconds to compound duration (week, days, hours, minutes, seconds)
80 ///
81 /// Example:
82 ///```
83 /// use compound_duration::format_wdhms;
84 /// // 9w6d10h40m
85 /// println!("{}", format_wdhms(6000000));
86 ///```
87 #[must_use]
88 pub fn format_wdhms<T: TryInto<usize> + TryFrom<usize> + BitAnd<Output = T>>(seconds: T) -> String
89 where
90 <T as TryFrom<usize>>::Error: Debug,
91 <T as TryInto<usize>>::Error: Debug,
92 {
93 let seconds: usize = if std::mem::size_of::<T>() <= std::mem::size_of::<usize>() {
94 seconds.try_into().unwrap()
95 } else {
96 (seconds & usize::MAX.try_into().unwrap())
97 .try_into()
98 .unwrap()
99 };
100 let mut compound_duration = String::new();
101 if seconds == 0 {
102 compound_duration.push_str("0s");
103 return compound_duration;
104 }
105
106 let mut sec = seconds % WEEK;
107 // weeks
108 let ws = seconds / WEEK;
109 if ws != 0 {
110 compound_duration.push_str(format!("{ws}w").as_str());
111 }
112
113 // days
114 let ds = sec / DAY;
115 sec %= DAY;
116 if ds != 0 {
117 compound_duration.push_str(format!("{ds}d").as_str());
118 }
119
120 // hours
121 let hs = sec / HOUR;
122 sec %= HOUR;
123 if hs != 0 {
124 compound_duration.push_str(format!("{hs}h").as_str());
125 }
126
127 // minutes
128 let ms = sec / MINUTE;
129 sec %= MINUTE;
130 if ms != 0 {
131 compound_duration.push_str(format!("{ms}m").as_str());
132 }
133
134 // seconds
135 if sec != 0 {
136 compound_duration.push_str(format!("{sec}s").as_str());
137 }
138
139 compound_duration
140 }
141
142 /// Convert seconds to compound duration (days, hours, minutes, seconds, ms, µs, ns)
143 ///
144 /// Example:
145 ///```
146 /// use compound_duration::format_ns;
147 /// use std::time::Instant;
148 ///
149 /// let now = Instant::now();
150 /// println!("{}", format_ns(now.elapsed().as_nanos() as usize));
151 ///```
152 #[must_use]
153 pub fn format_ns<T: TryInto<usize> + TryFrom<usize> + BitAnd<Output = T>>(nanos: T) -> String
154 where
155 <T as TryFrom<usize>>::Error: Debug,
156 <T as TryInto<usize>>::Error: Debug,
157 {
158 let nanos: usize = if std::mem::size_of::<T>() <= std::mem::size_of::<usize>() {
159 nanos.try_into().unwrap()
160 } else {
161 (nanos & usize::MAX.try_into().unwrap()).try_into().unwrap()
162 };
163 let mut compound_duration = String::new();
164 if nanos == 0 {
165 compound_duration.push_str("0ns");
166 return compound_duration;
167 }
168
169 let mut ns = nanos % (DAY * NANOS);
170 let d_ns = nanos / (DAY * NANOS);
171 // days
172 if d_ns != 0 {
173 compound_duration.push_str(format!("{d_ns}d").as_str());
174 }
175
176 // hours
177 let h_ns = ns / (HOUR * NANOS);
178 ns %= HOUR * NANOS;
179 if h_ns != 0 {
180 compound_duration.push_str(format!("{h_ns}h").as_str());
181 }
182
183 // minutes
184 let minutes_ns = ns / (MINUTE * NANOS);
185 ns %= MINUTE * NANOS;
186 if minutes_ns != 0 {
187 compound_duration.push_str(format!("{minutes_ns}m").as_str());
188 }
189
190 // seconds
191 let sec_ns = ns / (SECOND * NANOS);
192 ns %= SECOND * NANOS;
193 if sec_ns != 0 {
194 compound_duration.push_str(format!("{sec_ns}s").as_str());
195 }
196
197 // milliseconds
198 let ms_ns = ns / MS;
199 ns %= MS;
200 if ms_ns != 0 {
201 compound_duration.push_str(format!("{ms_ns}ms").as_str());
202 }
203
204 // microseconds
205 let micro_ns = ns / US;
206 ns %= US;
207 if micro_ns != 0 {
208 compound_duration.push_str(format!("{micro_ns}\u{b5}s").as_str());
209 }
210
211 // nanoseconds
212 if ns != 0 {
213 compound_duration.push_str(format!("{ns}ns").as_str());
214 }
215
216 compound_duration
217 }
218
219 #[cfg(test)]
220 mod tests {
221 use super::{format_dhms, format_ns, format_wdhms};
222
223 #[test]
224 fn test_format_dhms() {
225 assert_eq!(format_dhms(0), "0s");
226 assert_eq!(format_dhms(30), "30s");
227 assert_eq!(format_dhms(61), "1m1s");
228 assert_eq!(format_dhms(3600), "1h");
229 assert_eq!(format_dhms(86400), "1d");
230 assert_eq!(format_dhms(86401), "1d1s");
231 assert_eq!(format_dhms(7259), "2h59s");
232 assert_eq!(format_dhms(604_800), "7d");
233 assert_eq!(format_dhms(6_000_000), "69d10h40m");
234 assert_eq!(format_dhms(4_294_967_295_usize), "49710d6h28m15s");
235 }
236
237 #[test]
238 fn test_format_wdhms() {
239 assert_eq!(format_wdhms(0), "0s");
240 assert_eq!(format_wdhms(30), "30s");
241 assert_eq!(format_wdhms(61), "1m1s");
242 assert_eq!(format_wdhms(3600), "1h");
243 assert_eq!(format_wdhms(86400), "1d");
244 assert_eq!(format_wdhms(86401), "1d1s");
245 assert_eq!(format_wdhms(7259), "2h59s");
246 assert_eq!(format_wdhms(604_800), "1w");
247 assert_eq!(format_wdhms(6_000_000), "9w6d10h40m");
248 assert_eq!(format_wdhms(4_294_967_295_usize), "7101w3d6h28m15s");
249 }
250
251 #[test]
252 fn test_format_ns() {
253 assert_eq!(format_ns(3_000_129_723_usize), "3s129\u{b5}s723ns");
254 assert_eq!(format_ns(100_000_000_000_000_000_usize), "1157d9h46m40s");
255 assert_eq!(format_ns(100_000_000_000_000_001_usize), "1157d9h46m40s1ns");
256 assert_eq!(
257 format_ns(100_000_000_000_001_001_usize),
258 "1157d9h46m40s1\u{b5}s1ns"
259 );
260 assert_eq!(
261 format_ns(100_000_000_000_100_001_usize),
262 "1157d9h46m40s100\u{b5}s1ns"
263 );
264 assert_eq!(
265 format_ns(100_000_000_010_100_001_usize),
266 "1157d9h46m40s10ms100\u{b5}s1ns"
267 );
268 assert_eq!(
269 format_ns(100_000_000_010_000_001_usize),
270 "1157d9h46m40s10ms1ns"
271 );
272 assert_eq!(format_ns(10_000_000_000_000_000_usize), "115d17h46m40s");
273 assert_eq!(format_ns(1_000_000_000_000_000_usize), "11d13h46m40s");
274 assert_eq!(format_ns(100_000_000_000_000_usize), "1d3h46m40s");
275 assert_eq!(format_ns(10_000_000_000_000_usize), "2h46m40s");
276 assert_eq!(format_ns(1_000_000_000_000_usize), "16m40s");
277 assert_eq!(format_ns(100_000_000_000_usize), "1m40s");
278 assert_eq!(format_ns(100_000_000_010_usize), "1m40s10ns");
279 assert_eq!(format_ns(1_000_000_000), "1s");
280 assert_eq!(format_ns(100_000_000), "100ms");
281 assert_eq!(format_ns(10_000_000), "10ms");
282 assert_eq!(format_ns(1_000_001), "1ms1ns");
283 assert_eq!(format_ns(1_000_000), "1ms");
284 assert_eq!(format_ns(10_000), "10\u{b5}s");
285 assert_eq!(format_ns(1000), "1\u{b5}s");
286 assert_eq!(format_ns(100), "100ns");
287 assert_eq!(format_ns(1), "1ns");
288 assert_eq!(format_ns(0), "0ns");
289 }
290 }