@@ -83,10 +83,8 @@ func (d Duration) String() string {
8383 d = d - d % Duration (time .Minute )
8484 } else if d .Minutes () > 2 {
8585 d = d - d % Duration (time .Second )
86- } else if d .Seconds () > 2 {
87- d = d - d % Duration (time .Millisecond )
8886 } else if d .Nanoseconds () > 2 * 1000 * 1000 {
89- d = d - d % Duration (time .Microsecond )
87+ d = d - d % Duration (time .Millisecond )
9088 }
9189 // Largest time is 2540400h10m10.000000000s
9290 var buf [32 ]byte
@@ -213,7 +211,22 @@ func (d Duration) String() string {
213211 buf [w ] = '-'
214212 }
215213
216- return strings .ReplaceAll (strings .ReplaceAll (string (buf [w :]), "0s" , "" ), "0m" , "" )
214+ result := string (buf [w :])
215+ // Strip standalone zero-valued units "0m" (minutes) and "0s" (seconds).
216+ // Must not match inside sub-second units like "320ms", "500µs", "100ns".
217+
218+ // Strip "0m" only if NOT followed by "s" (which would make it "0ms" = milliseconds)
219+ if i := strings .Index (result , "0m" ); i >= 0 {
220+ if i + 2 >= len (result ) || result [i + 2 ] != 's' {
221+ result = result [:i ] + result [i + 2 :]
222+ }
223+ }
224+ // Strip "0s" only at the very end, and only if the result contains
225+ // larger units (meaning "0s" is a trailing zero seconds, not "0s" standalone)
226+ if strings .HasSuffix (result , "0s" ) && len (result ) > 2 {
227+ result = result [:len (result )- 2 ]
228+ }
229+ return result
217230}
218231
219232// fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the
0 commit comments