fix(log): stop ShortDur eating significant trailing zeros

TrimSuffix(s, "0s") was meant to turn "4h0m0s" into "4h", but it strips
any trailing "0s"/"0m" -- so 10s logged as "1", 20s as "2", 1m30s as
"1m3", 2h30m as "2h3", and a zero duration as the empty string. Every
elapsed/duration field in the app was affected.

The suffix now has to include the preceding unit, so only a whole
zero-valued component is dropped. The existing table only covered values
that dodge the bug (4m, 4h, 4m3s); added the ones that don't.
This commit is contained in:
Deluan 2026-07-27 14:15:57 -04:00
parent 3a0190dff3
commit 764bf55723
2 changed files with 15 additions and 2 deletions

View File

@ -26,8 +26,15 @@ func ShortDur(d time.Duration) string {
default:
s = d.String()
}
s = strings.TrimSuffix(s, "0s")
return strings.TrimSuffix(s, "0m")
// Drop whole zero-valued trailing components ("4h0m0s" -> "4h"). The suffix has to include
// the preceding unit, or a value that merely ends in a zero digit loses it: "10s" -> "1".
if strings.HasSuffix(s, "m0s") {
s = strings.TrimSuffix(s, "0s")
}
if strings.HasSuffix(s, "h0m") {
s = strings.TrimSuffix(s, "0m")
}
return s
}
func StringerValue(s fmt.Stringer) string {

View File

@ -25,6 +25,12 @@ var _ = DescribeTable("ShortDur",
Entry("4m3s", 4*time.Minute+3*time.Second, "4m3s"),
Entry("4h", 4*time.Hour, "4h"),
Entry("4h", 4*time.Hour+2*time.Second, "4h"),
// A trailing zero digit is significant: only a whole zero-valued component may be dropped.
Entry("zero", time.Duration(0), "0s"),
Entry("10s", 10*time.Second, "10s"),
Entry("20s", 20*time.Second, "20s"),
Entry("1m30s", time.Minute+30*time.Second, "1m30s"),
Entry("2h30m", 2*time.Hour+30*time.Minute, "2h30m"),
Entry("4h2m", 4*time.Hour+2*time.Minute+5*time.Second+200*time.Millisecond, "4h2m"),
)