Initial commit
This commit is contained in:
24
utils/time_utils.go
Normal file
24
utils/time_utils.go
Normal file
@@ -0,0 +1,24 @@
|
||||
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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user