-
Notifications
You must be signed in to change notification settings - Fork 29
/
time.go
63 lines (52 loc) · 1.47 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
55
56
57
58
59
60
61
62
63
package utils
import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/iwind/TeaGo/types"
"time"
)
var unixTime = time.Now().Unix()
var unixTimeMilli = time.Now().UnixMilli()
var unixTimeMilliString = types.String(unixTimeMilli)
func init() {
var ticker = time.NewTicker(200 * time.Millisecond)
goman.New(func() {
for range ticker.C {
unixTime = time.Now().Unix()
unixTimeMilli = time.Now().UnixMilli()
unixTimeMilliString = types.String(unixTimeMilli)
}
})
}
// UnixTime 最快获取时间戳的方式,通常用在不需要特别精确时间戳的场景
func UnixTime() int64 {
return unixTime
}
// FloorUnixTime 取整
func FloorUnixTime(seconds int) int64 {
return UnixTime() / int64(seconds) * int64(seconds)
}
// CeilUnixTime 取整并加1
func CeilUnixTime(seconds int) int64 {
return UnixTime()/int64(seconds)*int64(seconds) + int64(seconds)
}
// NextMinuteUnixTime 获取下一分钟开始的时间戳
func NextMinuteUnixTime() int64 {
return CeilUnixTime(60)
}
// UnixTimeMilli 获取时间戳,精确到毫秒
func UnixTimeMilli() int64 {
return unixTimeMilli
}
func UnixTimeMilliString() (int64, string) {
return unixTimeMilli, unixTimeMilliString
}
// GMTUnixTime 计算GMT时间戳
func GMTUnixTime(timestamp int64) int64 {
_, offset := time.Now().Zone()
return timestamp - int64(offset)
}
// GMTTime 计算GMT时间
func GMTTime(t time.Time) time.Time {
_, offset := time.Now().Zone()
return t.Add(-time.Duration(offset) * time.Second)
}