millisecond 0.4.0

Format milliseconds into a human-readable format. It also, parse a milliseonds into its core parts, including years, days, hours, etc. This package has no-std dependency.
Documentation
millisecond-0.4.0 has been yanked.

Millisecond crate

A better way to format and display time, which converts 33023448000ms to 1y 17d 5h 10m 48s

Install

In your Rust project root directory run:

$ cargo add millisecond

Example

use millisecond::prelude::*;

fn main() {
    // Obtain a duration instance
    let dur = core::time::Duration::from_millis(33_023_448_000);

    println!("pretty: {}", dur.pretty());
    // pretty: 1y 17d 5h 10m 48s

    println!("pretty_with: {}", dur.pretty_with(&MillisecondOption::long()));
    // pretty_with: 1 year 17 days 5 hours 10 minutes 48 seconds

    // the previous solution still works
    let ms = Millisecond::from_millis(33_023_448_000);
    println!("ms: {}", ms.pretty());
    // dur: 1y 17d 5h 10m 48s
}

Options

Customize the parser and the output format using the MillisecondOption struct.

Option Description
long When enabled, uses full and descriptive labels for time units, such as years instead of abbreviated forms like y.
days_instead_of_years When activated, displays time durations in days rather than converting them into years.

Option creating shorthand

In order to easily create a MillisecondOption instance, you can use the MillisecondOption::default() method:

let option = MillisecondOption{
    days_instead_of_years: true,
    ..MillisecondOption::default()
};

License

MIT

Inspiration

This crate is inspired by pretty-ms npm package.