Adds Duration to String for converting a duration to a human readable length of time

This commit is contained in:
Trevor Merritt 2025-07-23 08:54:29 -04:00
parent 0b11b91fe4
commit 3280123d22
2 changed files with 37 additions and 0 deletions

36
src/duration_to_string.rs Normal file
View File

@ -0,0 +1,36 @@
use std::time::Duration;
pub fn duration_to_string(to_convert: Duration) -> String {
let mut total_seconds = to_convert.as_secs() as u32;
let mut working_string = String::new();
if total_seconds > 86400 {
// days
let num_days = (total_seconds / SECONDS_IN_DAY) as u32;
working_string = format!("{} days", num_days);
total_seconds = total_seconds - (num_days * SECONDS_IN_DAY);
}
if total_seconds > 3600 {
// hours
let num_hours = (total_seconds / SECONDS_IN_HOUR) as u32;
if num_hours > 0 {
working_string = format!("{} {} hours", working_string, num_hours);
total_seconds = total_seconds - (num_hours * SECONDS_IN_HOUR);
}
}
if total_seconds > 60 {
let num_minutes = (total_seconds / SECONDS_IN_MINUTE) as u32;
if num_minutes > 0 {
working_string = format!("{} {} minutes", working_string, num_minutes);
total_seconds = total_seconds - (num_minutes * SECONDS_IN_MINUTE);
}
// minutes
}
working_string = format!("{} {} seconds", working_string, total_seconds);
working_string
}

View File

@ -1,3 +1,4 @@
pub mod number_system_conversion;
pub mod test_compression;
pub mod data_to_text;
pub mod duration_to_string;