Source file
src/time/time_test.go
1
2
3
4
5 package time_test
6
7 import (
8 "bytes"
9 "encoding/gob"
10 "encoding/json"
11 "fmt"
12 "internal/goexperiment"
13 "math"
14 "math/big"
15 "math/rand"
16 "os"
17 "runtime"
18 "slices"
19 "strings"
20 "sync"
21 "testing"
22 "testing/quick"
23 . "time"
24 )
25
26 func TestInternal(t *testing.T) {
27 for _, tt := range InternalTests {
28 t.Run(tt.Name, func(t *testing.T) { tt.Test(t) })
29 }
30 }
31
32 func TestZeroTime(t *testing.T) {
33 var zero Time
34 year, month, day := zero.Date()
35 hour, min, sec := zero.Clock()
36 nsec := zero.Nanosecond()
37 yday := zero.YearDay()
38 wday := zero.Weekday()
39 if year != 1 || month != January || day != 1 || hour != 0 || min != 0 || sec != 0 || nsec != 0 || yday != 1 || wday != Monday {
40 t.Errorf("zero time = %v %v %v year %v %02d:%02d:%02d.%09d yday %d want Monday Jan 1 year 1 00:00:00.000000000 yday 1",
41 wday, month, day, year, hour, min, sec, nsec, yday)
42 }
43
44 }
45
46
47
48
49 func TestZoneData(t *testing.T) {
50 lt := Now()
51
52 if name, off := lt.Zone(); off != -8*60*60 && off != -7*60*60 {
53 t.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", name, off)
54 t.Error("Likely problem: the time zone files have not been installed.")
55 }
56 }
57
58
59 type parsedTime struct {
60 Year int
61 Month Month
62 Day int
63 Hour, Minute, Second int
64 Nanosecond int
65 Weekday Weekday
66 ZoneOffset int
67 Zone string
68 }
69
70 type TimeTest struct {
71 seconds int64
72 golden parsedTime
73 }
74
75 var utctests = []TimeTest{
76 {0, parsedTime{1970, January, 1, 0, 0, 0, 0, Thursday, 0, "UTC"}},
77 {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 0, Wednesday, 0, "UTC"}},
78 {-1221681866, parsedTime{1931, April, 16, 3, 55, 34, 0, Thursday, 0, "UTC"}},
79 {-11644473600, parsedTime{1601, January, 1, 0, 0, 0, 0, Monday, 0, "UTC"}},
80 {599529660, parsedTime{1988, December, 31, 0, 1, 0, 0, Saturday, 0, "UTC"}},
81 {978220860, parsedTime{2000, December, 31, 0, 1, 0, 0, Sunday, 0, "UTC"}},
82 }
83
84 var nanoutctests = []TimeTest{
85 {0, parsedTime{1970, January, 1, 0, 0, 0, 1e8, Thursday, 0, "UTC"}},
86 {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 2e8, Wednesday, 0, "UTC"}},
87 }
88
89 var localtests = []TimeTest{
90 {0, parsedTime{1969, December, 31, 16, 0, 0, 0, Wednesday, -8 * 60 * 60, "PST"}},
91 {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 0, Wednesday, -7 * 60 * 60, "PDT"}},
92 {2159200800, parsedTime{2038, June, 3, 11, 0, 0, 0, Thursday, -7 * 60 * 60, "PDT"}},
93 {2152173599, parsedTime{2038, March, 14, 1, 59, 59, 0, Sunday, -8 * 60 * 60, "PST"}},
94 {2152173600, parsedTime{2038, March, 14, 3, 0, 0, 0, Sunday, -7 * 60 * 60, "PDT"}},
95 {2152173601, parsedTime{2038, March, 14, 3, 0, 1, 0, Sunday, -7 * 60 * 60, "PDT"}},
96 {2172733199, parsedTime{2038, November, 7, 1, 59, 59, 0, Sunday, -7 * 60 * 60, "PDT"}},
97 {2172733200, parsedTime{2038, November, 7, 1, 0, 0, 0, Sunday, -8 * 60 * 60, "PST"}},
98 {2172733201, parsedTime{2038, November, 7, 1, 0, 1, 0, Sunday, -8 * 60 * 60, "PST"}},
99 }
100
101 var nanolocaltests = []TimeTest{
102 {0, parsedTime{1969, December, 31, 16, 0, 0, 1e8, Wednesday, -8 * 60 * 60, "PST"}},
103 {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 3e8, Wednesday, -7 * 60 * 60, "PDT"}},
104 }
105
106 func same(t Time, u *parsedTime) bool {
107
108 year, month, day := t.Date()
109 hour, min, sec := t.Clock()
110 name, offset := t.Zone()
111 if year != u.Year || month != u.Month || day != u.Day ||
112 hour != u.Hour || min != u.Minute || sec != u.Second ||
113 name != u.Zone || offset != u.ZoneOffset {
114 return false
115 }
116
117 return t.Year() == u.Year &&
118 t.Month() == u.Month &&
119 t.Day() == u.Day &&
120 t.Hour() == u.Hour &&
121 t.Minute() == u.Minute &&
122 t.Second() == u.Second &&
123 t.Nanosecond() == u.Nanosecond &&
124 t.Weekday() == u.Weekday
125 }
126
127 func TestUnixUTC(t *testing.T) {
128 for _, test := range utctests {
129 sec := test.seconds
130 golden := &test.golden
131 tm := Unix(sec, 0).UTC()
132 newsec := tm.Unix()
133 if newsec != sec {
134 t.Errorf("Unix(%d, 0).Unix() = %d", sec, newsec)
135 }
136 if !same(tm, golden) {
137 t.Errorf("Unix(%d, 0): // %#v", sec, tm)
138 t.Errorf(" want=%+v", *golden)
139 t.Errorf(" have=%v", tm.Format(RFC3339+" MST"))
140 }
141 }
142 }
143
144 func TestUnixNanoUTC(t *testing.T) {
145 for _, test := range nanoutctests {
146 golden := &test.golden
147 nsec := test.seconds*1e9 + int64(golden.Nanosecond)
148 tm := Unix(0, nsec).UTC()
149 newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
150 if newnsec != nsec {
151 t.Errorf("Unix(0, %d).Nanoseconds() = %d", nsec, newnsec)
152 }
153 if !same(tm, golden) {
154 t.Errorf("Unix(0, %d):", nsec)
155 t.Errorf(" want=%+v", *golden)
156 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
157 }
158 }
159 }
160
161 func TestUnix(t *testing.T) {
162 for _, test := range localtests {
163 sec := test.seconds
164 golden := &test.golden
165 tm := Unix(sec, 0)
166 newsec := tm.Unix()
167 if newsec != sec {
168 t.Errorf("Unix(%d, 0).Seconds() = %d", sec, newsec)
169 }
170 if !same(tm, golden) {
171 t.Errorf("Unix(%d, 0):", sec)
172 t.Errorf(" want=%+v", *golden)
173 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
174 }
175 }
176 }
177
178 func TestUnixNano(t *testing.T) {
179 for _, test := range nanolocaltests {
180 golden := &test.golden
181 nsec := test.seconds*1e9 + int64(golden.Nanosecond)
182 tm := Unix(0, nsec)
183 newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
184 if newnsec != nsec {
185 t.Errorf("Unix(0, %d).Seconds() = %d", nsec, newnsec)
186 }
187 if !same(tm, golden) {
188 t.Errorf("Unix(0, %d):", nsec)
189 t.Errorf(" want=%+v", *golden)
190 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
191 }
192 }
193 }
194
195 func TestUnixUTCAndBack(t *testing.T) {
196 f := func(sec int64) bool { return Unix(sec, 0).UTC().Unix() == sec }
197 f32 := func(sec int32) bool { return f(int64(sec)) }
198 cfg := &quick.Config{MaxCount: 10000}
199
200
201 if err := quick.Check(f32, cfg); err != nil {
202 t.Fatal(err)
203 }
204 if err := quick.Check(f, cfg); err != nil {
205 t.Fatal(err)
206 }
207 }
208
209 func TestUnixNanoUTCAndBack(t *testing.T) {
210 f := func(nsec int64) bool {
211 t := Unix(0, nsec).UTC()
212 ns := t.Unix()*1e9 + int64(t.Nanosecond())
213 return ns == nsec
214 }
215 f32 := func(nsec int32) bool { return f(int64(nsec)) }
216 cfg := &quick.Config{MaxCount: 10000}
217
218
219
220 if err := quick.Check(f32, cfg); err != nil {
221 t.Fatal(err)
222 }
223 if err := quick.Check(f, cfg); err != nil {
224 t.Fatal(err)
225 }
226 }
227
228 func TestUnixMilli(t *testing.T) {
229 f := func(msec int64) bool {
230 t := UnixMilli(msec)
231 return t.UnixMilli() == msec
232 }
233 cfg := &quick.Config{MaxCount: 10000}
234 if err := quick.Check(f, cfg); err != nil {
235 t.Fatal(err)
236 }
237 }
238
239 func TestUnixMicro(t *testing.T) {
240 f := func(usec int64) bool {
241 t := UnixMicro(usec)
242 return t.UnixMicro() == usec
243 }
244 cfg := &quick.Config{MaxCount: 10000}
245 if err := quick.Check(f, cfg); err != nil {
246 t.Fatal(err)
247 }
248 }
249
250
251
252
253
254
255
256
257 const unixToZero = -978307200 + 63113904000
258
259
260 func abs(t Time) (sec, nsec int64) {
261 unix := t.Unix()
262 nano := t.Nanosecond()
263 return unix + unixToZero, int64(nano)
264 }
265
266
267 func absString(t Time) string {
268 sec, nsec := abs(t)
269 if sec < 0 {
270 sec = -sec
271 nsec = -nsec
272 if nsec < 0 {
273 nsec += 1e9
274 sec--
275 }
276 return fmt.Sprintf("-%d%09d", sec, nsec)
277 }
278 return fmt.Sprintf("%d%09d", sec, nsec)
279 }
280
281 var truncateRoundTests = []struct {
282 t Time
283 d Duration
284 }{
285 {Date(-1, January, 1, 12, 15, 30, 5e8, UTC), 3},
286 {Date(-1, January, 1, 12, 15, 31, 5e8, UTC), 3},
287 {Date(2012, January, 1, 12, 15, 30, 5e8, UTC), Second},
288 {Date(2012, January, 1, 12, 15, 31, 5e8, UTC), Second},
289 {Unix(-19012425939, 649146258), 7435029458905025217},
290 }
291
292 func TestTruncateRound(t *testing.T) {
293 var (
294 bsec = new(big.Int)
295 bnsec = new(big.Int)
296 bd = new(big.Int)
297 bt = new(big.Int)
298 br = new(big.Int)
299 bq = new(big.Int)
300 b1e9 = new(big.Int)
301 )
302
303 b1e9.SetInt64(1e9)
304
305 testOne := func(ti, tns, di int64) bool {
306 t.Helper()
307
308 t0 := Unix(ti, tns).UTC()
309 d := Duration(di)
310 if d < 0 {
311 d = -d
312 }
313 if d <= 0 {
314 d = 1
315 }
316
317
318 sec, nsec := abs(t0)
319 bsec.SetInt64(sec)
320 bnsec.SetInt64(nsec)
321 bt.Mul(bsec, b1e9)
322 bt.Add(bt, bnsec)
323
324
325 bd.SetInt64(int64(d))
326 bq.DivMod(bt, bd, br)
327
328
329
330 r := br.Int64()
331 t1 := t0.Add(-Duration(r))
332
333
334 if trunc := t0.Truncate(d); trunc != t1 {
335 t.Errorf("Time.Truncate(%s, %s) = %s, want %s\n"+
336 "%v trunc %v =\n%v want\n%v",
337 t0.Format(RFC3339Nano), d, trunc, t1.Format(RFC3339Nano),
338 absString(t0), int64(d), absString(trunc), absString(t1))
339 return false
340 }
341
342
343
344
345 if r > int64(d)/2 || r+r == int64(d) {
346 t1 = t1.Add(d)
347 }
348
349
350 if rnd := t0.Round(d); rnd != t1 {
351 t.Errorf("Time.Round(%s, %s) = %s, want %s\n"+
352 "%v round %v =\n%v want\n%v",
353 t0.Format(RFC3339Nano), d, rnd, t1.Format(RFC3339Nano),
354 absString(t0), int64(d), absString(rnd), absString(t1))
355 return false
356 }
357 return true
358 }
359
360
361 for _, tt := range truncateRoundTests {
362 testOne(tt.t.Unix(), int64(tt.t.Nanosecond()), int64(tt.d))
363 }
364
365
366 for i := 0; i < 100; i++ {
367 for j := 1; j < 100; j++ {
368 testOne(unixToZero, int64(i), int64(j))
369 testOne(unixToZero, -int64(i), int64(j))
370 if t.Failed() {
371 return
372 }
373 }
374 }
375
376 if t.Failed() {
377 return
378 }
379
380
381 cfg := &quick.Config{MaxCount: 100000}
382 if testing.Short() {
383 cfg.MaxCount = 1000
384 }
385
386
387 f1 := func(ti int64, tns int32, logdi int32) bool {
388 d := Duration(1)
389 a, b := uint(logdi%9), (logdi>>16)%9
390 d <<= a
391 for i := 0; i < int(b); i++ {
392 d *= 5
393 }
394
395
396
397
398
399 ti >>= 1
400
401 return testOne(ti, int64(tns), int64(d))
402 }
403 quick.Check(f1, cfg)
404
405
406 f2 := func(ti int64, tns int32, di int32) bool {
407 d := Duration(di) * Second
408 if d < 0 {
409 d = -d
410 }
411 ti >>= 1
412 return testOne(ti, int64(tns), int64(d))
413 }
414 quick.Check(f2, cfg)
415
416
417 f3 := func(tns, di int64) bool {
418 di &= 0xfffffffe
419 if di == 0 {
420 di = 2
421 }
422 tns -= tns % di
423 if tns < 0 {
424 tns += di / 2
425 } else {
426 tns -= di / 2
427 }
428 return testOne(0, tns, di)
429 }
430 quick.Check(f3, cfg)
431
432
433 f4 := func(ti int64, tns int32, di int64) bool {
434 ti >>= 1
435 return testOne(ti, int64(tns), di)
436 }
437 quick.Check(f4, cfg)
438 }
439
440 type ISOWeekTest struct {
441 year int
442 month, day int
443 yex int
444 wex int
445 }
446
447 var isoWeekTests = []ISOWeekTest{
448 {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52},
449 {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1},
450 {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52},
451 {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1},
452 {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1},
453 {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2},
454 {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53},
455 {2000, 1, 1, 1999, 52}, {2001, 1, 1, 2001, 1}, {2002, 1, 1, 2002, 1},
456 {2003, 1, 1, 2003, 1}, {2004, 1, 1, 2004, 1}, {2005, 1, 1, 2004, 53},
457 {2006, 1, 1, 2005, 52}, {2007, 1, 1, 2007, 1}, {2008, 1, 1, 2008, 1},
458 {2009, 1, 1, 2009, 1}, {2010, 1, 1, 2009, 53}, {2010, 1, 1, 2009, 53},
459 {2011, 1, 1, 2010, 52}, {2011, 1, 2, 2010, 52}, {2011, 1, 3, 2011, 1},
460 {2011, 1, 4, 2011, 1}, {2011, 1, 5, 2011, 1}, {2011, 1, 6, 2011, 1},
461 {2011, 1, 7, 2011, 1}, {2011, 1, 8, 2011, 1}, {2011, 1, 9, 2011, 1},
462 {2011, 1, 10, 2011, 2}, {2011, 1, 11, 2011, 2}, {2011, 6, 12, 2011, 23},
463 {2011, 6, 13, 2011, 24}, {2011, 12, 25, 2011, 51}, {2011, 12, 26, 2011, 52},
464 {2011, 12, 27, 2011, 52}, {2011, 12, 28, 2011, 52}, {2011, 12, 29, 2011, 52},
465 {2011, 12, 30, 2011, 52}, {2011, 12, 31, 2011, 52}, {1995, 1, 1, 1994, 52},
466 {2012, 1, 1, 2011, 52}, {2012, 1, 2, 2012, 1}, {2012, 1, 8, 2012, 1},
467 {2012, 1, 9, 2012, 2}, {2012, 12, 23, 2012, 51}, {2012, 12, 24, 2012, 52},
468 {2012, 12, 30, 2012, 52}, {2012, 12, 31, 2013, 1}, {2013, 1, 1, 2013, 1},
469 {2013, 1, 6, 2013, 1}, {2013, 1, 7, 2013, 2}, {2013, 12, 22, 2013, 51},
470 {2013, 12, 23, 2013, 52}, {2013, 12, 29, 2013, 52}, {2013, 12, 30, 2014, 1},
471 {2014, 1, 1, 2014, 1}, {2014, 1, 5, 2014, 1}, {2014, 1, 6, 2014, 2},
472 {2015, 1, 1, 2015, 1}, {2016, 1, 1, 2015, 53}, {2017, 1, 1, 2016, 52},
473 {2018, 1, 1, 2018, 1}, {2019, 1, 1, 2019, 1}, {2020, 1, 1, 2020, 1},
474 {2021, 1, 1, 2020, 53}, {2022, 1, 1, 2021, 52}, {2023, 1, 1, 2022, 52},
475 {2024, 1, 1, 2024, 1}, {2025, 1, 1, 2025, 1}, {2026, 1, 1, 2026, 1},
476 {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1},
477 {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1},
478 {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1},
479 {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53},
480 {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52},
481 }
482
483 func TestISOWeek(t *testing.T) {
484
485 for _, wt := range isoWeekTests {
486 dt := Date(wt.year, Month(wt.month), wt.day, 0, 0, 0, 0, UTC)
487 y, w := dt.ISOWeek()
488 if w != wt.wex || y != wt.yex {
489 t.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d",
490 y, w, wt.yex, wt.wex, wt.year, wt.month, wt.day)
491 }
492 }
493
494
495 for year := 1950; year < 2100; year++ {
496 if y, w := Date(year, January, 4, 0, 0, 0, 0, UTC).ISOWeek(); y != year || w != 1 {
497 t.Errorf("got %d/%d; expected %d/1 for Jan 04", y, w, year)
498 }
499 }
500 }
501
502 type YearDayTest struct {
503 year, month, day int
504 yday int
505 }
506
507
508
509 var yearDayTests = []YearDayTest{
510
511 {2007, 1, 1, 1},
512 {2007, 1, 15, 15},
513 {2007, 2, 1, 32},
514 {2007, 2, 15, 46},
515 {2007, 3, 1, 60},
516 {2007, 3, 15, 74},
517 {2007, 4, 1, 91},
518 {2007, 12, 31, 365},
519
520
521 {2008, 1, 1, 1},
522 {2008, 1, 15, 15},
523 {2008, 2, 1, 32},
524 {2008, 2, 15, 46},
525 {2008, 3, 1, 61},
526 {2008, 3, 15, 75},
527 {2008, 4, 1, 92},
528 {2008, 12, 31, 366},
529
530
531 {1900, 1, 1, 1},
532 {1900, 1, 15, 15},
533 {1900, 2, 1, 32},
534 {1900, 2, 15, 46},
535 {1900, 3, 1, 60},
536 {1900, 3, 15, 74},
537 {1900, 4, 1, 91},
538 {1900, 12, 31, 365},
539
540
541 {1, 1, 1, 1},
542 {1, 1, 15, 15},
543 {1, 2, 1, 32},
544 {1, 2, 15, 46},
545 {1, 3, 1, 60},
546 {1, 3, 15, 74},
547 {1, 4, 1, 91},
548 {1, 12, 31, 365},
549
550
551 {-1, 1, 1, 1},
552 {-1, 1, 15, 15},
553 {-1, 2, 1, 32},
554 {-1, 2, 15, 46},
555 {-1, 3, 1, 60},
556 {-1, 3, 15, 74},
557 {-1, 4, 1, 91},
558 {-1, 12, 31, 365},
559
560
561 {-400, 1, 1, 1},
562 {-400, 1, 15, 15},
563 {-400, 2, 1, 32},
564 {-400, 2, 15, 46},
565 {-400, 3, 1, 61},
566 {-400, 3, 15, 75},
567 {-400, 4, 1, 92},
568 {-400, 12, 31, 366},
569
570
571
572
573 {1582, 10, 4, 277},
574 {1582, 10, 15, 288},
575 }
576
577
578 var yearDayLocations = []*Location{
579 FixedZone("UTC-8", -8*60*60),
580 FixedZone("UTC-4", -4*60*60),
581 UTC,
582 FixedZone("UTC+4", 4*60*60),
583 FixedZone("UTC+8", 8*60*60),
584 }
585
586 func TestYearDay(t *testing.T) {
587 for i, loc := range yearDayLocations {
588 for _, ydt := range yearDayTests {
589 dt := Date(ydt.year, Month(ydt.month), ydt.day, 0, 0, 0, 0, loc)
590 yday := dt.YearDay()
591 if yday != ydt.yday {
592 t.Errorf("Date(%d-%02d-%02d in %v).YearDay() = %d, want %d",
593 ydt.year, ydt.month, ydt.day, loc, yday, ydt.yday)
594 continue
595 }
596
597 if ydt.year < 0 || ydt.year > 9999 {
598 continue
599 }
600 f := fmt.Sprintf("%04d-%02d-%02d %03d %+.2d00",
601 ydt.year, ydt.month, ydt.day, ydt.yday, (i-2)*4)
602 dt1, err := Parse("2006-01-02 002 -0700", f)
603 if err != nil {
604 t.Errorf(`Parse("2006-01-02 002 -0700", %q): %v`, f, err)
605 continue
606 }
607 if !dt1.Equal(dt) {
608 t.Errorf(`Parse("2006-01-02 002 -0700", %q) = %v, want %v`, f, dt1, dt)
609 }
610 }
611 }
612 }
613
614 var durationTests = []struct {
615 str string
616 d Duration
617 }{
618 {"0s", 0},
619 {"1ns", 1 * Nanosecond},
620 {"1.1µs", 1100 * Nanosecond},
621 {"2.2ms", 2200 * Microsecond},
622 {"3.3s", 3300 * Millisecond},
623 {"4m5s", 4*Minute + 5*Second},
624 {"4m5.001s", 4*Minute + 5001*Millisecond},
625 {"5h6m7.001s", 5*Hour + 6*Minute + 7001*Millisecond},
626 {"8m0.000000001s", 8*Minute + 1*Nanosecond},
627 {"2562047h47m16.854775807s", 1<<63 - 1},
628 {"-2562047h47m16.854775808s", -1 << 63},
629 }
630
631 func TestDurationString(t *testing.T) {
632 for _, tt := range durationTests {
633 if str := tt.d.String(); str != tt.str {
634 t.Errorf("Duration(%d).String() = %s, want %s", int64(tt.d), str, tt.str)
635 }
636 if tt.d > 0 {
637 if str := (-tt.d).String(); str != "-"+tt.str {
638 t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str)
639 }
640 }
641 }
642 }
643
644 var dateTests = []struct {
645 year, month, day, hour, min, sec, nsec int
646 z *Location
647 unix int64
648 }{
649 {2011, 11, 6, 1, 0, 0, 0, Local, 1320566400},
650 {2011, 11, 6, 1, 59, 59, 0, Local, 1320569999},
651 {2011, 11, 6, 2, 0, 0, 0, Local, 1320573600},
652
653 {2011, 3, 13, 1, 0, 0, 0, Local, 1300006800},
654 {2011, 3, 13, 1, 59, 59, 0, Local, 1300010399},
655 {2011, 3, 13, 3, 0, 0, 0, Local, 1300010400},
656 {2011, 3, 13, 2, 30, 0, 0, Local, 1300008600},
657 {2012, 12, 24, 0, 0, 0, 0, Local, 1356336000},
658
659
660 {2011, 11, 18, 7, 56, 35, 0, Local, 1321631795},
661 {2011, 11, 19, -17, 56, 35, 0, Local, 1321631795},
662 {2011, 11, 17, 31, 56, 35, 0, Local, 1321631795},
663 {2011, 11, 18, 6, 116, 35, 0, Local, 1321631795},
664 {2011, 10, 49, 7, 56, 35, 0, Local, 1321631795},
665 {2011, 11, 18, 7, 55, 95, 0, Local, 1321631795},
666 {2011, 11, 18, 7, 56, 34, 1e9, Local, 1321631795},
667 {2011, 12, -12, 7, 56, 35, 0, Local, 1321631795},
668 {2012, 1, -43, 7, 56, 35, 0, Local, 1321631795},
669 {2012, int(January - 2), 18, 7, 56, 35, 0, Local, 1321631795},
670 {2010, int(December + 11), 18, 7, 56, 35, 0, Local, 1321631795},
671 {1970, 1, 15297, 7, 56, 35, 0, Local, 1321631795},
672
673 {1970, 1, -25508, 0, 0, 0, 0, Local, -2203948800},
674 }
675
676 func TestDate(t *testing.T) {
677 for _, tt := range dateTests {
678 time := Date(tt.year, Month(tt.month), tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z)
679 want := Unix(tt.unix, 0)
680 if !time.Equal(want) {
681 t.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v",
682 tt.year, tt.month, tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z,
683 time, want)
684 }
685 }
686 }
687
688
689
690
691
692 var addDateTests = []struct {
693 years, months, days int
694 }{
695 {4, 4, 1},
696 {3, 16, 1},
697 {3, 15, 30},
698 {5, -6, -18 - 30 - 12},
699 }
700
701 func TestAddDate(t *testing.T) {
702 t0 := Date(2011, 11, 18, 7, 56, 35, 0, UTC)
703 t1 := Date(2016, 3, 19, 7, 56, 35, 0, UTC)
704 for _, at := range addDateTests {
705 time := t0.AddDate(at.years, at.months, at.days)
706 if !time.Equal(t1) {
707 t.Errorf("AddDate(%d, %d, %d) = %v, want %v",
708 at.years, at.months, at.days,
709 time, t1)
710 }
711 }
712
713 t2 := Date(1899, 12, 31, 0, 0, 0, 0, UTC)
714 days := t2.Unix() / (24 * 60 * 60)
715 t3 := Unix(0, 0).AddDate(0, 0, int(days))
716 if !t2.Equal(t3) {
717 t.Errorf("Adddate(0, 0, %d) = %v, want %v", days, t3, t2)
718 }
719 }
720
721 var daysInTests = []struct {
722 year, month, di int
723 }{
724 {2011, 1, 31},
725 {2011, 2, 28},
726 {2012, 2, 29},
727 {2011, 6, 30},
728 {2011, 12, 31},
729 }
730
731 func TestDaysIn(t *testing.T) {
732
733
734
735 for _, tt := range daysInTests {
736 di := DaysIn(Month(tt.month), tt.year)
737 if di != tt.di {
738 t.Errorf("got %d; expected %d for %d-%02d",
739 di, tt.di, tt.year, tt.month)
740 }
741 }
742 }
743
744 func TestAddToExactSecond(t *testing.T) {
745
746
747 t1 := Now()
748 t2 := t1.Add(Second - Duration(t1.Nanosecond()))
749 sec := (t1.Second() + 1) % 60
750 if t2.Second() != sec || t2.Nanosecond() != 0 {
751 t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec)
752 }
753 }
754
755 func equalTimeAndZone(a, b Time) bool {
756 aname, aoffset := a.Zone()
757 bname, boffset := b.Zone()
758 return a.Equal(b) && aoffset == boffset && aname == bname
759 }
760
761 var gobTests = []Time{
762 Date(0, 1, 2, 3, 4, 5, 6, UTC),
763 Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
764 Unix(81985467080890095, 0x76543210),
765 {},
766 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
767 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
768 }
769
770 func TestTimeGob(t *testing.T) {
771 var b bytes.Buffer
772 enc := gob.NewEncoder(&b)
773 dec := gob.NewDecoder(&b)
774 for _, tt := range gobTests {
775 var gobtt Time
776 if err := enc.Encode(&tt); err != nil {
777 t.Errorf("%v gob Encode error = %q, want nil", tt, err)
778 } else if err := dec.Decode(&gobtt); err != nil {
779 t.Errorf("%v gob Decode error = %q, want nil", tt, err)
780 } else if !equalTimeAndZone(gobtt, tt) {
781 t.Errorf("Decoded time = %v, want %v", gobtt, tt)
782 }
783 b.Reset()
784 }
785 }
786
787 var invalidEncodingTests = []struct {
788 bytes []byte
789 want string
790 }{
791 {[]byte{}, "Time.UnmarshalBinary: no data"},
792 {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"},
793 {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"},
794 }
795
796 func TestInvalidTimeGob(t *testing.T) {
797 for _, tt := range invalidEncodingTests {
798 var ignored Time
799 err := ignored.GobDecode(tt.bytes)
800 if err == nil || err.Error() != tt.want {
801 t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.bytes, err, tt.want)
802 }
803 err = ignored.UnmarshalBinary(tt.bytes)
804 if err == nil || err.Error() != tt.want {
805 t.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v", tt.bytes, err, tt.want)
806 }
807 }
808 }
809
810 var notEncodableTimes = []struct {
811 time Time
812 want string
813 }{
814 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: unexpected zone offset"},
815 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinary: unexpected zone offset"},
816 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary: unexpected zone offset"},
817 }
818
819 func TestNotGobEncodableTime(t *testing.T) {
820 for _, tt := range notEncodableTimes {
821 _, err := tt.time.GobEncode()
822 if err == nil || err.Error() != tt.want {
823 t.Errorf("%v GobEncode error = %v, want %v", tt.time, err, tt.want)
824 }
825 _, err = tt.time.MarshalBinary()
826 if err == nil || err.Error() != tt.want {
827 t.Errorf("%v MarshalBinary error = %v, want %v", tt.time, err, tt.want)
828 }
829 }
830 }
831
832 var jsonTests = []struct {
833 time Time
834 json string
835 }{
836 {Date(9999, 4, 12, 23, 20, 50, 520*1e6, UTC), `"9999-04-12T23:20:50.52Z"`},
837 {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`},
838 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`},
839 {Date(2020, 1, 1, 0, 0, 0, 0, FixedZone("", 23*60*60+59*60)), `"2020-01-01T00:00:00+23:59"`},
840 }
841
842 func TestTimeJSON(t *testing.T) {
843 for _, tt := range jsonTests {
844 var jsonTime Time
845
846 if jsonBytes, err := json.Marshal(tt.time); err != nil {
847 t.Errorf("%v json.Marshal error = %v, want nil", tt.time, err)
848 } else if string(jsonBytes) != tt.json {
849 t.Errorf("%v JSON = %#q, want %#q", tt.time, string(jsonBytes), tt.json)
850 } else if err = json.Unmarshal(jsonBytes, &jsonTime); err != nil {
851 t.Errorf("%v json.Unmarshal error = %v, want nil", tt.time, err)
852 } else if !equalTimeAndZone(jsonTime, tt.time) {
853 t.Errorf("Unmarshaled time = %v, want %v", jsonTime, tt.time)
854 }
855 }
856 }
857
858 func TestUnmarshalInvalidTimes(t *testing.T) {
859 tests := []struct {
860 in string
861 want string
862 }{
863 {`{}`, func() string {
864 if goexperiment.JSONv2 {
865 return "json: cannot unmarshal JSON object into Go type time.Time"
866 } else {
867 return "Time.UnmarshalJSON: input is not a JSON string"
868 }
869 }()},
870 {`[]`, func() string {
871 if goexperiment.JSONv2 {
872 return "json: cannot unmarshal JSON array into Go type time.Time"
873 } else {
874 return "Time.UnmarshalJSON: input is not a JSON string"
875 }
876 }()},
877 {`"2000-01-01T1:12:34Z"`, `<nil>`},
878 {`"2000-01-01T00:00:00,000Z"`, `<nil>`},
879 {`"2000-01-01T00:00:00+24:00"`, `<nil>`},
880 {`"2000-01-01T00:00:00+00:60"`, `<nil>`},
881 {`"2000-01-01T00:00:00+123:45"`, `parsing time "2000-01-01T00:00:00+123:45" as "2006-01-02T15:04:05Z07:00": cannot parse "+123:45" as "Z07:00"`},
882 }
883
884 for _, tt := range tests {
885 var ts Time
886
887 want := tt.want
888 err := json.Unmarshal([]byte(tt.in), &ts)
889 if fmt.Sprint(err) != want {
890 t.Errorf("Time.UnmarshalJSON(%s) = %v, want %v", tt.in, err, want)
891 }
892
893 if strings.HasPrefix(tt.in, `"`) && strings.HasSuffix(tt.in, `"`) {
894 err = ts.UnmarshalText([]byte(strings.Trim(tt.in, `"`)))
895 if fmt.Sprint(err) != want {
896 t.Errorf("Time.UnmarshalText(%s) = %v, want %v", tt.in, err, want)
897 }
898 }
899 }
900 }
901
902 func TestMarshalInvalidTimes(t *testing.T) {
903 tests := []struct {
904 time Time
905 want string
906 }{
907 {Date(10000, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
908 {Date(-998, 1, 1, 0, 0, 0, 0, UTC).Add(-Second), "Time.MarshalJSON: year outside of range [0,9999]"},
909 {Date(0, 1, 1, 0, 0, 0, 0, UTC).Add(-Nanosecond), "Time.MarshalJSON: year outside of range [0,9999]"},
910 {Date(2020, 1, 1, 0, 0, 0, 0, FixedZone("", 24*60*60)), "Time.MarshalJSON: timezone hour outside of range [0,23]"},
911 {Date(2020, 1, 1, 0, 0, 0, 0, FixedZone("", 123*60*60)), "Time.MarshalJSON: timezone hour outside of range [0,23]"},
912 }
913
914 for _, tt := range tests {
915 want := tt.want
916 b, err := tt.time.MarshalJSON()
917 switch {
918 case b != nil:
919 t.Errorf("(%v).MarshalText() = %q, want nil", tt.time, b)
920 case err == nil || err.Error() != want:
921 t.Errorf("(%v).MarshalJSON() error = %v, want %v", tt.time, err, want)
922 }
923
924 want = strings.ReplaceAll(tt.want, "JSON", "Text")
925 b, err = tt.time.MarshalText()
926 switch {
927 case b != nil:
928 t.Errorf("(%v).MarshalText() = %q, want nil", tt.time, b)
929 case err == nil || err.Error() != want:
930 t.Errorf("(%v).MarshalText() error = %v, want %v", tt.time, err, want)
931 }
932
933 buf := make([]byte, 0, 64)
934 want = strings.ReplaceAll(tt.want, "MarshalJSON", "AppendText")
935 b, err = tt.time.AppendText(buf)
936 switch {
937 case b != nil:
938 t.Errorf("(%v).AppendText() = %q, want nil", tt.time, b)
939 case err == nil || err.Error() != want:
940 t.Errorf("(%v).AppendText() error = %v, want %v", tt.time, err, want)
941 }
942 }
943 }
944
945 var parseDurationTests = []struct {
946 in string
947 want Duration
948 }{
949
950 {"0", 0},
951 {"5s", 5 * Second},
952 {"30s", 30 * Second},
953 {"1478s", 1478 * Second},
954
955 {"-5s", -5 * Second},
956 {"+5s", 5 * Second},
957 {"-0", 0},
958 {"+0", 0},
959
960 {"5.0s", 5 * Second},
961 {"5.6s", 5*Second + 600*Millisecond},
962 {"5.s", 5 * Second},
963 {".5s", 500 * Millisecond},
964 {"1.0s", 1 * Second},
965 {"1.00s", 1 * Second},
966 {"1.004s", 1*Second + 4*Millisecond},
967 {"1.0040s", 1*Second + 4*Millisecond},
968 {"100.00100s", 100*Second + 1*Millisecond},
969
970 {"10ns", 10 * Nanosecond},
971 {"11us", 11 * Microsecond},
972 {"12µs", 12 * Microsecond},
973 {"12μs", 12 * Microsecond},
974 {"13ms", 13 * Millisecond},
975 {"14s", 14 * Second},
976 {"15m", 15 * Minute},
977 {"16h", 16 * Hour},
978
979 {"3h30m", 3*Hour + 30*Minute},
980 {"10.5s4m", 4*Minute + 10*Second + 500*Millisecond},
981 {"-2m3.4s", -(2*Minute + 3*Second + 400*Millisecond)},
982 {"1h2m3s4ms5us6ns", 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
983 {"39h9m14.425s", 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
984
985 {"52763797000ns", 52763797000 * Nanosecond},
986
987 {"0.3333333333333333333h", 20 * Minute},
988
989 {"9007199254740993ns", (1<<53 + 1) * Nanosecond},
990
991 {"9223372036854775807ns", (1<<63 - 1) * Nanosecond},
992 {"9223372036854775.807us", (1<<63 - 1) * Nanosecond},
993 {"9223372036s854ms775us807ns", (1<<63 - 1) * Nanosecond},
994 {"-9223372036854775808ns", -1 << 63 * Nanosecond},
995 {"-9223372036854775.808us", -1 << 63 * Nanosecond},
996 {"-9223372036s854ms775us808ns", -1 << 63 * Nanosecond},
997
998 {"-9223372036854775808ns", -1 << 63 * Nanosecond},
999
1000 {"-2562047h47m16.854775808s", -1 << 63 * Nanosecond},
1001
1002 {"0.100000000000000000000h", 6 * Minute},
1003
1004 {"0.830103483285477580700h", 49*Minute + 48*Second + 372539827*Nanosecond},
1005 }
1006
1007 func TestParseDuration(t *testing.T) {
1008 for _, tc := range parseDurationTests {
1009 d, err := ParseDuration(tc.in)
1010 if err != nil || d != tc.want {
1011 t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want)
1012 }
1013 }
1014 }
1015
1016 var parseDurationErrorTests = []struct {
1017 in string
1018 expect string
1019 }{
1020
1021 {"", `""`},
1022 {"3", `"3"`},
1023 {"-", `"-"`},
1024 {"s", `"s"`},
1025 {".", `"."`},
1026 {"-.", `"-."`},
1027 {".s", `".s"`},
1028 {"+.s", `"+.s"`},
1029 {"1d", `"1d"`},
1030 {"\x85\x85", `"\x85\x85"`},
1031 {"\xffff", `"\xffff"`},
1032 {"hello \xffff world", `"hello \xffff world"`},
1033 {"\uFFFD", `"\xef\xbf\xbd"`},
1034 {"\uFFFD hello \uFFFD world", `"\xef\xbf\xbd hello \xef\xbf\xbd world"`},
1035
1036 {"9223372036854775810ns", `"9223372036854775810ns"`},
1037 {"9223372036854775808ns", `"9223372036854775808ns"`},
1038 {"-9223372036854775809ns", `"-9223372036854775809ns"`},
1039 {"9223372036854776us", `"9223372036854776us"`},
1040 {"3000000h", `"3000000h"`},
1041 {"9223372036854775.808us", `"9223372036854775.808us"`},
1042 {"9223372036854ms775us808ns", `"9223372036854ms775us808ns"`},
1043 }
1044
1045 func TestParseDurationErrors(t *testing.T) {
1046 for _, tc := range parseDurationErrorTests {
1047 _, err := ParseDuration(tc.in)
1048 if err == nil {
1049 t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in)
1050 } else if !strings.Contains(err.Error(), tc.expect) {
1051 t.Errorf("ParseDuration(%q) = _, %q, error does not contain %q", tc.in, err, tc.expect)
1052 }
1053 }
1054 }
1055
1056 func TestParseDurationRoundTrip(t *testing.T) {
1057
1058 max0 := Duration(math.MaxInt64)
1059 max1, err := ParseDuration(max0.String())
1060 if err != nil || max0 != max1 {
1061 t.Errorf("round-trip failed: %d => %q => %d, %v", max0, max0.String(), max1, err)
1062 }
1063
1064 min0 := Duration(math.MinInt64)
1065 min1, err := ParseDuration(min0.String())
1066 if err != nil || min0 != min1 {
1067 t.Errorf("round-trip failed: %d => %q => %d, %v", min0, min0.String(), min1, err)
1068 }
1069
1070 for i := 0; i < 100; i++ {
1071
1072
1073 d0 := Duration(rand.Int31()) * Millisecond
1074 s := d0.String()
1075 d1, err := ParseDuration(s)
1076 if err != nil || d0 != d1 {
1077 t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s, d1, err)
1078 }
1079 }
1080 }
1081
1082
1083 func TestLocationRace(t *testing.T) {
1084 ResetLocalOnceForTest()
1085
1086 c := make(chan string, 1)
1087 go func() {
1088 c <- Now().String()
1089 }()
1090 _ = Now().String()
1091 <-c
1092 Sleep(100 * Millisecond)
1093
1094
1095 ForceUSPacificForTesting()
1096 }
1097
1098 var (
1099 t Time
1100 u int64
1101 )
1102
1103 var mallocTest = []struct {
1104 count int
1105 desc string
1106 fn func()
1107 }{
1108 {0, `time.Now()`, func() { t = Now() }},
1109 {0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }},
1110 {0, `time.Now().UnixMilli()`, func() { u = Now().UnixMilli() }},
1111 {0, `time.Now().UnixMicro()`, func() { u = Now().UnixMicro() }},
1112 }
1113
1114 func TestCountMallocs(t *testing.T) {
1115 if testing.Short() {
1116 t.Skip("skipping malloc count in short mode")
1117 }
1118 if runtime.GOMAXPROCS(0) > 1 {
1119 t.Skip("skipping; GOMAXPROCS>1")
1120 }
1121 for _, mt := range mallocTest {
1122 allocs := int(testing.AllocsPerRun(100, mt.fn))
1123 if allocs > mt.count {
1124 t.Errorf("%s: %d allocs, want %d", mt.desc, allocs, mt.count)
1125 }
1126 }
1127 }
1128
1129 func TestLoadFixed(t *testing.T) {
1130
1131 loc, err := LoadLocation("Etc/GMT+1")
1132 if err != nil {
1133 t.Fatal(err)
1134 }
1135
1136
1137
1138
1139 name, offset := Now().In(loc).Zone()
1140
1141
1142 wantName := []string{"GMT+1", "-01"}
1143
1144 if runtime.GOOS == "openbsd" {
1145 wantName = append(wantName, "+01")
1146 }
1147 if !slices.Contains(wantName, name) || offset != -1*60*60 {
1148 t.Errorf("Now().In(loc).Zone() = %q, %d, want %q (one of), %d",
1149 name, offset, wantName, -1*60*60)
1150 }
1151 }
1152
1153 const (
1154 minDuration Duration = -1 << 63
1155 maxDuration Duration = 1<<63 - 1
1156 )
1157
1158 var subTests = []struct {
1159 t Time
1160 u Time
1161 d Duration
1162 }{
1163 {Time{}, Time{}, Duration(0)},
1164 {Date(2009, 11, 23, 0, 0, 0, 1, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), Duration(1)},
1165 {Date(2009, 11, 23, 0, 0, 0, 0, UTC), Date(2009, 11, 24, 0, 0, 0, 0, UTC), -24 * Hour},
1166 {Date(2009, 11, 24, 0, 0, 0, 0, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour},
1167 {Date(-2009, 11, 24, 0, 0, 0, 0, UTC), Date(-2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour},
1168 {Time{}, Date(2109, 11, 23, 0, 0, 0, 0, UTC), minDuration},
1169 {Date(2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, maxDuration},
1170 {Time{}, Date(-2109, 11, 23, 0, 0, 0, 0, UTC), maxDuration},
1171 {Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, minDuration},
1172 {Date(2290, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), 290*365*24*Hour + 71*24*Hour},
1173 {Date(2300, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), maxDuration},
1174 {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2290, 1, 1, 0, 0, 0, 0, UTC), -290*365*24*Hour - 71*24*Hour},
1175 {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2300, 1, 1, 0, 0, 0, 0, UTC), minDuration},
1176 {Date(2311, 11, 26, 02, 16, 47, 63535996, UTC), Date(2019, 8, 16, 2, 29, 30, 268436582, UTC), 9223372036795099414},
1177 {MinMonoTime, MaxMonoTime, minDuration},
1178 {MaxMonoTime, MinMonoTime, maxDuration},
1179 }
1180
1181 func TestSub(t *testing.T) {
1182 for i, st := range subTests {
1183 got := st.t.Sub(st.u)
1184 if got != st.d {
1185 t.Errorf("#%d: Sub(%v, %v): got %v; want %v", i, st.t, st.u, got, st.d)
1186 }
1187 }
1188 }
1189
1190 var nsDurationTests = []struct {
1191 d Duration
1192 want int64
1193 }{
1194 {Duration(-1000), -1000},
1195 {Duration(-1), -1},
1196 {Duration(1), 1},
1197 {Duration(1000), 1000},
1198 }
1199
1200 func TestDurationNanoseconds(t *testing.T) {
1201 for _, tt := range nsDurationTests {
1202 if got := tt.d.Nanoseconds(); got != tt.want {
1203 t.Errorf("Duration(%s).Nanoseconds() = %d; want: %d", tt.d, got, tt.want)
1204 }
1205 }
1206 }
1207
1208 var usDurationTests = []struct {
1209 d Duration
1210 want int64
1211 }{
1212 {Duration(-1000), -1},
1213 {Duration(1000), 1},
1214 }
1215
1216 func TestDurationMicroseconds(t *testing.T) {
1217 for _, tt := range usDurationTests {
1218 if got := tt.d.Microseconds(); got != tt.want {
1219 t.Errorf("Duration(%s).Microseconds() = %d; want: %d", tt.d, got, tt.want)
1220 }
1221 }
1222 }
1223
1224 var msDurationTests = []struct {
1225 d Duration
1226 want int64
1227 }{
1228 {Duration(-1000000), -1},
1229 {Duration(1000000), 1},
1230 }
1231
1232 func TestDurationMilliseconds(t *testing.T) {
1233 for _, tt := range msDurationTests {
1234 if got := tt.d.Milliseconds(); got != tt.want {
1235 t.Errorf("Duration(%s).Milliseconds() = %d; want: %d", tt.d, got, tt.want)
1236 }
1237 }
1238 }
1239
1240 var secDurationTests = []struct {
1241 d Duration
1242 want float64
1243 }{
1244 {Duration(300000000), 0.3},
1245 }
1246
1247 func TestDurationSeconds(t *testing.T) {
1248 for _, tt := range secDurationTests {
1249 if got := tt.d.Seconds(); got != tt.want {
1250 t.Errorf("Duration(%s).Seconds() = %g; want: %g", tt.d, got, tt.want)
1251 }
1252 }
1253 }
1254
1255 var minDurationTests = []struct {
1256 d Duration
1257 want float64
1258 }{
1259 {Duration(-60000000000), -1},
1260 {Duration(-1), -1 / 60e9},
1261 {Duration(1), 1 / 60e9},
1262 {Duration(60000000000), 1},
1263 {Duration(3000), 5e-8},
1264 }
1265
1266 func TestDurationMinutes(t *testing.T) {
1267 for _, tt := range minDurationTests {
1268 if got := tt.d.Minutes(); got != tt.want {
1269 t.Errorf("Duration(%s).Minutes() = %g; want: %g", tt.d, got, tt.want)
1270 }
1271 }
1272 }
1273
1274 var hourDurationTests = []struct {
1275 d Duration
1276 want float64
1277 }{
1278 {Duration(-3600000000000), -1},
1279 {Duration(-1), -1 / 3600e9},
1280 {Duration(1), 1 / 3600e9},
1281 {Duration(3600000000000), 1},
1282 {Duration(36), 1e-11},
1283 }
1284
1285 func TestDurationHours(t *testing.T) {
1286 for _, tt := range hourDurationTests {
1287 if got := tt.d.Hours(); got != tt.want {
1288 t.Errorf("Duration(%s).Hours() = %g; want: %g", tt.d, got, tt.want)
1289 }
1290 }
1291 }
1292
1293 var durationTruncateTests = []struct {
1294 d Duration
1295 m Duration
1296 want Duration
1297 }{
1298 {0, Second, 0},
1299 {Minute, -7 * Second, Minute},
1300 {Minute, 0, Minute},
1301 {Minute, 1, Minute},
1302 {Minute + 10*Second, 10 * Second, Minute + 10*Second},
1303 {2*Minute + 10*Second, Minute, 2 * Minute},
1304 {10*Minute + 10*Second, 3 * Minute, 9 * Minute},
1305 {Minute + 10*Second, Minute + 10*Second + 1, 0},
1306 {Minute + 10*Second, Hour, 0},
1307 {-Minute, Second, -Minute},
1308 {-10 * Minute, 3 * Minute, -9 * Minute},
1309 {-10 * Minute, Hour, 0},
1310 }
1311
1312 func TestDurationTruncate(t *testing.T) {
1313 for _, tt := range durationTruncateTests {
1314 if got := tt.d.Truncate(tt.m); got != tt.want {
1315 t.Errorf("Duration(%s).Truncate(%s) = %s; want: %s", tt.d, tt.m, got, tt.want)
1316 }
1317 }
1318 }
1319
1320 var durationRoundTests = []struct {
1321 d Duration
1322 m Duration
1323 want Duration
1324 }{
1325 {0, Second, 0},
1326 {Minute, -11 * Second, Minute},
1327 {Minute, 0, Minute},
1328 {Minute, 1, Minute},
1329 {2 * Minute, Minute, 2 * Minute},
1330 {2*Minute + 10*Second, Minute, 2 * Minute},
1331 {2*Minute + 30*Second, Minute, 3 * Minute},
1332 {2*Minute + 50*Second, Minute, 3 * Minute},
1333 {-Minute, 1, -Minute},
1334 {-2 * Minute, Minute, -2 * Minute},
1335 {-2*Minute - 10*Second, Minute, -2 * Minute},
1336 {-2*Minute - 30*Second, Minute, -3 * Minute},
1337 {-2*Minute - 50*Second, Minute, -3 * Minute},
1338 {8e18, 3e18, 9e18},
1339 {9e18, 5e18, 1<<63 - 1},
1340 {-8e18, 3e18, -9e18},
1341 {-9e18, 5e18, -1 << 63},
1342 {3<<61 - 1, 3 << 61, 3 << 61},
1343 }
1344
1345 func TestDurationRound(t *testing.T) {
1346 for _, tt := range durationRoundTests {
1347 if got := tt.d.Round(tt.m); got != tt.want {
1348 t.Errorf("Duration(%s).Round(%s) = %s; want: %s", tt.d, tt.m, got, tt.want)
1349 }
1350 }
1351 }
1352
1353 var durationAbsTests = []struct {
1354 d Duration
1355 want Duration
1356 }{
1357 {0, 0},
1358 {1, 1},
1359 {-1, 1},
1360 {1 * Minute, 1 * Minute},
1361 {-1 * Minute, 1 * Minute},
1362 {minDuration, maxDuration},
1363 {minDuration + 1, maxDuration},
1364 {minDuration + 2, maxDuration - 1},
1365 {maxDuration, maxDuration},
1366 {maxDuration - 1, maxDuration - 1},
1367 }
1368
1369 func TestDurationAbs(t *testing.T) {
1370 for _, tt := range durationAbsTests {
1371 if got := tt.d.Abs(); got != tt.want {
1372 t.Errorf("Duration(%s).Abs() = %s; want: %s", tt.d, got, tt.want)
1373 }
1374 }
1375 }
1376
1377 var defaultLocTests = []struct {
1378 name string
1379 f func(t1, t2 Time) bool
1380 }{
1381 {"After", func(t1, t2 Time) bool { return t1.After(t2) == t2.After(t1) }},
1382 {"Before", func(t1, t2 Time) bool { return t1.Before(t2) == t2.Before(t1) }},
1383 {"Equal", func(t1, t2 Time) bool { return t1.Equal(t2) == t2.Equal(t1) }},
1384 {"Compare", func(t1, t2 Time) bool { return t1.Compare(t2) == t2.Compare(t1) }},
1385
1386 {"IsZero", func(t1, t2 Time) bool { return t1.IsZero() == t2.IsZero() }},
1387 {"Date", func(t1, t2 Time) bool {
1388 a1, b1, c1 := t1.Date()
1389 a2, b2, c2 := t2.Date()
1390 return a1 == a2 && b1 == b2 && c1 == c2
1391 }},
1392 {"Year", func(t1, t2 Time) bool { return t1.Year() == t2.Year() }},
1393 {"Month", func(t1, t2 Time) bool { return t1.Month() == t2.Month() }},
1394 {"Day", func(t1, t2 Time) bool { return t1.Day() == t2.Day() }},
1395 {"Weekday", func(t1, t2 Time) bool { return t1.Weekday() == t2.Weekday() }},
1396 {"ISOWeek", func(t1, t2 Time) bool {
1397 a1, b1 := t1.ISOWeek()
1398 a2, b2 := t2.ISOWeek()
1399 return a1 == a2 && b1 == b2
1400 }},
1401 {"Clock", func(t1, t2 Time) bool {
1402 a1, b1, c1 := t1.Clock()
1403 a2, b2, c2 := t2.Clock()
1404 return a1 == a2 && b1 == b2 && c1 == c2
1405 }},
1406 {"Hour", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }},
1407 {"Minute", func(t1, t2 Time) bool { return t1.Minute() == t2.Minute() }},
1408 {"Second", func(t1, t2 Time) bool { return t1.Second() == t2.Second() }},
1409 {"Nanosecond", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }},
1410 {"YearDay", func(t1, t2 Time) bool { return t1.YearDay() == t2.YearDay() }},
1411
1412
1413 {"Add", func(t1, t2 Time) bool { return t1.Add(Hour).Equal(t2.Add(Hour)) }},
1414 {"Sub", func(t1, t2 Time) bool { return t1.Sub(t2) == t2.Sub(t1) }},
1415
1416
1417 {"AddDate", func(t1, t2 Time) bool { return t1.AddDate(1991, 9, 3) == t2.AddDate(1991, 9, 3) }},
1418
1419 {"UTC", func(t1, t2 Time) bool { return t1.UTC() == t2.UTC() }},
1420 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }},
1421 {"In", func(t1, t2 Time) bool { return t1.In(UTC) == t2.In(UTC) }},
1422
1423 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }},
1424 {"Zone", func(t1, t2 Time) bool {
1425 a1, b1 := t1.Zone()
1426 a2, b2 := t2.Zone()
1427 return a1 == a2 && b1 == b2
1428 }},
1429
1430 {"Unix", func(t1, t2 Time) bool { return t1.Unix() == t2.Unix() }},
1431 {"UnixNano", func(t1, t2 Time) bool { return t1.UnixNano() == t2.UnixNano() }},
1432 {"UnixMilli", func(t1, t2 Time) bool { return t1.UnixMilli() == t2.UnixMilli() }},
1433 {"UnixMicro", func(t1, t2 Time) bool { return t1.UnixMicro() == t2.UnixMicro() }},
1434
1435 {"AppendBinary", func(t1, t2 Time) bool {
1436 buf1 := make([]byte, 4, 32)
1437 buf2 := make([]byte, 4, 32)
1438 a1, b1 := t1.AppendBinary(buf1)
1439 a2, b2 := t2.AppendBinary(buf2)
1440 return bytes.Equal(a1[4:], a2[4:]) && b1 == b2
1441 }},
1442 {"MarshalBinary", func(t1, t2 Time) bool {
1443 a1, b1 := t1.MarshalBinary()
1444 a2, b2 := t2.MarshalBinary()
1445 return bytes.Equal(a1, a2) && b1 == b2
1446 }},
1447 {"GobEncode", func(t1, t2 Time) bool {
1448 a1, b1 := t1.GobEncode()
1449 a2, b2 := t2.GobEncode()
1450 return bytes.Equal(a1, a2) && b1 == b2
1451 }},
1452 {"MarshalJSON", func(t1, t2 Time) bool {
1453 a1, b1 := t1.MarshalJSON()
1454 a2, b2 := t2.MarshalJSON()
1455 return bytes.Equal(a1, a2) && b1 == b2
1456 }},
1457 {"AppendText", func(t1, t2 Time) bool {
1458 maxCap := len(RFC3339Nano) + 4
1459 buf1 := make([]byte, 4, maxCap)
1460 buf2 := make([]byte, 4, maxCap)
1461 a1, b1 := t1.AppendText(buf1)
1462 a2, b2 := t2.AppendText(buf2)
1463 return bytes.Equal(a1[4:], a2[4:]) && b1 == b2
1464 }},
1465 {"MarshalText", func(t1, t2 Time) bool {
1466 a1, b1 := t1.MarshalText()
1467 a2, b2 := t2.MarshalText()
1468 return bytes.Equal(a1, a2) && b1 == b2
1469 }},
1470
1471 {"Truncate", func(t1, t2 Time) bool { return t1.Truncate(Hour).Equal(t2.Truncate(Hour)) }},
1472 {"Round", func(t1, t2 Time) bool { return t1.Round(Hour).Equal(t2.Round(Hour)) }},
1473
1474 {"== Time{}", func(t1, t2 Time) bool { return (t1 == Time{}) == (t2 == Time{}) }},
1475 }
1476
1477 func TestDefaultLoc(t *testing.T) {
1478
1479
1480 for _, tt := range defaultLocTests {
1481 t1 := Time{}
1482 t2 := Time{}.UTC()
1483 if !tt.f(t1, t2) {
1484 t.Errorf("Time{} and Time{}.UTC() behave differently for %s", tt.name)
1485 }
1486 }
1487 }
1488
1489 func BenchmarkNow(b *testing.B) {
1490 for i := 0; i < b.N; i++ {
1491 t = Now()
1492 }
1493 }
1494
1495 func BenchmarkNowUnixNano(b *testing.B) {
1496 for i := 0; i < b.N; i++ {
1497 u = Now().UnixNano()
1498 }
1499 }
1500
1501 func BenchmarkNowUnixMilli(b *testing.B) {
1502 for i := 0; i < b.N; i++ {
1503 u = Now().UnixMilli()
1504 }
1505 }
1506
1507 func BenchmarkNowUnixMicro(b *testing.B) {
1508 for i := 0; i < b.N; i++ {
1509 u = Now().UnixMicro()
1510 }
1511 }
1512
1513 func BenchmarkSince(b *testing.B) {
1514 start := Now()
1515 for b.Loop() {
1516 u = int64(Since(start))
1517 }
1518 }
1519
1520 func BenchmarkUntil(b *testing.B) {
1521 end := Now().Add(1 * Hour)
1522 for b.Loop() {
1523 u = int64(Until(end))
1524 }
1525 }
1526
1527 func BenchmarkFormat(b *testing.B) {
1528 t := Unix(1265346057, 0)
1529 for i := 0; i < b.N; i++ {
1530 t.Format("Mon Jan 2 15:04:05 2006")
1531 }
1532 }
1533
1534 func BenchmarkFormatRFC3339(b *testing.B) {
1535 t := Unix(1265346057, 0)
1536 for i := 0; i < b.N; i++ {
1537 t.Format("2006-01-02T15:04:05Z07:00")
1538 }
1539 }
1540
1541 func BenchmarkFormatRFC3339Nano(b *testing.B) {
1542 t := Unix(1265346057, 0)
1543 for i := 0; i < b.N; i++ {
1544 t.Format("2006-01-02T15:04:05.999999999Z07:00")
1545 }
1546 }
1547
1548 func BenchmarkFormatNow(b *testing.B) {
1549
1550
1551 t := Now()
1552 for i := 0; i < b.N; i++ {
1553 t.Format("Mon Jan 2 15:04:05 2006")
1554 }
1555 }
1556
1557 func BenchmarkMarshalJSON(b *testing.B) {
1558 t := Now()
1559 for i := 0; i < b.N; i++ {
1560 t.MarshalJSON()
1561 }
1562 }
1563
1564 func BenchmarkMarshalText(b *testing.B) {
1565 t := Now()
1566 for i := 0; i < b.N; i++ {
1567 t.MarshalText()
1568 }
1569 }
1570
1571 func BenchmarkMarshalBinary(b *testing.B) {
1572 t := Now()
1573 for i := 0; i < b.N; i++ {
1574 t.MarshalBinary()
1575 }
1576 }
1577
1578 func BenchmarkParse(b *testing.B) {
1579 for i := 0; i < b.N; i++ {
1580 Parse(ANSIC, "Mon Jan 2 15:04:05 2006")
1581 }
1582 }
1583
1584 const testdataRFC3339UTC = "2020-08-22T11:27:43.123456789Z"
1585
1586 func BenchmarkParseRFC3339UTC(b *testing.B) {
1587 for i := 0; i < b.N; i++ {
1588 Parse(RFC3339, testdataRFC3339UTC)
1589 }
1590 }
1591
1592 var testdataRFC3339UTCBytes = []byte(testdataRFC3339UTC)
1593
1594 func BenchmarkParseRFC3339UTCBytes(b *testing.B) {
1595 for i := 0; i < b.N; i++ {
1596 Parse(RFC3339, string(testdataRFC3339UTCBytes))
1597 }
1598 }
1599
1600 const testdataRFC3339TZ = "2020-08-22T11:27:43.123456789-02:00"
1601
1602 func BenchmarkParseRFC3339TZ(b *testing.B) {
1603 for i := 0; i < b.N; i++ {
1604 Parse(RFC3339, testdataRFC3339TZ)
1605 }
1606 }
1607
1608 var testdataRFC3339TZBytes = []byte(testdataRFC3339TZ)
1609
1610 func BenchmarkParseRFC3339TZBytes(b *testing.B) {
1611 for i := 0; i < b.N; i++ {
1612 Parse(RFC3339, string(testdataRFC3339TZBytes))
1613 }
1614 }
1615
1616 func BenchmarkParseDuration(b *testing.B) {
1617 for i := 0; i < b.N; i++ {
1618 ParseDuration("9007199254.740993ms")
1619 ParseDuration("9007199254740993ns")
1620 }
1621 }
1622
1623 func BenchmarkHour(b *testing.B) {
1624 t := Now()
1625 for i := 0; i < b.N; i++ {
1626 _ = t.Hour()
1627 }
1628 }
1629
1630 func BenchmarkSecond(b *testing.B) {
1631 t := Now()
1632 for i := 0; i < b.N; i++ {
1633 _ = t.Second()
1634 }
1635 }
1636
1637 func BenchmarkDate(b *testing.B) {
1638 t := Now()
1639 for i := 0; i < b.N; i++ {
1640 _, _, _ = t.Date()
1641 }
1642 }
1643
1644 func BenchmarkYear(b *testing.B) {
1645 t := Now()
1646 for i := 0; i < b.N; i++ {
1647 _ = t.Year()
1648 }
1649 }
1650
1651 func BenchmarkYearDay(b *testing.B) {
1652 t := Now()
1653 for i := 0; i < b.N; i++ {
1654 _ = t.YearDay()
1655 }
1656 }
1657
1658 func BenchmarkMonth(b *testing.B) {
1659 t := Now()
1660 for i := 0; i < b.N; i++ {
1661 _ = t.Month()
1662 }
1663 }
1664
1665 func BenchmarkDay(b *testing.B) {
1666 t := Now()
1667 for i := 0; i < b.N; i++ {
1668 _ = t.Day()
1669 }
1670 }
1671
1672 func BenchmarkISOWeek(b *testing.B) {
1673 t := Now()
1674 for i := 0; i < b.N; i++ {
1675 _, _ = t.ISOWeek()
1676 }
1677 }
1678
1679 func BenchmarkGoString(b *testing.B) {
1680 t := Now()
1681 for i := 0; i < b.N; i++ {
1682 _ = t.GoString()
1683 }
1684 }
1685
1686 func BenchmarkDateFunc(b *testing.B) {
1687 var t Time
1688 for range b.N {
1689 t = Date(2020, 8, 22, 11, 27, 43, 123456789, UTC)
1690 }
1691 _ = t
1692 }
1693
1694 func BenchmarkUnmarshalText(b *testing.B) {
1695 var t Time
1696 in := []byte("2020-08-22T11:27:43.123456789-02:00")
1697 for i := 0; i < b.N; i++ {
1698 t.UnmarshalText(in)
1699 }
1700 }
1701
1702 func TestMarshalBinaryZeroTime(t *testing.T) {
1703 t0 := Time{}
1704 enc, err := t0.MarshalBinary()
1705 if err != nil {
1706 t.Fatal(err)
1707 }
1708 t1 := Now()
1709 if err := t1.UnmarshalBinary(enc); err != nil {
1710 t.Fatal(err)
1711 }
1712 if t1 != t0 {
1713 t.Errorf("t0=%#v\nt1=%#v\nwant identical structures", t0, t1)
1714 }
1715 }
1716
1717 func TestMarshalBinaryVersion2(t *testing.T) {
1718 t0, err := Parse(RFC3339, "1880-01-01T00:00:00Z")
1719 if err != nil {
1720 t.Errorf("Failed to parse time, error = %v", err)
1721 }
1722 loc, err := LoadLocation("US/Eastern")
1723 if err != nil {
1724 t.Errorf("Failed to load location, error = %v", err)
1725 }
1726 t1 := t0.In(loc)
1727 b, err := t1.MarshalBinary()
1728 if err != nil {
1729 t.Errorf("Failed to Marshal, error = %v", err)
1730 }
1731
1732 t2 := Time{}
1733 err = t2.UnmarshalBinary(b)
1734 if err != nil {
1735 t.Errorf("Failed to Unmarshal, error = %v", err)
1736 }
1737
1738 if !(t0.Equal(t1) && t1.Equal(t2)) {
1739 if !t0.Equal(t1) {
1740 t.Errorf("The result t1: %+v after Marshal is not matched original t0: %+v", t1, t0)
1741 }
1742 if !t1.Equal(t2) {
1743 t.Errorf("The result t2: %+v after Unmarshal is not matched original t1: %+v", t2, t1)
1744 }
1745 }
1746 }
1747
1748 func TestUnmarshalTextAllocations(t *testing.T) {
1749 in := []byte(testdataRFC3339UTC)
1750 if allocs := testing.AllocsPerRun(100, func() {
1751 var t Time
1752 t.UnmarshalText(in)
1753 }); allocs != 0 {
1754 t.Errorf("got %v allocs, want 0 allocs", allocs)
1755 }
1756 }
1757
1758
1759 func TestZeroMonthString(t *testing.T) {
1760 if got, want := Month(0).String(), "%!Month(0)"; got != want {
1761 t.Errorf("zero month = %q; want %q", got, want)
1762 }
1763 }
1764
1765
1766 func TestWeekdayString(t *testing.T) {
1767 if got, want := Tuesday.String(), "Tuesday"; got != want {
1768 t.Errorf("Tuesday weekday = %q; want %q", got, want)
1769 }
1770 if got, want := Weekday(14).String(), "%!Weekday(14)"; got != want {
1771 t.Errorf("14th weekday = %q; want %q", got, want)
1772 }
1773 }
1774
1775 func TestReadFileLimit(t *testing.T) {
1776 const zero = "/dev/zero"
1777 if _, err := os.Stat(zero); err != nil {
1778 t.Skip("skipping test without a /dev/zero")
1779 }
1780 _, err := ReadFile(zero)
1781 if err == nil || !strings.Contains(err.Error(), "is too large") {
1782 t.Errorf("readFile(%q) error = %v; want error containing 'is too large'", zero, err)
1783 }
1784 }
1785
1786
1787
1788
1789
1790
1791 func TestConcurrentTimerReset(t *testing.T) {
1792 const goroutines = 8
1793 const tries = 1000
1794 var wg sync.WaitGroup
1795 wg.Add(goroutines)
1796 timer := NewTimer(Hour)
1797 for i := 0; i < goroutines; i++ {
1798 go func(i int) {
1799 defer wg.Done()
1800 for j := 0; j < tries; j++ {
1801 timer.Reset(Hour + Duration(i*j))
1802 }
1803 }(i)
1804 }
1805 wg.Wait()
1806 }
1807
1808
1809 func TestConcurrentTimerResetStop(t *testing.T) {
1810 const goroutines = 8
1811 const tries = 1000
1812 var wg sync.WaitGroup
1813 wg.Add(goroutines * 2)
1814 timer := NewTimer(Hour)
1815 for i := 0; i < goroutines; i++ {
1816 go func(i int) {
1817 defer wg.Done()
1818 for j := 0; j < tries; j++ {
1819 timer.Reset(Hour + Duration(i*j))
1820 }
1821 }(i)
1822 go func(i int) {
1823 defer wg.Done()
1824 timer.Stop()
1825 }(i)
1826 }
1827 wg.Wait()
1828 }
1829
1830 func TestTimeIsDST(t *testing.T) {
1831 undo := DisablePlatformSources()
1832 defer undo()
1833
1834 tzWithDST, err := LoadLocation("Australia/Sydney")
1835 if err != nil {
1836 t.Fatalf("could not load tz 'Australia/Sydney': %v", err)
1837 }
1838 tzWithoutDST, err := LoadLocation("Australia/Brisbane")
1839 if err != nil {
1840 t.Fatalf("could not load tz 'Australia/Brisbane': %v", err)
1841 }
1842 tzFixed := FixedZone("FIXED_TIME", 12345)
1843
1844 tests := [...]struct {
1845 time Time
1846 want bool
1847 }{
1848 0: {Date(2009, 1, 1, 12, 0, 0, 0, UTC), false},
1849 1: {Date(2009, 6, 1, 12, 0, 0, 0, UTC), false},
1850 2: {Date(2009, 1, 1, 12, 0, 0, 0, tzWithDST), true},
1851 3: {Date(2009, 6, 1, 12, 0, 0, 0, tzWithDST), false},
1852 4: {Date(2009, 1, 1, 12, 0, 0, 0, tzWithoutDST), false},
1853 5: {Date(2009, 6, 1, 12, 0, 0, 0, tzWithoutDST), false},
1854 6: {Date(2009, 1, 1, 12, 0, 0, 0, tzFixed), false},
1855 7: {Date(2009, 6, 1, 12, 0, 0, 0, tzFixed), false},
1856 }
1857
1858 for i, tt := range tests {
1859 got := tt.time.IsDST()
1860 if got != tt.want {
1861 t.Errorf("#%d:: (%#v).IsDST()=%t, want %t", i, tt.time.Format(RFC3339), got, tt.want)
1862 }
1863 }
1864 }
1865
1866 func TestTimeAddSecOverflow(t *testing.T) {
1867
1868 var maxInt64 int64 = 1<<63 - 1
1869 timeExt := maxInt64 - UnixToInternal - 50
1870 notMonoTime := Unix(timeExt, 0)
1871 for i := int64(0); i < 100; i++ {
1872 sec := notMonoTime.Unix()
1873 notMonoTime = notMonoTime.Add(Duration(i * 1e9))
1874 if newSec := notMonoTime.Unix(); newSec != sec+i && newSec+UnixToInternal != maxInt64 {
1875 t.Fatalf("time ext: %d overflows with positive delta, overflow threshold: %d", newSec, maxInt64)
1876 }
1877 }
1878
1879
1880 maxInt64 = -maxInt64
1881 notMonoTime = NotMonoNegativeTime
1882 for i := int64(0); i > -100; i-- {
1883 sec := notMonoTime.Unix()
1884 notMonoTime = notMonoTime.Add(Duration(i * 1e9))
1885 if newSec := notMonoTime.Unix(); newSec != sec+i && newSec+UnixToInternal != maxInt64 {
1886 t.Fatalf("time ext: %d overflows with positive delta, overflow threshold: %d", newSec, maxInt64)
1887 }
1888 }
1889 }
1890
1891
1892 func TestTimeWithZoneTransition(t *testing.T) {
1893 undo := DisablePlatformSources()
1894 defer undo()
1895
1896 loc, err := LoadLocation("Asia/Shanghai")
1897 if err != nil {
1898 t.Fatal(err)
1899 }
1900
1901 tests := [...]struct {
1902 give Time
1903 want Time
1904 }{
1905
1906
1907
1908
1909
1910 0: {Date(1991, April, 13, 17, 50, 0, 0, loc), Date(1991, April, 13, 9, 50, 0, 0, UTC)},
1911 1: {Date(1991, April, 13, 18, 0, 0, 0, loc), Date(1991, April, 13, 10, 0, 0, 0, UTC)},
1912 2: {Date(1991, April, 14, 1, 50, 0, 0, loc), Date(1991, April, 13, 17, 50, 0, 0, UTC)},
1913 3: {Date(1991, April, 14, 3, 0, 0, 0, loc), Date(1991, April, 13, 18, 0, 0, 0, UTC)},
1914
1915
1916
1917
1918
1919
1920 4: {Date(1991, September, 14, 16, 50, 0, 0, loc), Date(1991, September, 14, 7, 50, 0, 0, UTC)},
1921 5: {Date(1991, September, 14, 17, 0, 0, 0, loc), Date(1991, September, 14, 8, 0, 0, 0, UTC)},
1922 6: {Date(1991, September, 15, 0, 50, 0, 0, loc), Date(1991, September, 14, 15, 50, 0, 0, UTC)},
1923 7: {Date(1991, September, 15, 2, 00, 0, 0, loc), Date(1991, September, 14, 18, 00, 0, 0, UTC)},
1924 }
1925
1926 for i, tt := range tests {
1927 if !tt.give.Equal(tt.want) {
1928 t.Errorf("#%d:: %#v is not equal to %#v", i, tt.give.Format(RFC3339), tt.want.Format(RFC3339))
1929 }
1930 }
1931 }
1932
1933 func TestZoneBounds(t *testing.T) {
1934 undo := DisablePlatformSources()
1935 defer undo()
1936 loc, err := LoadLocation("Asia/Shanghai")
1937 if err != nil {
1938 t.Fatal(err)
1939 }
1940
1941
1942 for _, test := range utctests {
1943 sec := test.seconds
1944 golden := &test.golden
1945 tm := Unix(sec, 0).UTC()
1946 start, end := tm.ZoneBounds()
1947 if !(start.IsZero() && end.IsZero()) {
1948 t.Errorf("ZoneBounds of %+v expects two zero Time, got:\n start=%v\n end=%v", *golden, start, end)
1949 }
1950 }
1951
1952
1953
1954 beginTime := Date(math.MinInt32, January, 1, 0, 0, 0, 0, loc)
1955 start, end := beginTime.ZoneBounds()
1956 if !start.IsZero() || end.IsZero() {
1957 t.Errorf("ZoneBounds of %v expects start is zero Time, got:\n start=%v\n end=%v", beginTime, start, end)
1958 }
1959
1960
1961
1962 foreverTime := Date(math.MaxInt32, January, 1, 0, 0, 0, 0, loc)
1963 start, end = foreverTime.ZoneBounds()
1964 if start.IsZero() || !end.IsZero() {
1965 t.Errorf("ZoneBounds of %v expects end is zero Time, got:\n start=%v\n end=%v", foreverTime, start, end)
1966 }
1967
1968
1969 boundOne := Date(1990, September, 16, 1, 0, 0, 0, loc)
1970 boundTwo := Date(1991, April, 14, 3, 0, 0, 0, loc)
1971 boundThree := Date(1991, September, 15, 1, 0, 0, 0, loc)
1972 makeLocalTime := func(sec int64) Time { return Unix(sec, 0) }
1973 realTests := [...]struct {
1974 giveTime Time
1975 wantStart Time
1976 wantEnd Time
1977 }{
1978
1979 0: {Date(1991, April, 13, 17, 50, 0, 0, loc), boundOne, boundTwo},
1980 1: {Date(1991, April, 13, 18, 0, 0, 0, loc), boundOne, boundTwo},
1981 2: {Date(1991, April, 14, 1, 50, 0, 0, loc), boundOne, boundTwo},
1982 3: {boundTwo, boundTwo, boundThree},
1983 4: {Date(1991, September, 14, 16, 50, 0, 0, loc), boundTwo, boundThree},
1984 5: {Date(1991, September, 14, 17, 0, 0, 0, loc), boundTwo, boundThree},
1985 6: {Date(1991, September, 15, 0, 50, 0, 0, loc), boundTwo, boundThree},
1986
1987
1988 7: {boundThree, boundThree, Time{}},
1989 8: {Date(1991, December, 15, 1, 50, 0, 0, loc), boundThree, Time{}},
1990 9: {Date(1992, April, 13, 17, 50, 0, 0, loc), boundThree, Time{}},
1991 10: {Date(1992, April, 13, 18, 0, 0, 0, loc), boundThree, Time{}},
1992 11: {Date(1992, April, 14, 1, 50, 0, 0, loc), boundThree, Time{}},
1993 12: {Date(1992, September, 14, 16, 50, 0, 0, loc), boundThree, Time{}},
1994 13: {Date(1992, September, 14, 17, 0, 0, 0, loc), boundThree, Time{}},
1995 14: {Date(1992, September, 15, 0, 50, 0, 0, loc), boundThree, Time{}},
1996
1997
1998
1999 15: {makeLocalTime(0), makeLocalTime(-5756400), makeLocalTime(9972000)},
2000 16: {makeLocalTime(1221681866), makeLocalTime(1205056800), makeLocalTime(1225616400)},
2001 17: {makeLocalTime(2152173599), makeLocalTime(2145916800), makeLocalTime(2152173600)},
2002 18: {makeLocalTime(2152173600), makeLocalTime(2152173600), makeLocalTime(2172733200)},
2003 19: {makeLocalTime(2152173601), makeLocalTime(2152173600), makeLocalTime(2172733200)},
2004 20: {makeLocalTime(2159200800), makeLocalTime(2152173600), makeLocalTime(2172733200)},
2005 21: {makeLocalTime(2172733199), makeLocalTime(2152173600), makeLocalTime(2172733200)},
2006 22: {makeLocalTime(2172733200), makeLocalTime(2172733200), makeLocalTime(2177452800)},
2007 }
2008 for i, tt := range realTests {
2009 start, end := tt.giveTime.ZoneBounds()
2010 if !start.Equal(tt.wantStart) || !end.Equal(tt.wantEnd) {
2011 t.Errorf("#%d:: ZoneBounds of %v expects right bounds:\n got start=%v\n want start=%v\n got end=%v\n want end=%v",
2012 i, tt.giveTime, start, tt.wantStart, end, tt.wantEnd)
2013 }
2014 }
2015 }
2016
View as plain text