Millisecond crate
A better way to format and display duration, which converts 33023448000ms to 1y 17d 5h 10m 48s or relatively
timestamp ofabout a year ago.
Install
In your Rust project's root directory run:
$ cargo add millisecond
Example
// Activate and bring the crate into scope
use *;
Options
Customize the parser and the output format using the MillisecondOption struct.
| Option | Description | Example |
|---|---|---|
long |
uses full and descriptive labels for time units, such as years instead of abbreviated forms like y |
2y -> 2 years |
dominant_only |
displays the most dominant part only (the most left part). | 1y 2d -> 1y |
days_instead_of_years |
displays time durations in days rather than converting them into years. | 1y 1d -> 366d |
format_sub_milliseconds |
shows and formats microseconds and nanoseconds if present. | 1s 2ms -> 1s 2ms 3µs 4ns |
seconds |
determines whether to display seconds and Milliseconds or not. Can be Separate (default), Combine, CombineWith, or Hide. |
1s 2ms -> 1.2s |
All options have default value unless specified
Options 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 ()
};
Day of Week
Calculating the weekday could be easy if the duration was calculated from a known epoch. The weekday function
is implemented to convert the duration into the proper weekday value (enum).
use *;
use ;
License
MIT
Inspiration
This crate is inspired by pretty-ms npm package.