forked from Angey40/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
54 lines (48 loc) · 1.29 KB
/
time.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
// Package pcstime 时间工具包
package pcstime
import (
"fmt"
"time"
)
var (
// CSTLocation 东八区时区
CSTLocation = time.FixedZone("CST", 8*3600)
)
/*
BeijingTimeOption 根据给定的 get 返回时间格式.
get: 时间格式
"Refer": 2017-7-21 12:02:32.000
"printLog": 2017-7-21_12:02:32
"day": 21
"ymd": 2017-7-21
"hour": 12
默认时间戳: 1500609752
*/
func BeijingTimeOption(get string) string {
//获取北京(东八区)时间
now := time.Now().In(CSTLocation)
year, mon, day := now.Date()
hour, min, sec := now.Clock()
millisecond := now.Nanosecond() / 1e6
switch get {
case "Refer":
return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d.%03d", year, mon, day, hour, min, sec, millisecond)
case "printLog":
return fmt.Sprintf("%d-%d-%d_%02dh%02dm%02ds", year, mon, day, hour, min, sec)
case "day":
return fmt.Sprint(day)
case "ymd":
return fmt.Sprintf("%d-%d-%d", year, mon, day)
case "hour":
return fmt.Sprint(hour)
default:
return fmt.Sprint(time.Now().Unix())
}
}
// FormatTime 将 Unix 时间戳, 转换为字符串
func FormatTime(t int64) string {
tt := time.Unix(t, 0).In(CSTLocation)
year, mon, day := tt.Date()
hour, min, sec := tt.Clock()
return fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d", year, mon, day, hour, min, sec)
}