Files
pve-exporter/utils/time_utils.go
Jan Lošťák a1ab163804 Initial commit
2024-05-27 21:27:07 +02:00

25 lines
765 B
Go

package utils
import (
"fmt"
"time"
)
// HumanDuration converts a time.Duration into a human-readable format.
// It returns the duration formatted as seconds (with two decimal places) if the duration is one second or more,
// milliseconds if the duration is one millisecond or more,
// microseconds if the duration is one microsecond or more,
// otherwise nanoseconds.
func HumanDuration(duration time.Duration) string {
switch {
case duration.Seconds() >= 1:
return fmt.Sprintf("%.2fs", duration.Seconds())
case duration.Milliseconds() >= 1:
return fmt.Sprintf("%dms", duration.Milliseconds())
case duration.Microseconds() >= 1:
return fmt.Sprintf("%dμs", duration.Microseconds())
default:
return fmt.Sprintf("%dns", duration.Nanoseconds())
}
}