adds duration_to_string to make durations pretty strings
This commit is contained in:
parent
d642822709
commit
ee5986fedf
42
src/bin/duration_to_string.rs
Normal file
42
src/bin/duration_to_string.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use std::time::Duration;
|
||||
|
||||
fn duration_to_string(to_convert: Duration) -> String {
|
||||
let mut total_seconds = to_convert.as_secs();
|
||||
let mut working_string = String::new();
|
||||
|
||||
if total_seconds > 86400 {
|
||||
// days
|
||||
let num_days = total_seconds / 86400;
|
||||
working_string = format!("{} days", num_days);
|
||||
total_seconds = total_seconds - (num_days * 86400);
|
||||
}
|
||||
|
||||
|
||||
if total_seconds > 3600 {
|
||||
// hours
|
||||
let num_hours = total_seconds / 3600;
|
||||
if num_hours > 0 {
|
||||
working_string = format!("{} {} hours", working_string, num_hours);
|
||||
total_seconds = total_seconds - (num_hours * 3600);
|
||||
}
|
||||
}
|
||||
if total_seconds > 60 {
|
||||
let num_minutes = total_seconds / 60;
|
||||
if num_minutes > 0 {
|
||||
working_string = format!("{} {} minutes", working_string, num_minutes);
|
||||
total_seconds = total_seconds - ( num_minutes * 60);
|
||||
}
|
||||
// minutes
|
||||
}
|
||||
|
||||
|
||||
working_string = format!("{} {} seconds.", working_string, total_seconds);
|
||||
|
||||
working_string
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("1m 12s => {}", duration_to_string(Duration::from_secs(72)));
|
||||
println!("1h 1m 12s => {}", duration_to_string(Duration::from_secs(3672)));
|
||||
println!("1d 1h 1m 12s => {}", duration_to_string(Duration::from_secs(90072)));
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user