Merge pull request #982 from Aloso/master

Format durations nicely
This commit is contained in:
Jonathan Turner 2019-11-18 11:49:58 +13:00 committed by GitHub
commit c04da4c232
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -172,13 +172,7 @@ impl Primitive {
_ => format!("{}", byte.format(1)),
}
}
Primitive::Duration(sec) => {
if *sec == 1 {
format!("{} sec", sec)
} else {
format!("{} secs", sec)
}
} //FIXME: make nicer duration output
Primitive::Duration(sec) => format_duration(*sec),
Primitive::Int(i) => format!("{}", i),
Primitive::Decimal(decimal) => format!("{}", decimal),
Primitive::Pattern(s) => format!("{}", s),
@ -205,6 +199,20 @@ impl Primitive {
}
}
fn format_duration(sec: u64) -> String {
let (minutes, seconds) = (sec / 60, sec % 60);
let (hours, minutes) = (minutes / 60, minutes % 60);
let (days, hours) = (hours / 24, hours % 24);
match (days, hours, minutes, seconds) {
(0, 0, 0, 1) => format!("1 sec"),
(0, 0, 0, s) => format!("{} secs", s),
(0, 0, m, s) => format!("{}:{:02}", m, s),
(0, h, m, s) => format!("{}:{:02}:{:02}", h, m, s),
(d, h, m, s) => format!("{}:{:02}:{:02}:{:02}", d, h, m, s),
}
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, new, Serialize)]
pub struct Operation {
pub(crate) left: Value,