Update format.go

This commit is contained in:
Doryu 2025-11-27 00:49:35 +01:00 committed by GitHub
parent 7805e203f0
commit e77f5d54c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,8 +17,8 @@
package common package common
import ( import (
"fmt"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -66,11 +66,15 @@ func (t PrettyAge) String() string {
return "0" return "0"
} }
// Accumulate a precision of 3 components before returning // Accumulate a precision of 3 components before returning
result, prec := "", 0 var (
builder strings.Builder
prec int
)
for _, unit := range ageUnits { for _, unit := range ageUnits {
if diff >= unit.Size { if diff >= unit.Size {
result = fmt.Sprintf("%s%d%s", result, diff/unit.Size, unit.Symbol) builder.WriteString(strconv.FormatInt(int64(diff/unit.Size), 10))
builder.WriteString(unit.Symbol)
diff %= unit.Size diff %= unit.Size
if prec += 1; prec >= 3 { if prec += 1; prec >= 3 {
@ -78,5 +82,5 @@ func (t PrettyAge) String() string {
} }
} }
} }
return result return builder.String()
} }