-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
time_test.go
445 lines (385 loc) · 11.5 KB
/
time_test.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package utils
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/Laisky/go-utils/v5/log"
)
func TestTimeZone(t *testing.T) {
ts := "2021-10-24T20:00:00+10:00"
tt, err := time.Parse(time.RFC3339, ts)
require.NoError(t, err)
_, offset := tt.Zone()
require.Equal(t, 10*3600, offset)
tt = tt.In(TimeZoneUTC)
_, offset = tt.Zone()
require.Equal(t, 0, offset)
tt = tt.In(TimeZoneShanghai)
_, offset = tt.Zone()
require.Equal(t, 8*3600, offset)
tz, err := time.LoadLocation("Asia/Shanghai")
require.NoError(t, err)
tt = tt.In(tz)
_, offset = tt.Zone()
require.Equal(t, 8*3600, offset)
}
func TestParseTs2String(t *testing.T) {
var (
got string
layout = time.RFC3339
)
cases := map[int64]string{
1: "1970-01-01T00:00:01Z",
100000: "1970-01-02T03:46:40Z",
100000000: "1973-03-03T09:46:40Z",
}
for ts, v := range cases {
if got = ParseTs2String(ts, layout); got != v {
t.Errorf("expect %v, got %v", v, got)
}
}
}
func TestTimeCompare(t *testing.T) {
str1 := "2019-10-12T02:03:14Z"
str2 := "2019-10-12T02:03:14.123Z"
ts1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
ts2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)
// ⚠️ Even though RFC3339 doesn't specifically define milliseconds,
// if the string has a decimal point, the parsing result
// will actually include the millisecond automatically
require.False(t, ts1.Equal(ts2))
require.True(t, ts2.After(ts1))
// truncate to second
require.True(t, ts1.Equal(ts2.Truncate(time.Second)))
// If you want to compare times, you need to manually adjust the precision
// after `Parse` through `Truncate`
t.Run("truncate", func(t *testing.T) {
ts2, err := ParseTimeWithTruncate(time.RFC3339, str2, time.Second)
require.NoError(t, err)
require.True(t, ts1.Equal(ts2))
})
}
func TestParseUnix2UTC(t *testing.T) {
ut := int64(1570845794)
ts := ParseUnix2UTC(ut).Format(time.RFC3339)
if ts != "2019-10-12T02:03:14Z" {
t.Fatalf("got %v", ts)
}
utnano := int64(1570848785196500001)
ts = ParseUnixNano2UTC(utnano).Format(time.RFC3339Nano)
if ts != "2019-10-12T02:53:05.196500001Z" {
t.Fatalf("got %v", ts)
}
}
func TestParseHex2UTC(t *testing.T) {
hex := "5da140b4"
ts, err := ParseHex2UTC(hex)
if err != nil {
t.Fatalf("got error: %+v", err)
}
if ts.Format(time.RFC3339) != "2019-10-12T02:55:48Z" {
t.Fatalf("got %v", ts.Format(time.RFC3339))
}
hexnano := "15ccc6cbb2f54a48"
ts, err = ParseHexNano2UTC(hexnano)
if err != nil {
t.Fatalf("got error: %+v", err)
}
if ts.Format(time.RFC3339Nano) != "2019-10-12T02:55:48.228541Z" {
t.Fatalf("got %v", ts.Format(time.RFC3339Nano))
}
}
// func TestTimeParse(t *testing.T) {
// s := "2018-11-12 03:41:39,735"
// layout := "2006-01-02 15:04:05,000"
// ts, err := time.Parse(layout, s)
// if err != nil {
// t.Fatalf("got error: %+v", err)
// }
// t.Logf("%+v", ts)
// }
// func TestTimeFormat(t *testing.T) {
// ts := time.Now()
// layout := "2006-01-02 15:04:05.000"
// t.Errorf("%+v - %+v", ts, ts.Format(layout))
// time.Sleep(20 * time.Millisecond)
// t.Errorf("%+v - %+v", ts, ts.Format(layout))
// time.Sleep(20 * time.Millisecond)
// t.Errorf("%+v - %+v", ts, ts.Format(layout))
// time.Sleep(20 * time.Millisecond)
// t.Errorf("%+v - %+v", ts, ts.Format(layout))
// time.Sleep(20 * time.Millisecond)
// t.Errorf("%+v - %+v", ts, ts.Format("2006-01-02 15:04:05.999"))
// }
func ExampleClock() {
// use internal clock
// get utc now
Clock.GetUTCNow()
// get time string
Clock.GetTimeInRFC3339Nano()
// change clock refresh step
SetInternalClock(10 * time.Millisecond)
// create new clock
c := NewClock(context.Background(), 1*time.Second)
c.GetUTCNow()
}
func TestClock(t *testing.T) {
ctx := context.Background()
c := NewClock(ctx, 100*time.Millisecond)
ts := c.GetUTCNow()
var err error
t.Logf("ts: %v", ts.Format(time.RFC3339Nano))
c.SetInterval(100 * time.Millisecond)
// test ts
time.Sleep(10 * time.Millisecond) // first refresh
if c.GetUTCNow().After(ts) {
t.Fatalf("should got same ts")
}
if c.GetUTCNow().After(ts) {
t.Fatalf("should got same ts")
}
time.Sleep(50 * time.Millisecond)
if c.GetUTCNow().After(ts) {
t.Fatalf("should got same ts")
}
if c.GetUTCNow().After(ts) {
t.Fatalf("should got same ts")
}
time.Sleep(50 * time.Millisecond)
if c.GetUTCNow().Sub(ts) < 50*time.Millisecond {
t.Fatalf("should not got same ts, got: %+v", c.GetUTCNow().Format(time.RFC3339Nano))
}
// test ts string
timeStr := c.GetTimeInRFC3339Nano()
if c.GetTimeInRFC3339Nano() != timeStr {
t.Fatalf("should got same time string")
}
if c.GetTimeInRFC3339Nano() != timeStr {
t.Fatalf("should got same time string")
}
time.Sleep(50 * time.Millisecond)
if c.GetTimeInRFC3339Nano() != timeStr {
t.Fatalf("should got same time string")
}
if c.GetTimeInRFC3339Nano() != timeStr {
t.Fatalf("should got same time string")
}
time.Sleep(50 * time.Millisecond)
if c.GetTimeInRFC3339Nano() == timeStr {
t.Fatalf("should not got same time string")
}
// test hex
timeStr = c.GetTimeInHex()
if ts, err = ParseHex2UTC(timeStr); err != nil {
t.Fatalf("try to parse timeStr got error: %+v", err)
}
if ts.Format(time.RFC3339) != c.GetUTCNow().Format(time.RFC3339) {
t.Errorf("ts: %v", ts.Format(time.RFC3339))
t.Errorf("c.get: %v", c.GetUTCNow().Format(time.RFC3339))
t.Fatalf("hex time must equal to time")
}
timeStr = c.GetNanoTimeInHex()
if ts, err = ParseHexNano2UTC(timeStr); err != nil {
t.Fatalf("try to parse timeStr got error: %+v", err)
}
if ts.Format(time.RFC3339Nano) != c.GetUTCNow().Format(time.RFC3339Nano) {
t.Errorf("ts: %v", ts.Format(time.RFC3339Nano))
t.Errorf("c.get: %v", c.GetTimeInRFC3339Nano())
t.Fatalf("hex time must equal to time")
}
// test date
{
_, err := c.GetDate()
require.NoError(t, err)
}
// case: close clock
{
ctx2, cancel := context.WithCancel(ctx)
_ = NewClock(ctx2, time.Second)
cancel()
ctx2, cancel = context.WithCancel(ctx)
defer cancel()
c2 := NewClock(ctx2, time.Second)
c2.Close()
}
}
func Benchmark_time(b *testing.B) {
b.Run("normal time", func(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Now()
}
})
b.Run("normal time with UTC", func(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Now().UTC()
}
})
b.Run("parse unix", func(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Unix(1623892878, 0)
}
})
b.Run("parse unix with utc", func(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Unix(1623892878, 0).UTC()
}
})
var n int64 = 1623892878
b.Run("parse unix with utc & load", func(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Unix(atomic.LoadInt64(&n), 0).UTC()
}
})
}
// func TestLoop(t *testing.T) {
// for {
// time.Sleep(1 * time.Millisecond)
// }
// }
/*
goos: linux
goarch: amd64
pkg: github.com/Laisky/go-utils
cpu: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
BenchmarkClock/normal_time-8 26779118 42.67 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_500ms-8 294967054 4.130 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_100ms-8 294066337 4.153 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_10ms-8 295792012 4.044 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_1ms-8 284931848 4.160 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_500us-8 293249996 4.167 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_100us-8 291018960 4.230 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_10us-8 294948268 4.302 ns/op 0 B/op 0 allocs/op
BenchmarkClock/clock2_time_with_10us#01-8 270614050 4.442 ns/op 0 B/op 0 allocs/op
*/
func BenchmarkClock(b *testing.B) {
var err error
if err = log.Shared.ChangeLevel("error"); err != nil {
b.Fatalf("set level: %+v", err)
}
// clock 1
b.Run("normal time", func(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Now().UTC()
}
})
// b.Run("demo", func(b *testing.B) {
// for i := 0; i < b.N; i++ {
// time.Unix(8742374732483, 0).UTC()
// }
// })
// clock 2
clock2 := NewClock(context.Background(), 500*time.Millisecond)
b.Run("clock2 time with 500ms", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
clock2.SetInterval(100 * time.Millisecond)
b.Run("clock2 time with 100ms", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
clock2.SetInterval(10 * time.Millisecond)
b.Run("clock2 time with 10ms", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
clock2.SetInterval(1 * time.Millisecond)
b.Run("clock2 time with 1ms", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
clock2.SetInterval(500 * time.Microsecond)
b.Run("clock2 time with 500us", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
clock2.SetInterval(100 * time.Microsecond)
b.Run("clock2 time with 100us", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
clock2.SetInterval(10 * time.Microsecond)
b.Run("clock2 time with 10us", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
clock2.SetInterval(1 * time.Microsecond)
b.Run("clock2 time with 10us", func(b *testing.B) {
for i := 0; i < b.N; i++ {
clock2.GetUTCNow()
}
})
}
func TestSetupClock(t *testing.T) {
SetInternalClock(100 * time.Millisecond)
// case: invalid interval
{
ok := IsPanic(func() {
SetInternalClock(time.Nanosecond)
})
require.True(t, ok)
}
}
func TestSleepWithContext(t *testing.T) {
t.Run("normal sleep", func(t *testing.T) {
startAt := time.Now()
SleepWithContext(context.Background(), time.Millisecond*10)
require.Greater(t, time.Since(startAt), 10*time.Millisecond)
})
t.Run("sleep break by context", func(t *testing.T) {
startAt := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
defer cancel()
SleepWithContext(ctx, time.Hour)
require.Less(t, time.Since(startAt), time.Second*2)
require.Greater(t, time.Since(startAt), time.Millisecond*10)
})
}
func TestTimeEqual(t *testing.T) {
t.Parallel()
t.Run("eq: no timezone", func(t *testing.T) {
t.Parallel()
str1 := "2019-10-12T02:03:14Z"
str2 := "2019-10-12T02:03:14.123Z"
// RFC3339 = "2006-01-02T15:04:05Z07:00"
t1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
t2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)
require.False(t, t1.Equal(t2))
require.True(t, TimeEqual(t1, t2, time.Second))
})
t.Run("ne: no timezone", func(t *testing.T) {
t.Parallel()
str1 := "2019-10-12T02:03:14Z"
str2 := "2019-10-12T02:03:15.123Z"
t1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
t2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)
require.False(t, t1.Equal(t2))
require.False(t, TimeEqual(t1, t2, time.Second))
})
t.Run("eq: in different timezone", func(t *testing.T) {
t.Parallel()
str1 := "2019-10-12T02:03:14+07:00"
str2 := "2019-10-12T03:03:14.123+08:00"
t1, err := time.Parse(time.RFC3339, str1)
require.NoError(t, err)
t2, err := time.Parse(time.RFC3339, str2)
require.NoError(t, err)
require.False(t, t1.Equal(t2))
require.True(t, TimeEqual(t1, t2, time.Second))
})
}