35 lines
837 B
Go
35 lines
837 B
Go
package bcap
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func FormatDuration(d time.Duration) string {
|
|
if d < time.Microsecond {
|
|
return fmt.Sprintf("%dns", d.Nanoseconds())
|
|
} else if d < time.Millisecond {
|
|
return fmt.Sprintf("%.2fµs", float64(d.Nanoseconds())/1000.0)
|
|
} else if d < time.Second {
|
|
return fmt.Sprintf("%.2fms", float64(d.Microseconds())/1000.0)
|
|
} else if d < time.Minute {
|
|
return fmt.Sprintf("%.2fs", d.Seconds())
|
|
} else if d < time.Hour {
|
|
return fmt.Sprintf("%.2fm", d.Minutes())
|
|
}
|
|
return fmt.Sprintf("%.2fh", d.Hours())
|
|
}
|
|
|
|
func FormatBytes(bytes uint64) string {
|
|
const unit = 1024
|
|
if bytes < unit {
|
|
return fmt.Sprintf("%d B", bytes)
|
|
}
|
|
div, exp := uint64(unit), 0
|
|
for n := bytes / unit; n >= unit; n /= unit {
|
|
div *= unit
|
|
exp++
|
|
}
|
|
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
|
}
|