-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathconvert.go
56 lines (51 loc) · 1.04 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package pcsutil
import (
"fmt"
"time"
)
const (
b = (int64)(1 << (10 * iota))
kb
mb
gb
tb
pb
)
// ConvertFileSize 文件大小格式化输出
func ConvertFileSize(size int64, precision ...int) string {
p := 6
if len(precision) == 1 {
p = precision[0]
}
pint := fmt.Sprint(p)
if size <= 0 {
return "0"
}
if size < kb {
return fmt.Sprintf("%."+pint+"fB", float64(size)/float64(b))
}
if size < mb {
return fmt.Sprintf("%."+pint+"fKB", float64(size)/float64(kb))
}
if size < gb {
return fmt.Sprintf("%."+pint+"fMB", float64(size)/float64(mb))
}
if size < tb {
return fmt.Sprintf("%."+pint+"fGB", float64(size)/float64(gb))
}
if size < pb {
return fmt.Sprintf("%."+pint+"fTB", float64(size)/float64(tb))
}
return fmt.Sprintf("%."+pint+"fPB", float64(size)/float64(pb))
}
// IntToBool int 类型转换为 bool
func IntToBool(i int) bool {
if i == 0 {
return false
}
return true
}
// FormatTime 讲 Unix 时间戳, 转换为字符串
func FormatTime(t int64) string {
return time.Unix(t, 0).Format("2006-01-02 03:04:05")
}