Black Lives Matter. Support the Equal Justice Initiative.

Source file src/time/time_test.go

Documentation: time

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

View as plain text