Converts seconds into a human readable format (struct) containing years, days, hours, minutes and seconds.
Add this to your Cargo.toml
:
[dependencies]
secfmt = "0.1"
Add this to your *.rs
file:
extern crate secfmt;
Now you can proceed as follows:
let seconds = 31537529;
let seconds_human_readable = secfmt::from(seconds);
assert_eq!(1, secfmt::from(31537529).years);
assert_eq!(0, secfmt::from(31537529).days);
assert_eq!(0, secfmt::from(31537529).hours);
assert_eq!(25, secfmt::from(31537529).minutes);
assert_eq!(29, secfmt::from(31537529).seconds);
let shr = secfmt::from(31537529);
let s = format!("{}y {}d {}h {}m {}s", shr.years, shr.days, shr.hours, shr.minutes, shr.seconds);
assert_eq!("1y 0d 0h 25m 29s", s);
let shr = secfmt::from(31537529);
let mut duration = String::new();
match shr.years {
0 => {},
1 => duration.push_str(&format!("{} year ", shr.years)),
_ => duration.push_str(&format!("{} years ", shr.years)),
}
match shr.days {
0 => {},
1 => duration.push_str(&format!("{} day ", shr.days)),
_ => duration.push_str(&format!("{} days ", shr.days)),
}
match shr.hours {
0 => {},
1 => duration.push_str(&format!("{} hour ", shr.hours)),
_ => duration.push_str(&format!("{} hours ", shr.hours)),
}
match shr.minutes {
0 => {},
1 => duration.push_str(&format!("{} minute ", shr.minutes)),
_ => duration.push_str(&format!("{} minutes ", shr.minutes)),
}
match shr.seconds {
0 => {},
1 => duration.push_str(&format!("{} second ", shr.seconds)),
_ => duration.push_str(&format!("{} seconds ", shr.seconds)),
}
assert_eq!("1 year 25 minutes 29 seconds", duration.trim_end());
If you have questions or problems with secfmt
, then we are happy to respond to GitHub issues or come chat with us on our Gitter channel - if you have any questions about the project, or just want to say hi!
secfmt
is distributed under the terms of the MIT license.
See LICENSE for details.