5 releases

0.0.6 Jan 15, 2024
0.0.5 Jan 15, 2024
0.0.4 Jan 15, 2024
0.0.3 Jan 15, 2024
0.0.2 Jan 15, 2024

#92 in Value formatting

Download history 12/week @ 2024-01-14 18/week @ 2024-02-25 2/week @ 2024-03-10 76/week @ 2024-03-31

78 downloads per month

MIT license

21KB
494 lines

humanize-duration

crates.io ci

Convert time Duration to human-readable format with the ability to truncate the output string to a specific time unit.
It supports time::Duration, core::time::Duration and chrono::Duration (using the chrono feature)

Example:

    use humanize_duration::Truncate;
    use humanize_duration::prelude::DurationExt;

    pub fn main() {
        let duration = time::Duration::nanoseconds(131_200_001_301_021_123);
        let human = duration.human(Truncate::Nano);
        println!("duration: {}", human);
    }

It will print: 4years 1month 27days 2h 36m 17s 301ms 21µs 123ns

While:

    use humanize_duration::Truncate;
    use humanize_duration::prelude::DurationExt;
    
    pub fn main() {
        let duration = time::Duration::nanoseconds(131_200_001_301_021_123);
        let human2 = duration.human(Truncate::Day);
        println!("duration: {}", human2);
    }

It will print: 4years 1month 27days

Custom formatting is also possible. For example:

    use humanize_duration::{*};
    use humanize_duration::prelude::DurationExt;
    use humanize_duration::types::DurationParts;

    use time::Duration as TimeDuration;
    struct MyFormatter;

    unit!(MyYear, " anno", " anni");
    unit!(MyMonth, " mese", " mesi");
    unit!(MyDay, " giorno", " giorni");
    unit!(MyHour, " ora", " ore");
    unit!(MyMinute, " minuto", " minuti");
    unit!(MySecond, " secondo", " secondi");
    unit!(MyMillis, " millisecondo", " millisecondi");
    unit!(MyMicro, " microsecondo", " microsecondi");
    unit!(MyNano, " nanosecondo", " nanosecondi");

    impl Formatter for MyFormatter {
        fn get(&self, truncate: Truncate) -> Box<dyn Unit> {
            match truncate {
                Truncate::Nano => Box::new(MyNano),
                Truncate::Micro => Box::new(MyMicro),
                Truncate::Millis => Box::new(MyMillis),
                Truncate::Second => Box::new(MySecond),
                Truncate::Minute => Box::new(MyMinute),
                Truncate::Hour => Box::new(MyHour),
                Truncate::Day => Box::new(MyDay),
                Truncate::Month => Box::new(MyMonth),
                Truncate::Year => Box::new(MyYear),
            }
        }

        fn format(&self, f: &mut std::fmt::Formatter<'_>, parts: DurationParts, truncate: Truncate) -> std::fmt::Result {
            self.format_default(f, parts, truncate)
        }
    }

    pub fn main() {
        let duration = TimeDuration::nanoseconds(150_345_202_557_001);
        let human = duration.human_with_format(Truncate::Nano, MyFormatter);
        println!("human: {human}");
    }

It will print: 1 giorno 17 ore 45 minuti 45 secondi 202 millisecondi 557 microsecondi 1 nanosecondo

Dependencies

~0.7–1MB
~17K SLoC