Skip to content

Commit d826c98

Browse files
committed
refactor(human-readable):
- throw away handmade code for time presentation - now all is offloaded to the `humantime` crate - fix some little annoying details of too precise human readable time
1 parent da5f472 commit d826c98

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/common/utils.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::Context;
22
use humantime::{format_duration, parse_duration};
3+
use std::ops::Add;
34
use std::time::Duration;
45

56
const ONE_MIN: Duration = Duration::from_secs(60);
@@ -15,7 +16,13 @@ impl HumanReadable for Duration {
1516
if self >= &ONE_SEC && self < &ONE_MIN {
1617
format!("~{}s", self.as_secs_f32().round())
1718
} else {
18-
format_duration(*self).to_string()
19+
let mut less = Duration::from_millis(self.as_millis() as u64);
20+
let mut prefix = "";
21+
if less < *self {
22+
prefix = "~";
23+
less = less.add(Duration::from_millis(1))
24+
}
25+
format!("{}{}", prefix, format_duration(less).to_string())
1926
}
2027
}
2128
}
@@ -55,8 +62,10 @@ mod tests {
5562
assert_eq!(Duration::from_millis(1200).as_human_readable(), "~1s");
5663
assert_eq!(Duration::from_millis(1800).as_human_readable(), "~2s");
5764
assert_eq!(Duration::from_millis(100).as_human_readable(), "100ms");
58-
assert_eq!(Duration::from_micros(10).as_human_readable(), "10us");
59-
assert_eq!(Duration::from_nanos(10).as_human_readable(), "10ns");
65+
assert_eq!(Duration::from_micros(10).as_human_readable(), "~1ms");
66+
assert_eq!(Duration::from_nanos(10).as_human_readable(), "~1ms");
67+
// this is 1.12ms so ~2ms
68+
assert_eq!(Duration::from_micros(1120).as_human_readable(), "~2ms");
6069
}
6170

6271
#[test]

0 commit comments

Comments
 (0)