zebra_chain/fmt/
time.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! Human-readable formats for times and durations.

use std::time::Duration;

/// The minimum amount of time displayed with only seconds (no milliseconds).
pub const MIN_SECONDS_ONLY_TIME: Duration = Duration::from_secs(5);

/// Returns a human-friendly formatted string for the whole number of seconds in `duration`.
pub fn duration_short(duration: impl Into<Duration>) -> String {
    let duration = duration.into();

    if duration >= MIN_SECONDS_ONLY_TIME {
        humantime_seconds(duration)
    } else {
        humantime_milliseconds(duration)
    }
}

// TODO: rename these functions to duration_*

/// Returns a human-friendly formatted string for the whole number of seconds in `duration`.
pub fn humantime_seconds(duration: impl Into<Duration>) -> String {
    let duration = duration.into();

    // Truncate fractional seconds.
    let duration = Duration::from_secs(duration.as_secs());

    let duration = humantime::format_duration(duration);

    format!("{duration}")
}

/// Returns a human-friendly formatted string for the whole number of milliseconds in `duration`.
pub fn humantime_milliseconds(duration: impl Into<Duration>) -> String {
    let duration = duration.into();

    // Truncate fractional seconds.
    let duration_secs = Duration::from_secs(duration.as_secs());
    let duration_millis = Duration::from_millis(duration.subsec_millis().into());

    let duration = humantime::format_duration(duration_secs + duration_millis);

    format!("{duration}")
}