forked from Angey40/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
69 lines (62 loc) · 1.32 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
57
58
59
60
61
62
63
64
65
66
67
68
69
package pcsutil
import (
"fmt"
"time"
"unsafe"
)
const (
// B byte
B = (int64)(1 << (10 * iota))
// KB kilobyte
KB
// MB megabyte
MB
// GB gigabyte
GB
// TB terabyte
TB
// PB petabyte
PB
)
// ConvertFileSize 文件大小格式化输出
func ConvertFileSize(size int64, precision ...int) string {
pint := "6"
if len(precision) == 1 {
pint = fmt.Sprint(precision[0])
}
if size <= 0 {
return "0"
}
if size < KB {
return fmt.Sprintf("%."+pint+"fB", float64(size))
}
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))
}
// ToString 将 []byte 转换为 string
func ToString(p []byte) string {
return *(*string)(unsafe.Pointer(&p))
}
// ToBytes 将 string 转换为 []byte
func ToBytes(str string) []byte {
return *(*[]byte)(unsafe.Pointer(&str))
}
// IntToBool int 类型转换为 bool
func IntToBool(i int) bool {
return i != 0
}
// FormatTime 将 Unix 时间戳, 转换为字符串
func FormatTime(t int64) string {
return time.Unix(t, 0).Format("2006-01-02 03:04:05")
}