diff --git a/src/bin/duration_to_string.rs b/src/bin/duration_to_string.rs new file mode 100644 index 0000000..2d49bd0 --- /dev/null +++ b/src/bin/duration_to_string.rs @@ -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))); +} \ No newline at end of file