Crates.io | humanize-duration |
lib.rs | humanize-duration |
version | 0.0.6 |
source | src |
created_at | 2024-01-15 10:54:29.461575 |
updated_at | 2024-01-15 12:07:14.569253 |
description | Convert time Duration to human readable format |
homepage | https://github.com/sephiroth74/humanize-duration |
repository | https://github.com/sephiroth74/humanize-duration |
max_upload_size | |
id | 1100227 |
size | 23,023 |
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