-
Notifications
You must be signed in to change notification settings - Fork 114
/
time.go
246 lines (218 loc) · 5.73 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package antnet
import (
"time"
)
func ParseTime(str string) (time.Time, error) {
return time.Parse("2006-01-02 15:04:05", str)
}
func Date() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func UnixTime(sec, nsec int64) time.Time {
return time.Unix(sec, nsec)
}
func UnixUs() int64 {
return time.Now().UnixNano() / 1000
}
func UnixMs() int64 {
return time.Now().UnixNano() / 1000000
}
func UnixNano() int64 {
return time.Now().UnixNano()
}
func Now() time.Time {
return time.Now()
}
func NewTimer(ms int) *time.Timer {
return time.NewTimer(time.Millisecond * time.Duration(ms))
}
func NewTicker(ms int) *time.Ticker {
return time.NewTicker(time.Millisecond * time.Duration(ms))
}
func After(ms int) <-chan time.Time {
return time.After(time.Millisecond * time.Duration(ms))
}
func Tick(ms int) <-chan time.Time {
return time.Tick(time.Millisecond * time.Duration(ms))
}
func Sleep(ms int) {
time.Sleep(time.Millisecond * time.Duration(ms))
}
func SetTimeout(inteval int, fn func(...interface{}) int, args ...interface{}) {
if inteval < 0 {
LogError("new timeout inteval:%v ms", inteval)
return
}
LogInfo("new timeout inteval:%v ms", inteval)
Go2(func(cstop chan struct{}) {
tick := time.NewTimer(time.Millisecond * time.Duration(inteval))
for inteval > 0 {
select {
case <-cstop:
inteval = 0
case <-tick.C:
inteval = fn(args...)
if inteval > 0 {
tick.Reset(time.Millisecond * time.Duration(inteval))
}
}
}
tick.Stop()
})
}
func timerTick() {
now := time.Now()
StartTick = now.UnixNano() / 1000000
NowTick = StartTick
Timestamp = NowTick / 1000
TimeString = now.Format("2006-01-02 15:04:05")
lastTimestamp := Timestamp
var ticker = time.NewTicker(time.Millisecond)
Go(func() {
for IsRuning() {
select {
case <-ticker.C:
now := time.Now()
NowTick = now.UnixNano() / 1000000
Timestamp = NowTick / 1000
if Timestamp != lastTimestamp {
lastTimestamp = Timestamp
TimeString = now.Format("2006-01-02 15:04:05")
}
}
}
ticker.Stop()
})
}
/**
* @brief 获得timestamp距离下个小时的时间,单位s
*
* @return uint32_t 距离下个小时的时间,单位s
*/
func GetNextHourIntervalS() int {
return int(3600 - (Timestamp % 3600))
}
/**
* @brief 获得timestamp距离下个小时的时间,单位ms
*
* @return uint32_t 距离下个小时的时间,单位ms
*/
func GetNextHourIntervalMS() int {
return GetNextHourIntervalS() * 1000
}
/**
* @brief 时间戳转换为小时,24小时制,0点用24表示
*
* @param timestamp 时间戳
* @param timezone 时区
* @return uint32_t 小时 范围 1-24
*/
func GetHour24(timestamp int64, timezone int) int {
hour := int((timestamp%86400)/3600) + timezone
if hour > 24 {
return hour - 24
}
return hour
}
/**
* @brief 时间戳转换为小时,24小时制,0点用0表示
*
* @param timestamp 时间戳
* @param timezone 时区
* @return uint32_t 小时 范围 0-23
*/
func GetHour23(timestamp int64, timezone int) int {
hour := GetHour24(timestamp, timezone)
if hour == 24 {
return 0 //24点就是0点
}
return hour
}
func GetHour(timestamp int64, timezone int) int {
return GetHour23(timestamp, timezone)
}
/**
* @brief 判断两个时间戳是否是同一天
*
* @param now 需要比较的时间戳
* @param old 需要比较的时间戳
* @param timezone 时区
* @return uint32_t 返回不同的天数
*/
func IsDiffDay(now, old int64, timezone int) int {
now += int64(timezone * 3600)
old += int64(timezone * 3600)
return int((now / 86400) - (old / 86400))
}
/**
* @brief 判断时间戳是否处于两个小时之内
*
* @param now 需要比较的时间戳
* @param hour_start 起始的小时 0 -23
* @param hour_end 结束的小时 0 -23
* @param timezone 时区
* @return bool true表示处于两个小时之间
*/
func IsBetweenHour(now int64, hour_start, hour_end int, timezone int) bool {
hour := GetHour23(now, timezone)
return (hour >= hour_start) && (hour < hour_end)
}
/**
* @brief 判断时间戳是否处于一个小时的两边,即一个时间错大于当前的hour,一个小于
*
* @param now 需要比较的时间戳
* @param old 需要比较的时间戳
* @param hour 小时,0-23
* @param timezone 时区
* @return bool true表示时间戳是否处于一个小时的两边
*/
func IsDiffHour(now, old int64, hour, timezone int) bool {
diff := IsDiffDay(now, old, timezone)
if diff == 1 {
if GetHour23(old, timezone) > hour {
return GetHour23(now, timezone) >= hour
} else {
return true
}
} else if diff >= 2 {
return true
}
return (GetHour23(now, timezone) >= hour) && (GetHour23(old, timezone) < hour)
}
/**
* @brief 判断时间戳是否处于跨周, 在周一跨天节点的两边
*
* @param now 需要比较的时间戳
* @param old 需要比较的时间戳
* @param hour 小时,0-23
* @param timezone 时区
* @return bool true表示时间戳是否处于跨周, 在周一跨天节点的两边
*/
func IsDiffWeek(now, old int64, hour, timezone int) bool {
diffHour := IsDiffHour(now, old, hour, timezone)
now += int64(timezone * 3600)
old += int64(timezone * 3600)
// 使用UTC才能在本地时间采用周一作为一周的开始
_, nw := time.Unix(now, 0).UTC().ISOWeek()
_, ow := time.Unix(old, 0).UTC().ISOWeek()
return nw != ow && diffHour
}
/* 今天零点
* timezone 时区
* return 零点时间
*/
func ZeroTime(timezone int) int64 {
timeStr := time.Now().Format("2006-01-02")
t, _ := time.Parse("2006-01-02", timeStr)
return t.Unix() - int64(timezone*3600)
}
/* 年月日
* timestamp 时间
* timezone 时区
* return year,month,day
*/
func YearMonthDay(timestamp int64, timezone int) (int32, int32, int32) {
timestamp += int64(timezone * 3600)
year, month, day := time.Unix(timestamp, 0).UTC().Date()
return int32(year), int32(month), int32(day)
}