Skip to main content

dioxus_nox_timer/
format.rs

1//! Duration formatting utilities.
2
3/// Format seconds as `M:SS` or `H:MM:SS`.
4///
5/// - Negative values are treated as `0:00`.
6/// - Values under one hour display as `M:SS` (e.g., `1:05`).
7/// - Values one hour or above display as `H:MM:SS` (e.g., `1:01:01`).
8///
9/// # Examples
10///
11/// ```
12/// use dioxus_nox_timer::format_duration;
13///
14/// assert_eq!(format_duration(0), "0:00");
15/// assert_eq!(format_duration(65), "1:05");
16/// assert_eq!(format_duration(3661), "1:01:01");
17/// assert_eq!(format_duration(-5), "0:00");
18/// ```
19pub fn format_duration(total_seconds: i64) -> String {
20    let total_seconds = total_seconds.max(0);
21    let hours = total_seconds / 3600;
22    let minutes = (total_seconds % 3600) / 60;
23    let seconds = total_seconds % 60;
24
25    if hours > 0 {
26        format!("{hours}:{minutes:02}:{seconds:02}")
27    } else {
28        format!("{minutes}:{seconds:02}")
29    }
30}