Skip to content

Extend format_duration with rounding to milliseconds, seconds, minutes, hours, days, month and years #35

@flxo

Description

@flxo

Sometimes displaying durations in their full precision is not needed and counter productive. Especially in logs where the quantity of a duration is in a seconds/minute/hour range the formatting of ms, us and ns might want to be omitted.

The boilerplate workaround I did some times is (beside creating a trait and blah blah...) is to do something like

format_duration(Duration::from_secs(duration.as_secs());

to get rid of the subsec part.

A possible extension could look like

/// Formats duration into a human-readable string with second precision.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use humantime::format_duration;
///
/// let val = Duration::new(9420, 1_000_000);
/// assert_eq!(format_duration_secs(val).to_string(), "2h 37m");
/// ```
pub fn format_duration_secs(val: Duration) -> FormattedDuration { ... }

/// ...
pub fn format_duration_min(val: Duration) -> FormattedDuration { ... }

/// ...
pub fn format_duration_hour(val: Duration) -> FormattedDuration { ... }
...

If applicable I happily file a PR.

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions