* doc/xml/manual/profile_mode.xml: Update three ieeexplore.ieee.org
[official-gcc.git] / libgo / go / time / time_test.go
blobfd464c0b18402d5cac529a4e053be540142b283e
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
578 {2012, 12, 24, 0, 0, 0, 0, Local, 1356336000}, // Leap year
580 // Many names for Fri Nov 18 7:56:35 PST 2011
581 {2011, 11, 18, 7, 56, 35, 0, Local, 1321631795}, // Nov 18 7:56:35
582 {2011, 11, 19, -17, 56, 35, 0, Local, 1321631795}, // Nov 19 -17:56:35
583 {2011, 11, 17, 31, 56, 35, 0, Local, 1321631795}, // Nov 17 31:56:35
584 {2011, 11, 18, 6, 116, 35, 0, Local, 1321631795}, // Nov 18 6:116:35
585 {2011, 10, 49, 7, 56, 35, 0, Local, 1321631795}, // Oct 49 7:56:35
586 {2011, 11, 18, 7, 55, 95, 0, Local, 1321631795}, // Nov 18 7:55:95
587 {2011, 11, 18, 7, 56, 34, 1e9, Local, 1321631795}, // Nov 18 7:56:34 + 10⁹ns
588 {2011, 12, -12, 7, 56, 35, 0, Local, 1321631795}, // Dec -21 7:56:35
589 {2012, 1, -43, 7, 56, 35, 0, Local, 1321631795}, // Jan -52 7:56:35 2012
590 {2012, int(January - 2), 18, 7, 56, 35, 0, Local, 1321631795}, // (Jan-2) 18 7:56:35 2012
591 {2010, int(December + 11), 18, 7, 56, 35, 0, Local, 1321631795}, // (Dec+11) 18 7:56:35 2010
594 func TestDate(t *testing.T) {
595 for _, tt := range dateTests {
596 time := Date(tt.year, Month(tt.month), tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z)
597 want := Unix(tt.unix, 0)
598 if !time.Equal(want) {
599 t.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v",
600 tt.year, tt.month, tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z,
601 time, want)
606 // Several ways of getting from
607 // Fri Nov 18 7:56:35 PST 2011
608 // to
609 // Thu Mar 19 7:56:35 PST 2016
610 var addDateTests = []struct {
611 years, months, days int
613 {4, 4, 1},
614 {3, 16, 1},
615 {3, 15, 30},
616 {5, -6, -18 - 30 - 12},
619 func TestAddDate(t *testing.T) {
620 t0 := Date(2011, 11, 18, 7, 56, 35, 0, UTC)
621 t1 := Date(2016, 3, 19, 7, 56, 35, 0, UTC)
622 for _, at := range addDateTests {
623 time := t0.AddDate(at.years, at.months, at.days)
624 if !time.Equal(t1) {
625 t.Errorf("AddDate(%d, %d, %d) = %v, want %v",
626 at.years, at.months, at.days,
627 time, t1)
632 var daysInTests = []struct {
633 year, month, di int
635 {2011, 1, 31}, // January, first month, 31 days
636 {2011, 2, 28}, // February, non-leap year, 28 days
637 {2012, 2, 29}, // February, leap year, 29 days
638 {2011, 6, 30}, // June, 30 days
639 {2011, 12, 31}, // December, last month, 31 days
642 func TestDaysIn(t *testing.T) {
643 // The daysIn function is not exported.
644 // Test the daysIn function via the `var DaysIn = daysIn`
645 // statement in the internal_test.go file.
646 for _, tt := range daysInTests {
647 di := DaysIn(Month(tt.month), tt.year)
648 if di != tt.di {
649 t.Errorf("got %d; expected %d for %d-%02d",
650 di, tt.di, tt.year, tt.month)
655 func TestAddToExactSecond(t *testing.T) {
656 // Add an amount to the current time to round it up to the next exact second.
657 // This test checks that the nsec field still lies within the range [0, 999999999].
658 t1 := Now()
659 t2 := t1.Add(Second - Duration(t1.Nanosecond()))
660 sec := (t1.Second() + 1) % 60
661 if t2.Second() != sec || t2.Nanosecond() != 0 {
662 t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec)
666 func equalTimeAndZone(a, b Time) bool {
667 aname, aoffset := a.Zone()
668 bname, boffset := b.Zone()
669 return a.Equal(b) && aoffset == boffset && aname == bname
672 var gobTests = []Time{
673 Date(0, 1, 2, 3, 4, 5, 6, UTC),
674 Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
675 Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
676 {}, // nil location
677 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
678 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
681 func TestTimeGob(t *testing.T) {
682 var b bytes.Buffer
683 enc := gob.NewEncoder(&b)
684 dec := gob.NewDecoder(&b)
685 for _, tt := range gobTests {
686 var gobtt Time
687 if err := enc.Encode(&tt); err != nil {
688 t.Errorf("%v gob Encode error = %q, want nil", tt, err)
689 } else if err := dec.Decode(&gobtt); err != nil {
690 t.Errorf("%v gob Decode error = %q, want nil", tt, err)
691 } else if !equalTimeAndZone(gobtt, tt) {
692 t.Errorf("Decoded time = %v, want %v", gobtt, tt)
694 b.Reset()
698 var invalidEncodingTests = []struct {
699 bytes []byte
700 want string
702 {[]byte{}, "Time.UnmarshalBinary: no data"},
703 {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"},
704 {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"},
707 func TestInvalidTimeGob(t *testing.T) {
708 for _, tt := range invalidEncodingTests {
709 var ignored Time
710 err := ignored.GobDecode(tt.bytes)
711 if err == nil || err.Error() != tt.want {
712 t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.bytes, err, tt.want)
714 err = ignored.UnmarshalBinary(tt.bytes)
715 if err == nil || err.Error() != tt.want {
716 t.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v", tt.bytes, err, tt.want)
721 var notEncodableTimes = []struct {
722 time Time
723 want string
725 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.MarshalBinary: zone offset has fractional minute"},
726 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: unexpected zone offset"},
727 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinary: unexpected zone offset"},
728 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary: unexpected zone offset"},
731 func TestNotGobEncodableTime(t *testing.T) {
732 for _, tt := range notEncodableTimes {
733 _, err := tt.time.GobEncode()
734 if err == nil || err.Error() != tt.want {
735 t.Errorf("%v GobEncode error = %v, want %v", tt.time, err, tt.want)
737 _, err = tt.time.MarshalBinary()
738 if err == nil || err.Error() != tt.want {
739 t.Errorf("%v MarshalBinary error = %v, want %v", tt.time, err, tt.want)
744 var jsonTests = []struct {
745 time Time
746 json string
748 {Date(9999, 4, 12, 23, 20, 50, 520*1e6, UTC), `"9999-04-12T23:20:50.52Z"`},
749 {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`},
750 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`},
753 func TestTimeJSON(t *testing.T) {
754 for _, tt := range jsonTests {
755 var jsonTime Time
757 if jsonBytes, err := json.Marshal(tt.time); err != nil {
758 t.Errorf("%v json.Marshal error = %v, want nil", tt.time, err)
759 } else if string(jsonBytes) != tt.json {
760 t.Errorf("%v JSON = %#q, want %#q", tt.time, string(jsonBytes), tt.json)
761 } else if err = json.Unmarshal(jsonBytes, &jsonTime); err != nil {
762 t.Errorf("%v json.Unmarshal error = %v, want nil", tt.time, err)
763 } else if !equalTimeAndZone(jsonTime, tt.time) {
764 t.Errorf("Unmarshaled time = %v, want %v", jsonTime, tt.time)
769 func TestInvalidTimeJSON(t *testing.T) {
770 var tt Time
771 err := json.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt)
772 _, isParseErr := err.(*ParseError)
773 if !isParseErr {
774 t.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err)
778 var notJSONEncodableTimes = []struct {
779 time Time
780 want string
782 {Date(10000, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
783 {Date(-1, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
786 func TestNotJSONEncodableTime(t *testing.T) {
787 for _, tt := range notJSONEncodableTimes {
788 _, err := tt.time.MarshalJSON()
789 if err == nil || err.Error() != tt.want {
790 t.Errorf("%v MarshalJSON error = %v, want %v", tt.time, err, tt.want)
795 var parseDurationTests = []struct {
796 in string
797 ok bool
798 want Duration
800 // simple
801 {"0", true, 0},
802 {"5s", true, 5 * Second},
803 {"30s", true, 30 * Second},
804 {"1478s", true, 1478 * Second},
805 // sign
806 {"-5s", true, -5 * Second},
807 {"+5s", true, 5 * Second},
808 {"-0", true, 0},
809 {"+0", true, 0},
810 // decimal
811 {"5.0s", true, 5 * Second},
812 {"5.6s", true, 5*Second + 600*Millisecond},
813 {"5.s", true, 5 * Second},
814 {".5s", true, 500 * Millisecond},
815 {"1.0s", true, 1 * Second},
816 {"1.00s", true, 1 * Second},
817 {"1.004s", true, 1*Second + 4*Millisecond},
818 {"1.0040s", true, 1*Second + 4*Millisecond},
819 {"100.00100s", true, 100*Second + 1*Millisecond},
820 // different units
821 {"10ns", true, 10 * Nanosecond},
822 {"11us", true, 11 * Microsecond},
823 {"12µs", true, 12 * Microsecond}, // U+00B5
824 {"12μs", true, 12 * Microsecond}, // U+03BC
825 {"13ms", true, 13 * Millisecond},
826 {"14s", true, 14 * Second},
827 {"15m", true, 15 * Minute},
828 {"16h", true, 16 * Hour},
829 // composite durations
830 {"3h30m", true, 3*Hour + 30*Minute},
831 {"10.5s4m", true, 4*Minute + 10*Second + 500*Millisecond},
832 {"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)},
833 {"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
834 {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
835 // large value
836 {"52763797000ns", true, 52763797000 * Nanosecond},
837 // more than 9 digits after decimal point, see https://golang.org/issue/6617
838 {"0.3333333333333333333h", true, 20 * Minute},
839 // 9007199254740993 = 1<<53+1 cannot be stored precisely in a float64
840 {"9007199254740993ns", true, (1<<53 + 1) * Nanosecond},
841 // largest duration that can be represented by int64 in nanoseconds
842 {"9223372036854775807ns", true, (1<<63 - 1) * Nanosecond},
843 {"9223372036854775.807us", true, (1<<63 - 1) * Nanosecond},
844 {"9223372036s854ms775us807ns", true, (1<<63 - 1) * Nanosecond},
845 // large negative value
846 {"-9223372036854775807ns", true, -1<<63 + 1*Nanosecond},
847 // huge string; issue 15011.
848 {"0.100000000000000000000h", true, 6 * Minute},
849 // This value tests the first overflow check in leadingFraction.
850 {"0.830103483285477580700h", true, 49*Minute + 48*Second + 372539827*Nanosecond},
852 // errors
853 {"", false, 0},
854 {"3", false, 0},
855 {"-", false, 0},
856 {"s", false, 0},
857 {".", false, 0},
858 {"-.", false, 0},
859 {".s", false, 0},
860 {"+.s", false, 0},
861 {"3000000h", false, 0}, // overflow
862 {"9223372036854775808ns", false, 0}, // overflow
863 {"9223372036854775.808us", false, 0}, // overflow
864 {"9223372036854ms775us808ns", false, 0}, // overflow
865 // largest negative value of type int64 in nanoseconds should fail
866 // see https://go-review.googlesource.com/#/c/2461/
867 {"-9223372036854775808ns", false, 0},
870 func TestParseDuration(t *testing.T) {
871 for _, tc := range parseDurationTests {
872 d, err := ParseDuration(tc.in)
873 if tc.ok && (err != nil || d != tc.want) {
874 t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want)
875 } else if !tc.ok && err == nil {
876 t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in)
881 func TestParseDurationRoundTrip(t *testing.T) {
882 for i := 0; i < 100; i++ {
883 // Resolutions finer than milliseconds will result in
884 // imprecise round-trips.
885 d0 := Duration(rand.Int31()) * Millisecond
886 s := d0.String()
887 d1, err := ParseDuration(s)
888 if err != nil || d0 != d1 {
889 t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s, d1, err)
894 // golang.org/issue/4622
895 func TestLocationRace(t *testing.T) {
896 ResetLocalOnceForTest() // reset the Once to trigger the race
898 c := make(chan string, 1)
899 go func() {
900 c <- Now().String()
902 _ = Now().String()
904 Sleep(100 * Millisecond)
906 // Back to Los Angeles for subsequent tests:
907 ForceUSPacificForTesting()
910 var (
911 t Time
912 u int64
915 var mallocTest = []struct {
916 count int
917 desc string
918 fn func()
920 {0, `time.Now()`, func() { t = Now() }},
921 {0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }},
924 func TestCountMallocs(t *testing.T) {
925 if testing.Short() {
926 t.Skip("skipping malloc count in short mode")
928 if runtime.GOMAXPROCS(0) > 1 {
929 t.Skip("skipping; GOMAXPROCS>1")
931 for _, mt := range mallocTest {
932 allocs := int(testing.AllocsPerRun(100, mt.fn))
933 if allocs > mt.count {
934 t.Errorf("%s: %d allocs, want %d", mt.desc, allocs, mt.count)
939 func TestLoadFixed(t *testing.T) {
940 // Issue 4064: handle locations without any zone transitions.
941 loc, err := LoadLocation("Etc/GMT+1")
942 if err != nil {
943 t.Fatal(err)
946 // The tzdata name Etc/GMT+1 uses "east is negative",
947 // but Go and most other systems use "east is positive".
948 // So GMT+1 corresponds to -3600 in the Go zone, not +3600.
949 name, offset := Now().In(loc).Zone()
950 // The zone abbreviation is "-01" since tzdata-2016g, and "GMT+1"
951 // on earlier versions; we accept both. (Issue #17276).
952 if !(name == "GMT+1" || name == "-01") || offset != -1*60*60 {
953 t.Errorf("Now().In(loc).Zone() = %q, %d, want %q or %q, %d",
954 name, offset, "GMT+1", "-01", -1*60*60)
958 const (
959 minDuration Duration = -1 << 63
960 maxDuration Duration = 1<<63 - 1
963 var subTests = []struct {
964 t Time
965 u Time
966 d Duration
968 {Time{}, Time{}, Duration(0)},
969 {Date(2009, 11, 23, 0, 0, 0, 1, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), Duration(1)},
970 {Date(2009, 11, 23, 0, 0, 0, 0, UTC), Date(2009, 11, 24, 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 {Date(-2009, 11, 24, 0, 0, 0, 0, UTC), Date(-2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour},
973 {Time{}, Date(2109, 11, 23, 0, 0, 0, 0, UTC), Duration(minDuration)},
974 {Date(2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(maxDuration)},
975 {Time{}, Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Duration(maxDuration)},
976 {Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(minDuration)},
977 {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},
978 {Date(2300, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), Duration(maxDuration)},
979 {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},
980 {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2300, 1, 1, 0, 0, 0, 0, UTC), Duration(minDuration)},
983 func TestSub(t *testing.T) {
984 for i, st := range subTests {
985 got := st.t.Sub(st.u)
986 if got != st.d {
987 t.Errorf("#%d: Sub(%v, %v): got %v; want %v", i, st.t, st.u, got, st.d)
992 var nsDurationTests = []struct {
993 d Duration
994 want int64
996 {Duration(-1000), -1000},
997 {Duration(-1), -1},
998 {Duration(1), 1},
999 {Duration(1000), 1000},
1002 func TestDurationNanoseconds(t *testing.T) {
1003 for _, tt := range nsDurationTests {
1004 if got := tt.d.Nanoseconds(); got != tt.want {
1005 t.Errorf("d.Nanoseconds() = %d; want: %d", got, tt.want)
1010 var secDurationTests = []struct {
1011 d Duration
1012 want float64
1014 {Duration(300000000), 0.3},
1017 func TestDurationSeconds(t *testing.T) {
1018 for _, tt := range secDurationTests {
1019 if got := tt.d.Seconds(); got != tt.want {
1020 t.Errorf("d.Seconds() = %g; want: %g", got, tt.want)
1025 var minDurationTests = []struct {
1026 d Duration
1027 want float64
1029 {Duration(-60000000000), -1},
1030 {Duration(-1), -1 / 60e9},
1031 {Duration(1), 1 / 60e9},
1032 {Duration(60000000000), 1},
1033 {Duration(3000), 5e-8},
1036 func TestDurationMinutes(t *testing.T) {
1037 for _, tt := range minDurationTests {
1038 if got := tt.d.Minutes(); got != tt.want {
1039 t.Errorf("d.Minutes() = %g; want: %g", got, tt.want)
1044 var hourDurationTests = []struct {
1045 d Duration
1046 want float64
1048 {Duration(-3600000000000), -1},
1049 {Duration(-1), -1 / 3600e9},
1050 {Duration(1), 1 / 3600e9},
1051 {Duration(3600000000000), 1},
1052 {Duration(36), 1e-11},
1055 func TestDurationHours(t *testing.T) {
1056 for _, tt := range hourDurationTests {
1057 if got := tt.d.Hours(); got != tt.want {
1058 t.Errorf("d.Hours() = %g; want: %g", got, tt.want)
1063 var durationTruncateTests = []struct {
1064 d Duration
1065 m Duration
1066 want Duration
1068 {0, Second, 0},
1069 {Minute, -7 * Second, Minute},
1070 {Minute, 0, Minute},
1071 {Minute, 1, Minute},
1072 {Minute + 10*Second, 10 * Second, Minute + 10*Second},
1073 {2*Minute + 10*Second, Minute, 2 * Minute},
1074 {10*Minute + 10*Second, 3 * Minute, 9 * Minute},
1075 {Minute + 10*Second, Minute + 10*Second + 1, 0},
1076 {Minute + 10*Second, Hour, 0},
1077 {-Minute, Second, -Minute},
1078 {-10 * Minute, 3 * Minute, -9 * Minute},
1079 {-10 * Minute, Hour, 0},
1082 func TestDurationTruncate(t *testing.T) {
1083 for _, tt := range durationTruncateTests {
1084 if got := tt.d.Truncate(tt.m); got != tt.want {
1085 t.Errorf("Duration(%s).Truncate(%s) = %s; want: %s", tt.d, tt.m, got, tt.want)
1090 var durationRoundTests = []struct {
1091 d Duration
1092 m Duration
1093 want Duration
1095 {0, Second, 0},
1096 {Minute, -11 * Second, Minute},
1097 {Minute, 0, Minute},
1098 {Minute, 1, Minute},
1099 {2 * Minute, Minute, 2 * Minute},
1100 {2*Minute + 10*Second, Minute, 2 * Minute},
1101 {2*Minute + 30*Second, Minute, 3 * Minute},
1102 {2*Minute + 50*Second, Minute, 3 * Minute},
1103 {-Minute, 1, -Minute},
1104 {-2 * Minute, Minute, -2 * Minute},
1105 {-2*Minute - 10*Second, Minute, -2 * Minute},
1106 {-2*Minute - 30*Second, Minute, -3 * Minute},
1107 {-2*Minute - 50*Second, Minute, -3 * Minute},
1108 {8e18, 3e18, 9e18},
1109 {9e18, 5e18, 1<<63 - 1},
1110 {-8e18, 3e18, -9e18},
1111 {-9e18, 5e18, -1 << 63},
1112 {3<<61 - 1, 3 << 61, 3 << 61},
1115 func TestDurationRound(t *testing.T) {
1116 for _, tt := range durationRoundTests {
1117 if got := tt.d.Round(tt.m); got != tt.want {
1118 t.Errorf("Duration(%s).Round(%s) = %s; want: %s", tt.d, tt.m, got, tt.want)
1123 var defaultLocTests = []struct {
1124 name string
1125 f func(t1, t2 Time) bool
1127 {"After", func(t1, t2 Time) bool { return t1.After(t2) == t2.After(t1) }},
1128 {"Before", func(t1, t2 Time) bool { return t1.Before(t2) == t2.Before(t1) }},
1129 {"Equal", func(t1, t2 Time) bool { return t1.Equal(t2) == t2.Equal(t1) }},
1131 {"IsZero", func(t1, t2 Time) bool { return t1.IsZero() == t2.IsZero() }},
1132 {"Date", func(t1, t2 Time) bool {
1133 a1, b1, c1 := t1.Date()
1134 a2, b2, c2 := t2.Date()
1135 return a1 == a2 && b1 == b2 && c1 == c2
1137 {"Year", func(t1, t2 Time) bool { return t1.Year() == t2.Year() }},
1138 {"Month", func(t1, t2 Time) bool { return t1.Month() == t2.Month() }},
1139 {"Day", func(t1, t2 Time) bool { return t1.Day() == t2.Day() }},
1140 {"Weekday", func(t1, t2 Time) bool { return t1.Weekday() == t2.Weekday() }},
1141 {"ISOWeek", func(t1, t2 Time) bool {
1142 a1, b1 := t1.ISOWeek()
1143 a2, b2 := t2.ISOWeek()
1144 return a1 == a2 && b1 == b2
1146 {"Clock", func(t1, t2 Time) bool {
1147 a1, b1, c1 := t1.Clock()
1148 a2, b2, c2 := t2.Clock()
1149 return a1 == a2 && b1 == b2 && c1 == c2
1151 {"Hour", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }},
1152 {"Minute", func(t1, t2 Time) bool { return t1.Minute() == t2.Minute() }},
1153 {"Second", func(t1, t2 Time) bool { return t1.Second() == t2.Second() }},
1154 {"Nanosecond", func(t1, t2 Time) bool { return t1.Hour() == t2.Hour() }},
1155 {"YearDay", func(t1, t2 Time) bool { return t1.YearDay() == t2.YearDay() }},
1157 // Using Equal since Add don't modify loc using "==" will cause a fail
1158 {"Add", func(t1, t2 Time) bool { return t1.Add(Hour).Equal(t2.Add(Hour)) }},
1159 {"Sub", func(t1, t2 Time) bool { return t1.Sub(t2) == t2.Sub(t1) }},
1161 //Original caus for this test case bug 15852
1162 {"AddDate", func(t1, t2 Time) bool { return t1.AddDate(1991, 9, 3) == t2.AddDate(1991, 9, 3) }},
1164 {"UTC", func(t1, t2 Time) bool { return t1.UTC() == t2.UTC() }},
1165 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }},
1166 {"In", func(t1, t2 Time) bool { return t1.In(UTC) == t2.In(UTC) }},
1168 {"Local", func(t1, t2 Time) bool { return t1.Local() == t2.Local() }},
1169 {"Zone", func(t1, t2 Time) bool {
1170 a1, b1 := t1.Zone()
1171 a2, b2 := t2.Zone()
1172 return a1 == a2 && b1 == b2
1175 {"Unix", func(t1, t2 Time) bool { return t1.Unix() == t2.Unix() }},
1176 {"UnixNano", func(t1, t2 Time) bool { return t1.UnixNano() == t2.UnixNano() }},
1178 {"MarshalBinary", func(t1, t2 Time) bool {
1179 a1, b1 := t1.MarshalBinary()
1180 a2, b2 := t2.MarshalBinary()
1181 return bytes.Equal(a1, a2) && b1 == b2
1183 {"GobEncode", func(t1, t2 Time) bool {
1184 a1, b1 := t1.GobEncode()
1185 a2, b2 := t2.GobEncode()
1186 return bytes.Equal(a1, a2) && b1 == b2
1188 {"MarshalJSON", func(t1, t2 Time) bool {
1189 a1, b1 := t1.MarshalJSON()
1190 a2, b2 := t2.MarshalJSON()
1191 return bytes.Equal(a1, a2) && b1 == b2
1193 {"MarshalText", func(t1, t2 Time) bool {
1194 a1, b1 := t1.MarshalText()
1195 a2, b2 := t2.MarshalText()
1196 return bytes.Equal(a1, a2) && b1 == b2
1199 {"Truncate", func(t1, t2 Time) bool { return t1.Truncate(Hour).Equal(t2.Truncate(Hour)) }},
1200 {"Round", func(t1, t2 Time) bool { return t1.Round(Hour).Equal(t2.Round(Hour)) }},
1202 {"== Time{}", func(t1, t2 Time) bool { return (t1 == Time{}) == (t2 == Time{}) }},
1205 func TestDefaultLoc(t *testing.T) {
1206 // Verify that all of Time's methods behave identically if loc is set to
1207 // nil or UTC.
1208 for _, tt := range defaultLocTests {
1209 t1 := Time{}
1210 t2 := Time{}.UTC()
1211 if !tt.f(t1, t2) {
1212 t.Errorf("Time{} and Time{}.UTC() behave differently for %s", tt.name)
1217 func BenchmarkNow(b *testing.B) {
1218 for i := 0; i < b.N; i++ {
1219 t = Now()
1223 func BenchmarkNowUnixNano(b *testing.B) {
1224 for i := 0; i < b.N; i++ {
1225 u = Now().UnixNano()
1229 func BenchmarkFormat(b *testing.B) {
1230 t := Unix(1265346057, 0)
1231 for i := 0; i < b.N; i++ {
1232 t.Format("Mon Jan 2 15:04:05 2006")
1236 func BenchmarkFormatNow(b *testing.B) {
1237 // Like BenchmarkFormat, but easier, because the time zone
1238 // lookup cache is optimized for the present.
1239 t := Now()
1240 for i := 0; i < b.N; i++ {
1241 t.Format("Mon Jan 2 15:04:05 2006")
1245 func BenchmarkMarshalJSON(b *testing.B) {
1246 t := Now()
1247 for i := 0; i < b.N; i++ {
1248 t.MarshalJSON()
1252 func BenchmarkMarshalText(b *testing.B) {
1253 t := Now()
1254 for i := 0; i < b.N; i++ {
1255 t.MarshalText()
1259 func BenchmarkParse(b *testing.B) {
1260 for i := 0; i < b.N; i++ {
1261 Parse(ANSIC, "Mon Jan 2 15:04:05 2006")
1265 func BenchmarkParseDuration(b *testing.B) {
1266 for i := 0; i < b.N; i++ {
1267 ParseDuration("9007199254.740993ms")
1268 ParseDuration("9007199254740993ns")
1272 func BenchmarkHour(b *testing.B) {
1273 t := Now()
1274 for i := 0; i < b.N; i++ {
1275 _ = t.Hour()
1279 func BenchmarkSecond(b *testing.B) {
1280 t := Now()
1281 for i := 0; i < b.N; i++ {
1282 _ = t.Second()
1286 func BenchmarkYear(b *testing.B) {
1287 t := Now()
1288 for i := 0; i < b.N; i++ {
1289 _ = t.Year()
1293 func BenchmarkDay(b *testing.B) {
1294 t := Now()
1295 for i := 0; i < b.N; i++ {
1296 _ = t.Day()
1300 func TestMarshalBinaryZeroTime(t *testing.T) {
1301 t0 := Time{}
1302 enc, err := t0.MarshalBinary()
1303 if err != nil {
1304 t.Fatal(err)
1306 t1 := Now() // not zero
1307 if err := t1.UnmarshalBinary(enc); err != nil {
1308 t.Fatal(err)
1310 if t1 != t0 {
1311 t.Errorf("t0=%#v\nt1=%#v\nwant identical structures", t0, t1)
1315 // Issue 17720: Zero value of time.Month fails to print
1316 func TestZeroMonthString(t *testing.T) {
1317 if got, want := Month(0).String(), "%!Month(0)"; got != want {
1318 t.Errorf("zero month = %q; want %q", got, want)
1322 func TestReadFileLimit(t *testing.T) {
1323 const zero = "/dev/zero"
1324 if _, err := os.Stat(zero); err != nil {
1325 t.Skip("skipping test without a /dev/zero")
1327 _, err := ReadFile(zero)
1328 if err == nil || !strings.Contains(err.Error(), "is too large") {
1329 t.Errorf("readFile(%q) error = %v; want error containing 'is too large'", zero, err)