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