Files
opencloud/vendor/github.com/gookit/goutil/basefn/extfunc.go
dependabot[bot] 5ebc596352 Bump github.com/gookit/config/v2 from 2.1.8 to 2.2.2
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.1.8 to 2.2.2.
- [Release notes](https://github.com/gookit/config/releases)
- [Commits](https://github.com/gookit/config/compare/v2.1.8...v2.2.2)

---
updated-dependencies:
- dependency-name: github.com/gookit/config/v2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-13 10:54:58 +02:00

75 lines
1.4 KiB
Go

package basefn
import (
"fmt"
)
// DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB
//
// Usage:
//
// file, err := os.Open(path)
// fl, err := file.Stat()
// fmtSize := DataSize(fl.Size())
func DataSize(size uint64) string {
switch {
case size < 1024:
return fmt.Sprintf("%dB", size)
case size < 1024*1024:
return fmt.Sprintf("%.2fK", float64(size)/1024)
case size < 1024*1024*1024:
return fmt.Sprintf("%.2fM", float64(size)/1024/1024)
default:
return fmt.Sprintf("%.2fG", float64(size)/1024/1024/1024)
}
}
var timeFormats = [][]int{
{0},
{1},
{2, 1},
{60},
{120, 60},
{3600},
{7200, 3600},
{86400},
{172800, 86400},
}
var timeMessages = []string{
"< 1 sec", "1 sec", "secs", "1 min", "mins", "1 hr", "hrs", "1 day", "days",
}
// HowLongAgo format a seconds, get how lang ago
func HowLongAgo(sec int64) string {
intVal := int(sec)
length := len(timeFormats)
for i, item := range timeFormats {
if intVal >= item[0] {
ni := i + 1
match := false
if ni < length { // next exists
next := timeFormats[ni]
if intVal < next[0] { // current <= intVal < next
match = true
}
} else if ni == length { // current is last
match = true
}
if match { // match success
if len(item) == 1 {
return timeMessages[i]
}
// len is 2
return fmt.Sprintf("%d %s", intVal/item[1], timeMessages[i])
}
}
}
return "unknown" // He should never happen
}