human-time 0.1.7

This is a library for human readable elapsed time string
Documentation
#![allow(clippy::unwrap_used)]

use std::time::Duration;

pub use human_time_macros::elapsed;

// Time units in microseconds: (unit label, microseconds per unit)
const UNITS: [(&str, u128); 6] = [
    ("d", 86400000000),   // day = 24 * 60 * 60 * 1_000_000
    ("h", 3600000000),    // hour = 60 * 60 * 1_000_000
    ("m", 60000000),      // minute = 60 * 1_000_000
    ("s", 1000000),       // second = 1_000_000
    ("ms", 1000),         // millisecond = 1_000
    ("μs", 1),            // microsecond = 1
];

pub trait ToHumanTimeString {
    fn to_human_time_string(&self) -> String;
    fn to_human_time_string_with_format<F, F1>(&self, time_fmt: F, res_fmt: F1) -> String
    where
        F: Fn(u128, &str) -> String,
        F1: Fn(String, String) -> String;
}

impl ToHumanTimeString for Duration {
    fn to_human_time_string(&self) -> String {
        crate::human_time(*self)
    }

    fn to_human_time_string_with_format<F, F1>(&self, time_fmt: F, res_fmt: F1) -> String
    where
        F: Fn(u128, &str) -> String,
        F1: Fn(String, String) -> String,
    {
        human_time_with_format(*self, time_fmt, res_fmt)
    }
}

pub fn human_time(d: Duration) -> String {
    human_time_with_format(
        d,
        |n, unit| format!("{}{}", n, unit),
        |acc, item| format!("{},{}", acc, item),
    )
}

// Convert Duration to human-readable string with custom format closures.
//
// - `time_fmt`: formats each time component, e.g., `|n, unit| format!("{}{}", n, unit)`
// - `res_fmt`: combines all components, e.g., `|acc, item| format!("{},{}", acc, item)`
pub fn human_time_with_format<F, F1>(d: Duration, time_fmt: F, res_fmt: F1) -> String
where
    F: Fn(u128, &str) -> String,
    F1: Fn(String, String) -> String,
{
    // Step 1: Calculate count for each unit, building (count, unit) pairs
    let mut map: Vec<(u128, &str)> = Vec::new();
    let mut μs = d.as_micros();
    for (unit, n_μs) in UNITS {
        map.push((μs / n_μs, unit));
        μs %= n_μs;
    }

    // Step 2: Filter out zero counts, format each component, then reduce to final string
    match map
        .into_iter()
        .filter_map(|(n, u)| if n > 0 { Some(time_fmt(n, u)) } else { None })
        .reduce(res_fmt)
    {
        Some(val) => val,
        // Duration was zero, return "0" in smallest unit
        None => time_fmt(0, UNITS.last().unwrap().0),
    }
}