make one you can send a 'nil' to it

This commit is contained in:
Jeff Carr 2024-12-16 23:59:16 -06:00
parent 2b1b847143
commit d564d4f2d7
1 changed files with 22 additions and 1 deletions

21
time.go
View File

@ -51,6 +51,14 @@ func GetDurationStamp(t time.Time) string {
return FormatDuration(duration) return FormatDuration(duration)
} }
// allows nil
func HumanDuration(d *time.Duration) string {
if d == nil {
return ""
}
return FormatDuration(*d)
}
func FormatDuration(d time.Duration) string { func FormatDuration(d time.Duration) string {
result := "" result := ""
@ -94,6 +102,19 @@ func FormatDuration(d time.Duration) string {
if ms > 100 { if ms > 100 {
// todo: print .3s, etc ? // todo: print .3s, etc ?
} }
if ms > 0 {
result += fmt.Sprintf("%dms", ms) result += fmt.Sprintf("%dms", ms)
return result return result
} }
// report in milliseconds
mc := int(d.Microseconds())
if mc > 0 {
result += fmt.Sprintf("%dmc", mc)
return result
}
ns := int(d.Nanoseconds())
result += fmt.Sprintf("%dns", ns)
return result
}