From 764bf5572383583cdaba3b731bf70d600b7d000a Mon Sep 17 00:00:00 2001 From: Deluan Date: Mon, 27 Jul 2026 14:15:57 -0400 Subject: [PATCH] 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. --- log/formatters.go | 11 +++++++++-- log/formatters_test.go | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/log/formatters.go b/log/formatters.go index 0b27f3a43..5d864dae4 100644 --- a/log/formatters.go +++ b/log/formatters.go @@ -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 { diff --git a/log/formatters_test.go b/log/formatters_test.go index 6ed43a094..64d72bdbd 100644 --- a/log/formatters_test.go +++ b/log/formatters_test.go @@ -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"), )