Skip to content

Converts seconds into a human readable format (struct) containing years, days, hours, minutes and seconds.

License

Notifications You must be signed in to change notification settings

dirkeinecke/secfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

secfmt

Build Status Coverage Status Crate Crates.io (Downloads) API License Gitter

Converts seconds into a human readable format (struct) containing years, days, hours, minutes and seconds.

Usage

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:

Example 1

let seconds = 31537529;
let seconds_human_readable = secfmt::from(seconds);

Example 2

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);

Example 3

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);

Example 4

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());

Getting help

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!

License

secfmt is distributed under the terms of the MIT license.

See LICENSE for details.

About

Converts seconds into a human readable format (struct) containing years, days, hours, minutes and seconds.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages