libgo: update to go1.9
[official-gcc.git] / libgo / go / time / time_test.go
blobdba8e0dadcdd06bf45b5a0ac81ceed0fabbeaf8a
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package time_test
7 import (
8 "bytes"
9 "encoding/gob"
10 "encoding/json"
11 "fmt"
12 "math/big"
13 "math/rand"
14 "os"
15 "runtime"
16 "strings"
17 "testing"
18 "testing/quick"
19 . "time"
22 // We should be in PST/PDT, but if the time zone files are missing we
23 // won't be. The purpose of this test is to at least explain why some of
24 // the subsequent tests fail.
25 func TestZoneData(t *testing.T) {
26 lt := Now()
27 // PST is 8 hours west, PDT is 7 hours west. We could use the name but it's not unique.
28 if name, off := lt.Zone(); off != -8*60*60 && off != -7*60*60 {
29 t.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", name, off)
30 t.Error("Likely problem: the time zone files have not been installed.")
34 // parsedTime is the struct representing a parsed time value.
35 type parsedTime struct {
36 Year int
37 Month Month
38 Day int
39 Hour, Minute, Second int // 15:04:05 is 15, 4, 5.
40 Nanosecond int // Fractional second.
41 Weekday Weekday
42 ZoneOffset int // seconds east of UTC, e.g. -7*60*60 for -0700
43 Zone string // e.g., "MST"
46 type TimeTest struct {
47 seconds int64
48 golden parsedTime
51 var utctests = []TimeTest{
52 {0, parsedTime{1970, January, 1, 0, 0, 0, 0, Thursday, 0, "UTC"}},
53 {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 0, Wednesday, 0, "UTC"}},
54 {-1221681866, parsedTime{1931, April, 16, 3, 55, 34, 0, Thursday, 0, "UTC"}},
55 {-11644473600, parsedTime{1601, January, 1, 0, 0, 0, 0, Monday, 0, "UTC"}},
56 {599529660, parsedTime{1988, December, 31, 0, 1, 0, 0, Saturday, 0, "UTC"}},
57 {978220860, parsedTime{2000, December, 31, 0, 1, 0, 0, Sunday, 0, "UTC"}},
60 var nanoutctests = []TimeTest{
61 {0, parsedTime{1970, January, 1, 0, 0, 0, 1e8, Thursday, 0, "UTC"}},
62 {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 2e8, Wednesday, 0, "UTC"}},
65 var localtests = []TimeTest{
66 {0, parsedTime{1969, December, 31, 16, 0, 0, 0, Wednesday, -8 * 60 * 60, "PST"}},
67 {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 0, Wednesday, -7 * 60 * 60, "PDT"}},
70 var nanolocaltests = []TimeTest{
71 {0, parsedTime{1969, December, 31, 16, 0, 0, 1e8, Wednesday, -8 * 60 * 60, "PST"}},
72 {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 3e8, Wednesday, -7 * 60 * 60, "PDT"}},
75 func same(t Time, u *parsedTime) bool {
76 // Check aggregates.
77 year, month, day := t.Date()
78 hour, min, sec := t.Clock()
79 name, offset := t.Zone()
80 if year != u.Year || month != u.Month || day != u.Day ||
81 hour != u.Hour || min != u.Minute || sec != u.Second ||
82 name != u.Zone || offset != u.ZoneOffset {
83 return false
85 // Check individual entries.
86 return t.Year() == u.Year &&
87 t.Month() == u.Month &&
88 t.Day() == u.Day &&
89 t.Hour() == u.Hour &&
90 t.Minute() == u.Minute &&
91 t.Second() == u.Second &&
92 t.Nanosecond() == u.Nanosecond &&
93 t.Weekday() == u.Weekday
96 func TestSecondsToUTC(t *testing.T) {
97 for _, test := range utctests {
98 sec := test.seconds
99 golden := &test.golden
100 tm := Unix(sec, 0).UTC()
101 newsec := tm.Unix()
102 if newsec != sec {
103 t.Errorf("SecondsToUTC(%d).Seconds() = %d", sec, newsec)
105 if !same(tm, golden) {
106 t.Errorf("SecondsToUTC(%d): // %#v", sec, tm)
107 t.Errorf(" want=%+v", *golden)
108 t.Errorf(" have=%v", tm.Format(RFC3339+" MST"))
113 func TestNanosecondsToUTC(t *testing.T) {
114 for _, test := range nanoutctests {
115 golden := &test.golden
116 nsec := test.seconds*1e9 + int64(golden.Nanosecond)
117 tm := Unix(0, nsec).UTC()
118 newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
119 if newnsec != nsec {
120 t.Errorf("NanosecondsToUTC(%d).Nanoseconds() = %d", nsec, newnsec)
122 if !same(tm, golden) {
123 t.Errorf("NanosecondsToUTC(%d):", nsec)
124 t.Errorf(" want=%+v", *golden)
125 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
130 func TestSecondsToLocalTime(t *testing.T) {
131 for _, test := range localtests {
132 sec := test.seconds
133 golden := &test.golden
134 tm := Unix(sec, 0)
135 newsec := tm.Unix()
136 if newsec != sec {
137 t.Errorf("SecondsToLocalTime(%d).Seconds() = %d", sec, newsec)
139 if !same(tm, golden) {
140 t.Errorf("SecondsToLocalTime(%d):", sec)
141 t.Errorf(" want=%+v", *golden)
142 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
147 func TestNanosecondsToLocalTime(t *testing.T) {
148 for _, test := range nanolocaltests {
149 golden := &test.golden
150 nsec := test.seconds*1e9 + int64(golden.Nanosecond)
151 tm := Unix(0, nsec)
152 newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
153 if newnsec != nsec {
154 t.Errorf("NanosecondsToLocalTime(%d).Seconds() = %d", nsec, newnsec)
156 if !same(tm, golden) {
157 t.Errorf("NanosecondsToLocalTime(%d):", nsec)
158 t.Errorf(" want=%+v", *golden)
159 t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
164 func TestSecondsToUTCAndBack(t *testing.T) {
165 f := func(sec int64) bool { return Unix(sec, 0).UTC().Unix() == sec }
166 f32 := func(sec int32) bool { return f(int64(sec)) }
167 cfg := &quick.Config{MaxCount: 10000}
169 // Try a reasonable date first, then the huge ones.
170 if err := quick.Check(f32, cfg); err != nil {
171 t.Fatal(err)
173 if err := quick.Check(f, cfg); err != nil {
174 t.Fatal(err)
178 func TestNanosecondsToUTCAndBack(t *testing.T) {
179 f := func(nsec int64) bool {
180 t := Unix(0, nsec).UTC()
181 ns := t.Unix()*1e9 + int64(t.Nanosecond())
182 return ns == nsec
184 f32 := func(nsec int32) bool { return f(int64(nsec)) }
185 cfg := &quick.Config{MaxCount: 10000}
187 // Try a small date first, then the large ones. (The span is only a few hundred years
188 // for nanoseconds in an int64.)
189 if err := quick.Check(f32, cfg); err != nil {
190 t.Fatal(err)
192 if err := quick.Check(f, cfg); err != nil {
193 t.Fatal(err)
197 // The time routines provide no way to get absolute time
198 // (seconds since zero), but we need it to compute the right
199 // answer for bizarre roundings like "to the nearest 3 ns".
200 // Compute as t - year1 = (t - 1970) + (1970 - 2001) + (2001 - 1).
201 // t - 1970 is returned by Unix and Nanosecond.
202 // 1970 - 2001 is -(31*365+8)*86400 = -978307200 seconds.
203 // 2001 - 1 is 2000*365.2425*86400 = 63113904000 seconds.
204 const unixToZero = -978307200 + 63113904000
206 // abs returns the absolute time stored in t, as seconds and nanoseconds.
207 func abs(t Time) (sec, nsec int64) {
208 unix := t.Unix()
209 nano := t.Nanosecond()
210 return unix + unixToZero, int64(nano)
213 // absString returns abs as a decimal string.
214 func absString(t Time) string {
215 sec, nsec := abs(t)
216 if sec < 0 {
217 sec = -sec
218 nsec = -nsec
219 if nsec < 0 {
220 nsec += 1e9
221 sec--
223 return fmt.Sprintf("-%d%09d", sec, nsec)
225 return fmt.Sprintf("%d%09d", sec, nsec)
228 var truncateRoundTests = []struct {
229 t Time
230 d Duration
232 {Date(-1, January, 1, 12, 15, 30, 5e8, UTC), 3},
233 {Date(-1, January, 1, 12, 15, 31, 5e8, UTC), 3},
234 {Date(2012, January, 1, 12, 15, 30, 5e8, UTC), Second},
235 {Date(2012, January, 1, 12, 15, 31, 5e8, UTC), Second},
236 {Unix(-19012425939, 649146258), 7435029458905025217}, // 5.8*d rounds to 6*d, but .8*d+.8*d < 0 < d
239 func TestTruncateRound(t *testing.T) {
240 var (
241 bsec = new(big.Int)
242 bnsec = new(big.Int)
243 bd = new(big.Int)
244 bt = new(big.Int)
245 br = new(big.Int)
246 bq = new(big.Int)
247 b1e9 = new(big.Int)
250 b1e9.SetInt64(1e9)
252 testOne := func(ti, tns, di int64) bool {
253 t0 := Unix(ti, int64(tns)).UTC()
254 d := Duration(di)
255 if d < 0 {
256 d = -d
258 if d <= 0 {
259 d = 1
262 // Compute bt = absolute nanoseconds.
263 sec, nsec := abs(t0)
264 bsec.SetInt64(sec)
265 bnsec.SetInt64(nsec)
266 bt.Mul(bsec, b1e9)
267 bt.Add(bt, bnsec)
269 // Compute quotient and remainder mod d.
270 bd.SetInt64(int64(d))
271 bq.DivMod(bt, bd, br)
273 // To truncate, subtract remainder.
274 // br is < d, so it fits in an int64.
275 r := br.Int64()
276 t1 := t0.Add(-Duration(r))
278 // Check that time.Truncate works.
279 if trunc := t0.Truncate(d); trunc != t1 {
280 t.Errorf("Time.Truncate(%s, %s) = %s, want %s\n"+
281 "%v trunc %v =\n%v want\n%v",
282 t0.Format(RFC3339Nano), d, trunc, t1.Format(RFC3339Nano),
283 absString(t0), int64(d), absString(trunc), absString(t1))
284 return false
287 // To round, add d back if remainder r > d/2 or r == exactly d/2.
288 // The commented out code would round half to even instead of up,
289 // but that makes it time-zone dependent, which is a bit strange.
290 if r > int64(d)/2 || r+r == int64(d) /*&& bq.Bit(0) == 1*/ {
291 t1 = t1.Add(Duration(d))
294 // Check that time.Round works.
295 if rnd := t0.Round(d); rnd != t1 {
296 t.Errorf("Time.Round(%s, %s) = %s, want %s\n"+
297 "%v round %v =\n%v want\n%v",
298 t0.Format(RFC3339Nano), d, rnd, t1.Format(RFC3339Nano),
299 absString(t0), int64(d), absString(rnd), absString(t1))
300 return false
302 return true
305 // manual test cases
306 for _, tt := range truncateRoundTests {
307 testOne(tt.t.Unix(), int64(tt.t.Nanosecond()), int64(tt.d))
310 // exhaustive near 0
311 for i := 0; i < 100; i++ {
312 for j := 1; j < 100; j++ {
313 testOne(unixToZero, int64(i), int64(j))
314 testOne(unixToZero, -int64(i), int64(j))
315 if t.Failed() {
316 return
321 if t.Failed() {
322 return
325 // randomly generated test cases
326 cfg := &quick.Config{MaxCount: 100000}
327 if testing.Short() {
328 cfg.MaxCount = 1000
331 // divisors of Second
332 f1 := func(ti int64, tns int32, logdi int32) bool {
333 d := Duration(1)
334 a, b := uint(logdi%9), (logdi>>16)%9
335 d <<= a
336 for i := 0; i < int(b); i++ {
337 d *= 5
339 return testOne(ti, int64(tns), int64(d))
341 quick.Check(f1, cfg)
343 // multiples of Second
344 f2 := func(ti int64, tns int32, di int32) bool {
345 d := Duration(di) * Second
346 if d < 0 {
347 d = -d
349 return testOne(ti, int64(tns), int64(d))
351 quick.Check(f2, cfg)
353 // halfway cases
354 f3 := func(tns, di int64) bool {
355 di &= 0xfffffffe
356 if di == 0 {
357 di = 2
359 tns -= tns % di
360 if tns < 0 {
361 tns += di / 2
362 } else {
363 tns -= di / 2
365 return testOne(0, tns, di)
367 quick.Check(f3, cfg)
369 // full generality
370 f4 := func(ti int64, tns int32, di int64) bool {
371 return testOne(ti, int64(tns), di)
373 quick.Check(f4, cfg)
376 type ISOWeekTest struct {
377 year int // year
378 month, day int // month and day
379 yex int // expected year
380 wex int // expected week
383 var isoWeekTests = []ISOWeekTest{
384 {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52},
385 {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1},
386 {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52},
387 {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1},
388 {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1},
389 {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2},
390 {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53},
391 {2000, 1, 1, 1999, 52}, {2001, 1, 1, 2001, 1}, {2002, 1, 1, 2002, 1},
392 {2003, 1, 1, 2003, 1}, {2004, 1, 1, 2004, 1}, {2005, 1, 1, 2004, 53},
393 {2006, 1, 1, 2005, 52}, {2007, 1, 1, 2007, 1}, {2008, 1, 1, 2008, 1},
394 {2009, 1, 1, 2009, 1}, {2010, 1, 1, 2009, 53}, {2010, 1, 1, 2009, 53},
395 {2011, 1, 1, 2010, 52}, {2011, 1, 2, 2010, 52}, {2011, 1, 3, 2011, 1},
396 {2011, 1, 4, 2011, 1}, {2011, 1, 5, 2011, 1}, {2011, 1, 6, 2011, 1},
397 {2011, 1, 7, 2011, 1}, {2011, 1, 8, 2011, 1}, {2011, 1, 9, 2011, 1},
398 {2011, 1, 10, 2011, 2}, {2011, 1, 11, 2011, 2}, {2011, 6, 12, 2011, 23},
399 {2011, 6, 13, 2011, 24}, {2011, 12, 25, 2011, 51}, {2011, 12, 26, 2011, 52},
400 {2011, 12, 27, 2011, 52}, {2011, 12, 28, 2011, 52}, {2011, 12, 29, 2011, 52},
401 {2011, 12, 30, 2011, 52}, {2011, 12, 31, 2011, 52}, {1995, 1, 1, 1994, 52},
402 {2012, 1, 1, 2011, 52}, {2012, 1, 2, 2012, 1}, {2012, 1, 8, 2012, 1},
403 {2012, 1, 9, 2012, 2}, {2012, 12, 23, 2012, 51}, {2012, 12, 24, 2012, 52},
404 {2012, 12, 30, 2012, 52}, {2012, 12, 31, 2013, 1}, {2013, 1, 1, 2013, 1},
405 {2013, 1, 6, 2013, 1}, {2013, 1, 7, 2013, 2}, {2013, 12, 22, 2013, 51},
406 {2013, 12, 23, 2013, 52}, {2013, 12, 29, 2013, 52}, {2013, 12, 30, 2014, 1},
407 {2014, 1, 1, 2014, 1}, {2014, 1, 5, 2014, 1}, {2014, 1, 6, 2014, 2},
408 {2015, 1, 1, 2015, 1}, {2016, 1, 1, 2015, 53}, {2017, 1, 1, 2016, 52},
409 {2018, 1, 1, 2018, 1}, {2019, 1, 1, 2019, 1}, {2020, 1, 1, 2020, 1},
410 {2021, 1, 1, 2020, 53}, {2022, 1, 1, 2021, 52}, {2023, 1, 1, 2022, 52},
411 {2024, 1, 1, 2024, 1}, {2025, 1, 1, 2025, 1}, {2026, 1, 1, 2026, 1},
412 {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1},
413 {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1},
414 {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1},
415 {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53},
416 {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52},
419 func TestISOWeek(t *testing.T) {
420 // Selected dates and corner cases
421 for _, wt := range isoWeekTests {
422 dt := Date(wt.year, Month(wt.month), wt.day, 0, 0, 0, 0, UTC)
423 y, w := dt.ISOWeek()
424 if w != wt.wex || y != wt.yex {
425 t.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d",
426 y, w, wt.yex, wt.wex, wt.year, wt.month, wt.day)
430 // The only real invariant: Jan 04 is in week 1
431 for year := 1950; year < 2100; year++ {
432 if y, w := Date(year, January, 4, 0, 0, 0, 0, UTC).ISOWeek(); y != year || w != 1 {
433 t.Errorf("got %d/%d; expected %d/1 for Jan 04", y, w, year)
438 type YearDayTest struct {
439 year, month, day int
440 yday int
443 // Test YearDay in several different scenarios
444 // and corner cases
445 var yearDayTests = []YearDayTest{
446 // Non-leap-year tests
447 {2007, 1, 1, 1},
448 {2007, 1, 15, 15},
449 {2007, 2, 1, 32},
450 {2007, 2, 15, 46},
451 {2007, 3, 1, 60},
452 {2007, 3, 15, 74},
453 {2007, 4, 1, 91},
454 {2007, 12, 31, 365},
456 // Leap-year tests
457 {2008, 1, 1, 1},
458 {2008, 1, 15, 15},
459 {2008, 2, 1, 32},
460 {2008, 2, 15, 46},
461 {2008, 3, 1, 61},
462 {2008, 3, 15, 75},
463 {2008, 4, 1, 92},
464 {2008, 12, 31, 366},
466 // Looks like leap-year (but isn't) tests
467 {1900, 1, 1, 1},
468 {1900, 1, 15, 15},
469 {1900, 2, 1, 32},
470 {1900, 2, 15, 46},
471 {1900, 3, 1, 60},
472 {1900, 3, 15, 74},
473 {1900, 4, 1, 91},
474 {1900, 12, 31, 365},
476 // Year one tests (non-leap)
477 {1, 1, 1, 1},
478 {1, 1, 15, 15},
479 {1, 2, 1, 32},
480 {1, 2, 15, 46},
481 {1, 3, 1, 60},
482 {1, 3, 15, 74},
483 {1, 4, 1, 91},
484 {1, 12, 31, 365},
486 // Year minus one tests (non-leap)
487 {-1, 1, 1, 1},
488 {-1, 1, 15, 15},
489 {-1, 2, 1, 32},
490 {-1, 2, 15, 46},
491 {-1, 3, 1, 60},
492 {-1, 3, 15, 74},
493 {-1, 4, 1, 91},
494 {-1, 12, 31, 365},
496 // 400 BC tests (leap-year)
497 {-400, 1, 1, 1},
498 {-400, 1, 15, 15},
499 {-400, 2, 1, 32},
500 {-400, 2, 15, 46},
501 {-400, 3, 1, 61},
502 {-400, 3, 15, 75},
503 {-400, 4, 1, 92},
504 {-400, 12, 31, 366},
506 // Special Cases
508 // Gregorian calendar change (no effect)
509 {1582, 10, 4, 277},
510 {1582, 10, 15, 288},
513 // Check to see if YearDay is location sensitive
514 var yearDayLocations = []*Location{
515 FixedZone("UTC-8", -8*60*60),
516 FixedZone("UTC-4", -4*60*60),
517 UTC,
518 FixedZone("UTC+4", 4*60*60),
519 FixedZone("UTC+8", 8*60*60),
522 func TestYearDay(t *testing.T) {
523 for _, loc := range yearDayLocations {
524 for _, ydt := range yearDayTests {
525 dt := Date(ydt.year, Month(ydt.month), ydt.day, 0, 0, 0, 0, loc)
526 yday := dt.YearDay()
527 if yday != ydt.yday {
528 t.Errorf("got %d, expected %d for %d-%02d-%02d in %v",
529 yday, ydt.yday, ydt.year, ydt.month, ydt.day, loc)
535 var durationTests = []struct {
536 str string
537 d Duration
539 {"0s", 0},
540 {"1ns", 1 * Nanosecond},
541 {"1.1µs", 1100 * Nanosecond},
542 {"2.2ms", 2200 * Microsecond},
543 {"3.3s", 3300 * Millisecond},
544 {"4m5s", 4*Minute + 5*Second},
545 {"4m5.001s", 4*Minute + 5001*Millisecond},
546 {"5h6m7.001s", 5*Hour + 6*Minute + 7001*Millisecond},
547 {"8m0.000000001s", 8*Minute + 1*Nanosecond},
548 {"2562047h47m16.854775807s", 1<<63 - 1},
549 {"-2562047h47m16.854775808s", -1 << 63},
552 func TestDurationString(t *testing.T) {
553 for _, tt := range durationTests {
554 if str := tt.d.String(); str != tt.str {
555 t.Errorf("Duration(%d).String() = %s, want %s", int64(tt.d), str, tt.str)
557 if tt.d > 0 {
558 if str := (-tt.d).String(); str != "-"+tt.str {
559 t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str)
565 var dateTests = []struct {
566 year, month, day, hour, min, sec, nsec int
567 z *Location
568 unix int64
570 {2011, 11, 6, 1, 0, 0, 0, Local, 1320566400}, // 1:00:00 PDT
571 {2011, 11, 6, 1, 59, 59, 0, Local, 1320569999}, // 1:59:59 PDT
572 {2011, 11, 6, 2, 0, 0, 0, Local, 1320573600}, // 2:00:00 PST
574 {2011, 3, 13, 1, 0, 0, 0, Local, 1300006800}, // 1:00:00 PST
575 {2011, 3, 13, 1, 59, 59, 0, Local, 1300010399}, // 1:59:59 PST
576 {2011, 3, 13, 3, 0, 0, 0, Local, 1300010400}, // 3:00:00 PDT
577 {2011, 3, 13, 2, 30, 0, 0, Local, 1300008600}, // 2:30:00 PDT ≡ 1:30 PST
579 // Many names for Fri Nov 18 7:56:35 PST 2011
580 {2011, 11, 18, 7, 56, 35, 0, Local, 1321631795}, // Nov 18 7:56:35
581 {2011, 11, 19, -17, 56, 35, 0, Local, 1321631795}, // Nov 19 -17:56:35
582 {2011, 11, 17, 31, 56, 35, 0, Local, 1321631795}, // Nov 17 31:56:35
583 {2011, 11, 18, 6, 116, 35, 0, Local, 1321631795}, // Nov 18 6:116:35
584 {2011, 10, 49, 7, 56, 35, 0, Local, 1321631795}, // Oct 49 7:56:35
585 {2011, 11, 18, 7, 55, 95, 0, Local, 1321631795}, // Nov 18 7:55:95
586 {2011, 11, 18, 7, 56, 34, 1e9, Local, 1321631795}, // Nov 18 7:56:34 + 10⁹ns
587 {2011, 12, -12, 7, 56, 35, 0, Local, 1321631795}, // Dec -21 7:56:35
588 {2012, 1, -43, 7, 56, 35, 0, Local, 1321631795}, // Jan -52 7:56:35 2012
589 {2012, int(January - 2), 18, 7, 56, 35, 0, Local, 1321631795}, // (Jan-2) 18 7:56:35 2012
590 {2010, int(December + 11), 18, 7, 56, 35, 0, Local, 1321631795}, // (Dec+11) 18 7:56:35 2010
593 func TestDate(t *testing.T) {
594 for _, tt := range dateTests {
595 time := Date(tt.year, Month(tt.month), tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z)
596 want := Unix(tt.unix, 0)
597 if !time.Equal(want) {
598 t.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v",
599 tt.year, tt.month, tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z,
600 time, want)
605 // Several ways of getting from
606 // Fri Nov 18 7:56:35 PST 2011
607 // to
608 // Thu Mar 19 7:56:35 PST 2016
609 var addDateTests = []struct {
610 years, months, days int
612 {4, 4, 1},
613 {3, 16, 1},
614 {3, 15, 30},
615 {5, -6, -18 - 30 - 12},
618 func TestAddDate(t *testing.T) {
619 t0 := Date(2011, 11, 18, 7, 56, 35, 0, UTC)
620 t1 := Date(2016, 3, 19, 7, 56, 35, 0, UTC)
621 for _, at := range addDateTests {
622 time := t0.AddDate(at.years, at.months, at.days)
623 if !time.Equal(t1) {
624 t.Errorf("AddDate(%d, %d, %d) = %v, want %v",
625 at.years, at.months, at.days,
626 time, t1)
631 var daysInTests = []struct {
632 year, month, di int
634 {2011, 1, 31}, // January, first month, 31 days
635 {2011, 2, 28}, // February, non-leap year, 28 days
636 {2012, 2, 29}, // February, leap year, 29 days
637 {2011, 6, 30}, // June, 30 days
638 {2011, 12, 31}, // December, last month, 31 days
641 func TestDaysIn(t *testing.T) {
642 // The daysIn function is not exported.
643 // Test the daysIn function via the `var DaysIn = daysIn`
644 // statement in the internal_test.go file.
645 for _, tt := range daysInTests {
646 di := DaysIn(Month(tt.month), tt.year)
647 if di != tt.di {
648 t.Errorf("got %d; expected %d for %d-%02d",
649 di, tt.di, tt.year, tt.month)
654 func TestAddToExactSecond(t *testing.T) {
655 // Add an amount to the current time to round it up to the next exact second.
656 // This test checks that the nsec field still lies within the range [0, 999999999].
657 t1 := Now()
658 t2 := t1.Add(Second - Duration(t1.Nanosecond()))
659 sec := (t1.Second() + 1) % 60
660 if t2.Second() != sec || t2.Nanosecond() != 0 {
661 t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec)
665 func equalTimeAndZone(a, b Time) bool {
666 aname, aoffset := a.Zone()
667 bname, boffset := b.Zone()
668 return a.Equal(b) && aoffset == boffset && aname == bname
671 var gobTests = []Time{
672 Date(0, 1, 2, 3, 4, 5, 6, UTC),
673 Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
674 Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
675 {}, // nil location
676 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
677 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
680 func TestTimeGob(t *testing.T) {
681 var b bytes.Buffer
682 enc := gob.NewEncoder(&b)
683 dec := gob.NewDecoder(&b)
684 for _, tt := range gobTests {
685 var gobtt Time
686 if err := enc.Encode(&tt); err != nil {
687 t.Errorf("%v gob Encode error = %q, want nil", tt, err)
688 } else if err := dec.Decode(&gobtt); err != nil {
689 t.Errorf("%v gob Decode error = %q, want nil", tt, err)
690 } else if !equalTimeAndZone(gobtt, tt) {
691 t.Errorf("Decoded time = %v, want %v", gobtt, tt)
693 b.Reset()
697 var invalidEncodingTests = []struct {
698 bytes []byte
699 want string
701 {[]byte{}, "Time.UnmarshalBinary: no data"},
702 {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"},
703 {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"},
706 func TestInvalidTimeGob(t *testing.T) {
707 for _, tt := range invalidEncodingTests {
708 var ignored Time
709 err := ignored.GobDecode(tt.bytes)
710 if err == nil || err.Error() != tt.want {
711 t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.bytes, err, tt.want)
713 err = ignored.UnmarshalBinary(tt.bytes)
714 if err == nil || err.Error() != tt.want {
715 t.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v", tt.bytes, err, tt.want)
720 var notEncodableTimes = []struct {
721 time Time
722 want string
724 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.MarshalBinary: zone offset has fractional minute"},
725 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: unexpected zone offset"},
726 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinary: unexpected zone offset"},
727 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary: unexpected zone offset"},
730 func TestNotGobEncodableTime(t *testing.T) {
731 for _, tt := range notEncodableTimes {
732 _, err := tt.time.GobEncode()
733 if err == nil || err.Error() != tt.want {
734 t.Errorf("%v GobEncode error = %v, want %v", tt.time, err, tt.want)
736 _, err = tt.time.MarshalBinary()
737 if err == nil || err.Error() != tt.want {
738 t.Errorf("%v MarshalBinary error = %v, want %v", tt.time, err, tt.want)
743 var jsonTests = []struct {
744 time Time
745 json string
747 {Date(9999, 4, 12, 23, 20, 50, 520*1e6, UTC), `"9999-04-12T23:20:50.52Z"`},
748 {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`},
749 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`},
752 func TestTimeJSON(t *testing.T) {
753 for _, tt := range jsonTests {
754 var jsonTime Time
756 if jsonBytes, err := json.Marshal(tt.time); err != nil {
757 t.Errorf("%v json.Marshal error = %v, want nil", tt.time, err)
758 } else if string(jsonBytes) != tt.json {
759 t.Errorf("%v JSON = %#q, want %#q", tt.time, string(jsonBytes), tt.json)
760 } else if err = json.Unmarshal(jsonBytes, &jsonTime); err != nil {
761 t.Errorf("%v json.Unmarshal error = %v, want nil", tt.time, err)
762 } else if !equalTimeAndZone(jsonTime, tt.time) {
763 t.Errorf("Unmarshaled time = %v, want %v", jsonTime, tt.time)
768 func TestInvalidTimeJSON(t *testing.T) {
769 var tt Time
770 err := json.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt)
771 _, isParseErr := err.(*ParseError)
772 if !isParseErr {
773 t.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err)
777 var notJSONEncodableTimes = []struct {
778 time Time
779 want string
781 {Date(10000, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
782 {Date(-1, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
785 func TestNotJSONEncodableTime(t *testing.T) {
786 for _, tt := range notJSONEncodableTimes {
787 _, err := tt.time.MarshalJSON()
788 if err == nil || err.Error() != tt.want {
789 t.Errorf("%v MarshalJSON error = %v, want %v", tt.time, err, tt.want)
794 var parseDurationTests = []struct {
795 in string
796 ok bool
797 want Duration
799 // simple
800 {"0", true, 0},
801 {"5s", true, 5 * Second},
802 {"30s", true, 30 * Second},
803 {"1478s", true, 1478 * Second},
804 // sign
805 {"-5s", true, -5 * Second},
806 {"+5s", true, 5 * Second},
807 {"-0", true, 0},
808 {"+0", true, 0},
809 // decimal
810 {"5.0s", true, 5 * Second},
811 {"5.6s", true, 5*Second + 600*Millisecond},
812 {"5.s", true, 5 * Second},
813 {".5s", true, 500 * Millisecond},
814 {"1.0s", true, 1 * Second},
815 {"1.00s", true, 1 * Second},
816 {"1.004s", true, 1*Second + 4*Millisecond},
817 {"1.0040s", true, 1*Second + 4*Millisecond},
818 {"100.00100s", true, 100*Second + 1*Millisecond},
819 // different units
820 {"10ns", true, 10 * Nanosecond},
821 {"11us", true, 11 * Microsecond},
822 {"12µs", true, 12 * Microsecond}, // U+00B5
823 {"12μs", true, 12 * Microsecond}, // U+03BC
824 {"13ms", true, 13 * Millisecond},
825 {"14s", true, 14 * Second},
826 {"15m", true, 15 * Minute},
827 {"16h", true, 16 * Hour},
828 // composite durations
829 {"3h30m", true, 3*Hour + 30*Minute},
830 {"10.5s4m", true, 4*Minute + 10*Second + 500*Millisecond},
831 {"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)},
832 {"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
833 {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
834 // large value
835 {"52763797000ns", true, 52763797000 * Nanosecond},
836 // more than 9 digits after decimal point, see https://golang.org/issue/6617
837 {"0.3333333333333333333h", true, 20 * Minute},
838 // 9007199254740993 = 1<<53+1 cannot be stored precisely in a float64
839 {"9007199254740993ns", true, (1<<53 + 1) * Nanosecond},
840 // largest duration that can be represented by int64 in nanoseconds
841 {"9223372036854775807ns", true, (1<<63 - 1) * Nanosecond},
842 {"9223372036854775.807us", true, (1<<63 - 1) * Nanosecond},
843 {"9223372036s854ms775us807ns", true, (1<<63 - 1) * Nanosecond},
844 // large negative value
845 {"-9223372036854775807ns", true, -1<<63 + 1*Nanosecond},
846 // huge string; issue 15011.
847 {"0.100000000000000000000h", true, 6 * Minute},
848 // This value tests the first overflow check in leadingFraction.
849 {"0.830103483285477580700h", true, 49*Minute + 48*Second + 372539827*Nanosecond},
851 // errors
852 {"", false, 0},
853 {"3", false, 0},
854 {"-", false, 0},
855 {"s", false, 0},
856 {".", false, 0},
857 {"-.", false, 0},
858 {".s", false, 0},
859 {"+.s", false, 0},
860 {"3000000h", false, 0}, // overflow
861 {"9223372036854775808ns", false, 0}, // overflow
862 {"9223372036854775.808us", false, 0}, // overflow
863 {"9223372036854ms775us808ns", false, 0}, // overflow
864 // largest negative value of type int64 in nanoseconds should fail
865 // see https://go-review.googlesource.com/#/c/2461/
866 {"-9223372036854775808ns", false, 0},
869 func TestParseDuration(t *testing.T) {
870 for _, tc := range parseDurationTests {
871 d, err := ParseDuration(tc.in)
872 if tc.ok && (err != nil || d != tc.want) {
873 t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want)
874 } else if !tc.ok && err == nil {
875 t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in)
880 func TestParseDurationRoundTrip(t *testing.T) {
881 for i := 0; i < 100; i++ {
882 // Resolutions finer than milliseconds will result in
883 // imprecise round-trips.
884 d0 := Duration(rand.Int31()) * Millisecond
885 s := d0.String()
886 d1, err := ParseDuration(s)
887 if err != nil || d0 != d1 {
888 t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s, d1, err)
893 // golang.org/issue/4622
894 func TestLocationRace(t *testing.T) {
895 ResetLocalOnceForTest() // reset the Once to trigger the race
897 c := make(chan string, 1)
898 go func() {
899 c <- Now().String()
901 _ = Now().String()
903 Sleep(100 * Millisecond)
905 // Back to Los Angeles for subsequent tests:
906 ForceUSPacificForTesting()
909 var (
910 t Time
911 u int64
914 var mallocTest = []struct {
915 count int
916 desc string
917 fn func()
919 {0, `time.Now()`, func() { t = Now() }},
920 {0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }},
923 func TestCountMallocs(t *testing.T) {
924 if testing.Short() {
925 t.Skip("skipping malloc count in short mode")
927 if runtime.GOMAXPROCS(0) > 1 {
928 t.Skip("skipping; GOMAXPROCS>1")
930 for _, mt := range mallocTest {
931 allocs := int(testing.AllocsPerRun(100, mt.fn))
932 if allocs > mt.count {
933 t.Errorf("%s: %d allocs, want %d", mt.desc, allocs, mt.count)
938 func TestLoadFixed(t *testing.T) {
939 // Issue 4064: handle locations without any zone transitions.
940 loc, err := LoadLocation("Etc/GMT+1")
941 if err != nil {
942 t.Fatal(err)
945 // The tzdata name Etc/GMT+1 uses "east is negative",
946 // but Go and most other systems use "east is positive".
947 // So GMT+1 corresponds to -3600 in the Go zone, not +3600.
948 name, offset := Now().In(loc).Zone()
949 // The zone abbreviation is "-01" since tzdata-2016g, and "GMT+1"
950 // on earlier versions; we accept both. (Issue #17276).
951 if !(name == "GMT+1" || name == "-01") || offset != -1*60*60 {
952 t.Errorf("Now().In(loc).Zone() = %q, %d, want %q or %q, %d",
953 name, offset, "GMT+1", "-01", -1*60*60)
957 const (
958 minDuration Duration = -1 << 63
959 maxDuration Duration = 1<<63 - 1
962 var subTests = []struct {
963 t Time
964 u Time
965 d Duration
967 {Time{}, Time{}, Duration(0)},
968 {Date(2009, 11, 23, 0, 0, 0, 1, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), Duration(1)},
969 {Date(2009, 11, 23, 0, 0, 0, 0, UTC), Date(2009, 11, 24, 0, 0, 0, 0, UTC), -24 * Hour},
970 {Date(2009, 11, 24, 0, 0, 0, 0, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour},
971 {Date(-2009, 11, 24, 0, 0, 0, 0, UTC), Date(-2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour},
972 {Time{}, Date(2109, 11, 23, 0, 0, 0, 0, UTC), Duration(minDuration)},
973 {Date(2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(maxDuration)},
974 {Time{}, Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Duration(maxDuration)},
975 {Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(minDuration)},
976 {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},
977 {Date(2300, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), Duration(maxDuration)},
978 {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},
979 {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2300, 1, 1, 0, 0, 0, 0, UTC), Duration(minDuration)},
982 func TestSub(t *testing.T) {
983 for i, st := range subTests {
984 got := st.t.Sub(st.u)
985 if got != st.d {
986 t.Errorf("#%d: Sub(%v, %v): got %v; want %v", i, st.t, st.u, got, st.d)
991 var nsDurationTests = []struct {
992 d Duration
993 want int64
995 {Duration(-1000), -1000},
996 {Duration(-1), -1},
997 {Duration(1), 1},
998 {Duration(1000), 1000},
1001 func TestDurationNanoseconds(t *testing.T) {
1002 for _, tt := range nsDurationTests {
1003 if got := tt.d.Nanoseconds(); got != tt.want {
1004 t.Errorf("d.Nanoseconds() = %d; want: %d", got, tt.want)
1009 var secDurationTests = []struct {
1010 d Duration
1011 want float64
1013 {Duration(300000000), 0.3},
1016 func TestDurationSeconds(t *testing.T) {
1017 for _, tt := range secDurationTests {
1018 if got := tt.d.Seconds(); got != tt.want {
1019 t.Errorf("d.Seconds() = %g; want: %g", got, tt.want)
1024 var minDurationTests = []struct {
1025 d Duration
1026 want float64
1028 {Duration(-60000000000), -1},
1029 {Duration(-1), -1 / 60e9},
1030 {Duration(1), 1 / 60e9},
1031 {Duration(60000000000), 1},
1032 {Duration(3000), 5e-8},
1035 func TestDurationMinutes(t *testing.T) {
1036 for _, tt := range minDurationTests {
1037 if got := tt.d.Minutes(); got != tt.want {
1038 t.Errorf("d.Minutes() = %g; want: %g", got, tt.want)
1043 var hourDurationTests = []struct {
1044 d Duration
1045 want float64
1047 {Duration(-3600000000000), -1},
1048 {Duration(-1), -1 / 3600e9},
1049 {Duration(1), 1 / 3600e9},
1050 {Duration(3600000000000), 1},
1051 {Duration(36), 1e-11},
1054 func TestDurationHours(t *testing.T) {
1055 for _, tt := range hourDurationTests {
1056 if got := tt.d.Hours(); got != tt.want {
1057 t.Errorf("d.Hours() = %g; want: %g", got, tt.want)
1062 var durationTruncateTests = []struct {
1063 d Duration
1064 m Duration
1065 want Duration
1067 {0, Second, 0},
1068 {Minute, -7 * Second, Minute},
1069 {Minute, 0, Minute},
1070 {Minute, 1, Minute},
1071 {Minute + 10*Second, 10 * Second, Minute + 10*Second},
1072 {2*Minute + 10*Second, Minute, 2 * Minute},
1073 {10*Minute + 10*Second, 3 * Minute, 9 * Minute},
1074 {Minute + 10*Second, Minute + 10*Second + 1, 0},
1075 {Minute + 10*Second, Hour, 0},
1076 {-Minute, Second, -Minute},
1077 {-10 * Minute, 3 * Minute, -9 * Minute},
1078 {-10 * Minute, Hour, 0},
1081 func TestDurationTruncate(t *testing.T) {
1082 for _, tt := range durationTruncateTests {
1083 if got := tt.d.Truncate(tt.m); got != tt.want {
1084 t.Errorf("Duration(%s).Truncate(%s) = %s; want: %s", tt.d, tt.m, got, tt.want)
1089 var durationRoundTests = []struct {
1090 d Duration
1091 m Duration
1092 want Duration
1094 {0, Second, 0},
1095 {Minute, -11 * Second, Minute},
1096 {Minute, 0, Minute},
1097 {Minute, 1, Minute},
1098 {2 * Minute, Minute, 2 * Minute},
1099 {2*Minute + 10*Second, Minute, 2 * Minute},
1100 {2*Minute + 30*Second, Minute, 3 * Minute},
1101 {2*Minute + 50*Second, Minute, 3 * Minute},
1102 {-Minute, 1, -Minute},
1103 {-2 * Minute, Minute, -2 * Minute},
1104 {-2*Minute - 10*Second, Minute, -2 * Minute},
1105 {-2*Minute - 30*Second, Minute, -3 * Minute},
1106 {-2*Minute - 50*Second, Minute, -3 * Minute},
1107 {8e18, 3e18, 9e18},
1108 {9e18, 5e18, 1<<63 - 1},
1109 {-8e18, 3e18, -9e18},
1110 {-9e18, 5e18, -1 << 63},
1111 {3<<61 - 1, 3 << 61, 3 << 61},
1114 func TestDurationRound(t *testing.T) {
1115 for _, tt := range durationRoundTests {
1116 if got := tt.d.Round(tt.m); got != tt.want {
1117 t.Errorf("Duration(%s).Round(%s) = %s; want: %s", tt.d, tt.m, got, tt.want)
1122 var defaultLocTests = []struct {
1123 name string
1124 f func(t1, t2 Time) bool
1126 {"After", func(t1, t2 Time) bool { return t1.After(t2) == t2.After(t1) }},
1127 {"Before", func(t1, t2 Time) bool { return t1.Before(t2) == t2.Before(t1) }},
1128 {"Equal", func(t1, t2 Time) bool { return t1.Equal(t2) == t2.Equal(t1) }},
1130 {"IsZero", func(t1, t2 Time) bool { return t1.IsZero() == t2.IsZero() }},
1131 {"Date", func(t1, t2 Time) bool {
1132 a1, b1, c1 := t1.Date()
1133 a2, b2, c2 := t2.Date()
1134 return a1 == a2 && b1 == b2 && c1 == c2
1136 {"Year", func(t1, t2 Time) bool { return t1.Year() == t2.Year() }},
1137 {"Month", func(t1, t2 Time) bool { return t1.Month() == t2.Month() }},
1138 {"Day", func(t1, t2 Time) bool { return t1.Day() == t2.Day() }},
1139 {"Weekday", func(t1, t2 Time) bool { return t1.Weekday() == t2.Weekday() }},
1140 {"ISOWeek", func(t1, t2 Time) bool {
1141 a1, b1 := t1.ISOWeek()
1142 a2, b2 := t2.ISOWeek()
1143 return a1 == a2 && b1 == b2
1145 {"Clock", func(t1, t2 Time) bool {
1146 a1, b1, c1 := t1.Clock()
1147 a2, b2, c2 := t2.Clock()
1148 return a1 == a2 && b1 == b2 && c1 == c2
1150 {"Hour", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }},
1151 {"Minute", func(t1, t2 Time) bool { return t1.Minute() == t2.Minute() }},
1152 {"Second", func(t1, t2 Time) bool { return t1.Second() == t2.Second() }},
1153 {"Nanosecond", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }},
1154 {"YearDay", func(t1, t2 Time) bool { return t1.YearDay() == t2.YearDay() }},
1156 // Using Equal since Add don't modify loc using "==" will cause a fail
1157 {"Add", func(t1, t2 Time) bool { return t1.Add(Hour).Equal(t2.Add(Hour)) }},
1158 {"Sub", func(t1, t2 Time) bool { return t1.Sub(t2) == t2.Sub(t1) }},
1160 //Original caus for this test case bug 15852
1161 {"AddDate", func(t1, t2 Time) bool { return t1.AddDate(1991, 9, 3) == t2.AddDate(1991, 9, 3) }},
1163 {"UTC", func(t1, t2 Time) bool { return t1.UTC() == t2.UTC() }},
1164 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }},
1165 {"In", func(t1, t2 Time) bool { return t1.In(UTC) == t2.In(UTC) }},
1167 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }},
1168 {"Zone", func(t1, t2 Time) bool {
1169 a1, b1 := t1.Zone()
1170 a2, b2 := t2.Zone()
1171 return a1 == a2 && b1 == b2
1174 {"Unix", func(t1, t2 Time) bool { return t1.Unix() == t2.Unix() }},
1175 {"UnixNano", func(t1, t2 Time) bool { return t1.UnixNano() == t2.UnixNano() }},
1177 {"MarshalBinary", func(t1, t2 Time) bool {
1178 a1, b1 := t1.MarshalBinary()
1179 a2, b2 := t2.MarshalBinary()
1180 return bytes.Equal(a1, a2) && b1 == b2
1182 {"GobEncode", func(t1, t2 Time) bool {
1183 a1, b1 := t1.GobEncode()
1184 a2, b2 := t2.GobEncode()
1185 return bytes.Equal(a1, a2) && b1 == b2
1187 {"MarshalJSON", func(t1, t2 Time) bool {
1188 a1, b1 := t1.MarshalJSON()
1189 a2, b2 := t2.MarshalJSON()
1190 return bytes.Equal(a1, a2) && b1 == b2
1192 {"MarshalText", func(t1, t2 Time) bool {
1193 a1, b1 := t1.MarshalText()
1194 a2, b2 := t2.MarshalText()
1195 return bytes.Equal(a1, a2) && b1 == b2
1198 {"Truncate", func(t1, t2 Time) bool { return t1.Truncate(Hour).Equal(t2.Truncate(Hour)) }},
1199 {"Round", func(t1, t2 Time) bool { return t1.Round(Hour).Equal(t2.Round(Hour)) }},
1201 {"== Time{}", func(t1, t2 Time) bool { return (t1 == Time{}) == (t2 == Time{}) }},
1204 func TestDefaultLoc(t *testing.T) {
1205 //This test verifyes that all Time's methods behaves identical if loc is set
1206 //as nil or UTC
1207 for _, tt := range defaultLocTests {
1208 t1 := Time{}
1209 t2 := Time{}.UTC()
1210 if !tt.f(t1, t2) {
1211 t.Errorf("Time{} and Time{}.UTC() behave differently for %s", tt.name)
1216 func BenchmarkNow(b *testing.B) {
1217 for i := 0; i < b.N; i++ {
1218 t = Now()
1222 func BenchmarkNowUnixNano(b *testing.B) {
1223 for i := 0; i < b.N; i++ {
1224 u = Now().UnixNano()
1228 func BenchmarkFormat(b *testing.B) {
1229 t := Unix(1265346057, 0)
1230 for i := 0; i < b.N; i++ {
1231 t.Format("Mon Jan 2 15:04:05 2006")
1235 func BenchmarkFormatNow(b *testing.B) {
1236 // Like BenchmarkFormat, but easier, because the time zone
1237 // lookup cache is optimized for the present.
1238 t := Now()
1239 for i := 0; i < b.N; i++ {
1240 t.Format("Mon Jan 2 15:04:05 2006")
1244 func BenchmarkMarshalJSON(b *testing.B) {
1245 t := Now()
1246 for i := 0; i < b.N; i++ {
1247 t.MarshalJSON()
1251 func BenchmarkMarshalText(b *testing.B) {
1252 t := Now()
1253 for i := 0; i < b.N; i++ {
1254 t.MarshalText()
1258 func BenchmarkParse(b *testing.B) {
1259 for i := 0; i < b.N; i++ {
1260 Parse(ANSIC, "Mon Jan 2 15:04:05 2006")
1264 func BenchmarkParseDuration(b *testing.B) {
1265 for i := 0; i < b.N; i++ {
1266 ParseDuration("9007199254.740993ms")
1267 ParseDuration("9007199254740993ns")
1271 func BenchmarkHour(b *testing.B) {
1272 t := Now()
1273 for i := 0; i < b.N; i++ {
1274 _ = t.Hour()
1278 func BenchmarkSecond(b *testing.B) {
1279 t := Now()
1280 for i := 0; i < b.N; i++ {
1281 _ = t.Second()
1285 func BenchmarkYear(b *testing.B) {
1286 t := Now()
1287 for i := 0; i < b.N; i++ {
1288 _ = t.Year()
1292 func BenchmarkDay(b *testing.B) {
1293 t := Now()
1294 for i := 0; i < b.N; i++ {
1295 _ = t.Day()
1299 func TestMarshalBinaryZeroTime(t *testing.T) {
1300 t0 := Time{}
1301 enc, err := t0.MarshalBinary()
1302 if err != nil {
1303 t.Fatal(err)
1305 t1 := Now() // not zero
1306 if err := t1.UnmarshalBinary(enc); err != nil {
1307 t.Fatal(err)
1309 if t1 != t0 {
1310 t.Errorf("t0=%#v\nt1=%#v\nwant identical structures", t0, t1)
1314 // Issue 17720: Zero value of time.Month fails to print
1315 func TestZeroMonthString(t *testing.T) {
1316 if got, want := Month(0).String(), "%!Month(0)"; got != want {
1317 t.Errorf("zero month = %q; want %q", got, want)
1321 func TestReadFileLimit(t *testing.T) {
1322 const zero = "/dev/zero"
1323 if _, err := os.Stat(zero); err != nil {
1324 t.Skip("skipping test without a /dev/zero")
1326 _, err := ReadFile(zero)
1327 if err == nil || !strings.Contains(err.Error(), "is too large") {
1328 t.Errorf("readFile(%q) error = %v; want error containing 'is too large'", zero, err)