optimize stats

This commit is contained in:
Jason
2019-08-11 22:15:20 +08:00
parent 07a658ae0e
commit 79d45f4850
2 changed files with 31 additions and 7 deletions

View File

@@ -110,3 +110,31 @@ func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
return
}
func byteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
func byteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}