Files
ncspot/src/utils.rs
Henrik Friedrichsen 32cb6e70d3 Show tracks + duration of album, playlist, queue
Stats are in the top right

Fixes #475
2021-04-08 22:26:19 +02:00

22 lines
556 B
Rust

/// Returns a human readable String of a Duration
///
/// Example: `3h 12m 53s`
pub fn format_duration(d: &std::time::Duration) -> String {
let mut s = String::new();
let mut append_unit = |value, unit| {
if value > 0 {
s.push_str(&format!("{}{}", value, unit));
}
};
let seconds = d.as_secs() % 60;
let minutes = (d.as_secs() / 60) % 60;
let hours = (d.as_secs() / 60) / 60;
append_unit(hours, "h ");
append_unit(minutes, "m ");
append_unit(seconds, "s ");
s.trim_end().to_string()
}