25 lines
765 B
Go
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())
|
|
}
|
|
}
|