#human-readable #timestamp #pretty-ms #pretty-date #relative-date-time

no-std millisecond

Format milliseconds into a human-readable and relative timestamp formats. This package has no-std dependency.

6 releases (3 breaking)

new 0.6.2 Mar 22, 2025
0.6.1 Feb 26, 2025
0.5.1 Feb 20, 2025
0.4.1 Feb 16, 2025
0.1.0 Jun 27, 2024

#76 in Value formatting

Download history 83/week @ 2024-11-27 84/week @ 2024-12-04 94/week @ 2024-12-11 76/week @ 2024-12-18 58/week @ 2024-12-25 69/week @ 2025-01-01 77/week @ 2025-01-08 69/week @ 2025-01-15 61/week @ 2025-01-22 55/week @ 2025-01-29 49/week @ 2025-02-05 224/week @ 2025-02-12 404/week @ 2025-02-19 251/week @ 2025-02-26 36/week @ 2025-03-05 30/week @ 2025-03-12

743 downloads per month

MIT license

52KB
1.5K SLoC

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 millisecond::prelude::*;

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

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

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

    println!("{}", dur.relative());
    // displays: about a year ago

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

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

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 millisecond::prelude::*;
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
    let dur = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
    println!("today: {}", dur.weekday());
}

License

MIT

Inspiration

This crate is inspired by pretty-ms npm package.

No runtime deps