MCPcopy Create free account
hub / github.com/efogdev/mpris-timer / FormatDuration

Function FormatDuration

internal/core/time.go:83–111  ·  view source on GitHub ↗

FormatDuration converts a time.Duration to a string in the format "HH:MM:SS" or "MM:SS". Hours are only included if the duration is 1 hour or longer. The output is zero-padded and rounded to the nearest second. Examples: - 1h30m45s -> "01:30:45" - 45m30s -> "45:30" - 30s -> "00:30" Heil hype

(d time.Duration)

Source from the content-addressed store, hash-verified

81// Heil hyperoptimization.
82// Voices in my head told me to do so.
83func FormatDuration(d time.Duration) string {
84 d = d.Round(time.Second)
85 h := d / time.Hour
86 d -= h * time.Hour
87 m := d / time.Minute
88 d -= m * time.Minute
89 s := d / time.Second
90
91 if h > 0 {
92 buf := make([]byte, 8)
93 buf[0] = '0' + byte(h/10)
94 buf[1] = '0' + byte(h%10)
95 buf[2] = ':'
96 buf[3] = '0' + byte(m/10)
97 buf[4] = '0' + byte(m%10)
98 buf[5] = ':'
99 buf[6] = '0' + byte(s/10)
100 buf[7] = '0' + byte(s%10)
101 return string(buf)
102 }
103
104 buf := make([]byte, 5)
105 buf[0] = '0' + byte(m/10)
106 buf[1] = '0' + byte(m%10)
107 buf[2] = ':'
108 buf[3] = '0' + byte(s/10)
109 buf[4] = '0' + byte(s%10)
110 return string(buf)
111}

Callers 1

runTickerMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected