Removes 'magic numbers' and uses constants for number of seconds in hour and day
This commit is contained in:
parent
002ad36068
commit
613f22d31c
@ -1,41 +0,0 @@
|
||||
use std::time::Duration;
|
||||
|
||||
pub 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)));
|
||||
println!("30d 1h 1m 12s => {}", duration_to_string(Duration::from_secs(2595672)));
|
||||
}
|
||||
@ -17,31 +17,35 @@ use std::{env, error::Error, ffi::OsString, process};
|
||||
use color_eyre::owo_colors::OwoColorize;
|
||||
use crossterm::style::Stylize;
|
||||
use log::debug;
|
||||
const SECONDS_IN_MINUTE: u32 = 60;
|
||||
const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60;
|
||||
const SECONDS_IN_DAY: u32 = SECONDS_IN_HOUR * 24;
|
||||
|
||||
|
||||
pub fn duration_to_string(to_convert: Duration) -> String {
|
||||
let mut total_seconds = to_convert.as_secs();
|
||||
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 / 86400;
|
||||
let num_days = (total_seconds / SECONDS_IN_DAY) as u32;
|
||||
working_string = format!("{} days", num_days);
|
||||
total_seconds = total_seconds - (num_days * 86400);
|
||||
total_seconds = total_seconds - (num_days * SECONDS_IN_DAY);
|
||||
}
|
||||
|
||||
if total_seconds > 3600 {
|
||||
// hours
|
||||
let num_hours = total_seconds / 3600;
|
||||
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 * 3600);
|
||||
total_seconds = total_seconds - (num_hours * SECONDS_IN_HOUR);
|
||||
}
|
||||
}
|
||||
if total_seconds > 60 {
|
||||
let num_minutes = 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 * 60);
|
||||
total_seconds = total_seconds - (num_minutes * SECONDS_IN_MINUTE);
|
||||
}
|
||||
// minutes
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user