chore: add convert bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB)
All checks were successful
Lint / Run on Ubuntu (push) Successful in 20m39s
Lint / Run on Ubuntu (pull_request) Successful in 20m14s

This commit is contained in:
2024-12-12 12:43:06 +01:00
parent ad2f3a1990
commit abf1aeba7c
8 changed files with 31 additions and 14 deletions

View File

@@ -255,6 +255,18 @@ func CronNextTime(cronExpr string) time.Time {
return next
}
func BytesToMb(b uint64) uint64 {
return b / 1024 / 1024
// ConvertBytes converts bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB).
func ConvertBytes(bytes uint64) string {
const (
MiB = 1024 * 1024
GiB = MiB * 1024
)
switch {
case bytes >= GiB:
return fmt.Sprintf("%.2f GiB", float64(bytes)/float64(GiB))
case bytes >= MiB:
return fmt.Sprintf("%.2f MiB", float64(bytes)/float64(MiB))
default:
return fmt.Sprintf("%d bytes", bytes)
}
}