Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display milliseconds for low elapsed times. #598

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/format.rs
Expand Up @@ -3,6 +3,7 @@ use std::time::Duration;

use number_prefix::NumberPrefix;

const MILLISECOND: Duration = Duration::from_millis(1);
const SECOND: Duration = Duration::from_secs(1);
const MINUTE: Duration = Duration::from_secs(60);
const HOUR: Duration = Duration::from_secs(60 * 60);
Expand Down Expand Up @@ -104,6 +105,7 @@ const UNITS: &[(Duration, &str, &str)] = &[
(HOUR, "hour", "h"),
(MINUTE, "minute", "m"),
(SECOND, "second", "s"),
(MILLISECOND, "millisecond", "ms"),
];

impl fmt::Display for HumanBytes {
Expand Down Expand Up @@ -192,25 +194,39 @@ mod tests {
#[test]
fn human_duration_less_than_one_second() {
assert_eq!(
"0 seconds",
"0 milliseconds",
format!("{}", HumanDuration(Duration::from_secs(0)))
);
assert_eq!("0 seconds", format!("{}", HumanDuration(MILLI)));
assert_eq!("0 seconds", format!("{}", HumanDuration(499 * MILLI)));
assert_eq!("1 second", format!("{}", HumanDuration(500 * MILLI)));
assert_eq!("1 second", format!("{}", HumanDuration(999 * MILLI)));
assert_eq!("1 millisecond", format!("{}", HumanDuration(MILLI)));
assert_eq!("2 milliseconds", format!("{}", HumanDuration(2 * MILLI)));
assert_eq!(
"499 milliseconds",
format!("{}", HumanDuration(499 * MILLI))
);
assert_eq!(
"500 milliseconds",
format!("{}", HumanDuration(500 * MILLI))
);
assert_eq!(
"999 milliseconds",
format!("{}", HumanDuration(999 * MILLI))
);
}

#[test]
fn human_duration_less_than_two_seconds() {
assert_eq!("1 second", format!("{}", HumanDuration(1499 * MILLI)));
assert_eq!(
"1499 milliseconds",
format!("{}", HumanDuration(1499 * MILLI))
);
assert_eq!("2 seconds", format!("{}", HumanDuration(1500 * MILLI)));
assert_eq!("2 seconds", format!("{}", HumanDuration(1999 * MILLI)));
}

#[test]
fn human_duration_one_unit() {
assert_eq!("1 second", format!("{}", HumanDuration(SECOND)));
assert_eq!("1 millisecond", format!("{}", HumanDuration(MILLI)));
assert_eq!("1000 milliseconds", format!("{}", HumanDuration(SECOND)));
assert_eq!("60 seconds", format!("{}", HumanDuration(MINUTE)));
assert_eq!("60 minutes", format!("{}", HumanDuration(HOUR)));
assert_eq!("24 hours", format!("{}", HumanDuration(DAY)));
Expand Down