humanize-duration

Crates.iohumanize-duration
lib.rshumanize-duration
version0.0.6
sourcesrc
created_at2024-01-15 10:54:29.461575
updated_at2024-01-15 12:07:14.569253
descriptionConvert time Duration to human readable format
homepagehttps://github.com/sephiroth74/humanize-duration
repositoryhttps://github.com/sephiroth74/humanize-duration
max_upload_size
id1100227
size23,023
Alessandro Crugnola (sephiroth74)

documentation

README

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

Commit count: 0

cargo fmt