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.
24 // We should be in PST/PDT, but if the time zone files are missing we
25 // won't be. The purpose of this test is to at least explain why some of
26 // the subsequent tests fail.
27 func TestZoneData(t
*testing
.T
) {
29 // PST is 8 hours west, PDT is 7 hours west. We could use the name but it's not unique.
30 if name
, off
:= lt
.Zone(); off
!= -8*60*60 && off
!= -7*60*60 {
31 t
.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", name
, off
)
32 t
.Error("Likely problem: the time zone files have not been installed.")
36 // parsedTime is the struct representing a parsed time value.
37 type parsedTime
struct {
41 Hour
, Minute
, Second
int // 15:04:05 is 15, 4, 5.
42 Nanosecond
int // Fractional second.
44 ZoneOffset
int // seconds east of UTC, e.g. -7*60*60 for -0700
45 Zone
string // e.g., "MST"
48 type TimeTest
struct {
53 var utctests
= []TimeTest
{
54 {0, parsedTime
{1970, January
, 1, 0, 0, 0, 0, Thursday
, 0, "UTC"}},
55 {1221681866, parsedTime
{2008, September
, 17, 20, 4, 26, 0, Wednesday
, 0, "UTC"}},
56 {-1221681866, parsedTime
{1931, April
, 16, 3, 55, 34, 0, Thursday
, 0, "UTC"}},
57 {-11644473600, parsedTime
{1601, January
, 1, 0, 0, 0, 0, Monday
, 0, "UTC"}},
58 {599529660, parsedTime
{1988, December
, 31, 0, 1, 0, 0, Saturday
, 0, "UTC"}},
59 {978220860, parsedTime
{2000, December
, 31, 0, 1, 0, 0, Sunday
, 0, "UTC"}},
62 var nanoutctests
= []TimeTest
{
63 {0, parsedTime
{1970, January
, 1, 0, 0, 0, 1e8
, Thursday
, 0, "UTC"}},
64 {1221681866, parsedTime
{2008, September
, 17, 20, 4, 26, 2e8
, Wednesday
, 0, "UTC"}},
67 var localtests
= []TimeTest
{
68 {0, parsedTime
{1969, December
, 31, 16, 0, 0, 0, Wednesday
, -8 * 60 * 60, "PST"}},
69 {1221681866, parsedTime
{2008, September
, 17, 13, 4, 26, 0, Wednesday
, -7 * 60 * 60, "PDT"}},
72 var nanolocaltests
= []TimeTest
{
73 {0, parsedTime
{1969, December
, 31, 16, 0, 0, 1e8
, Wednesday
, -8 * 60 * 60, "PST"}},
74 {1221681866, parsedTime
{2008, September
, 17, 13, 4, 26, 3e8
, Wednesday
, -7 * 60 * 60, "PDT"}},
77 func same(t Time
, u
*parsedTime
) bool {
79 year
, month
, day
:= t
.Date()
80 hour
, min
, sec
:= t
.Clock()
81 name
, offset
:= t
.Zone()
82 if year
!= u
.Year || month
!= u
.Month || day
!= u
.Day ||
83 hour
!= u
.Hour || min
!= u
.Minute || sec
!= u
.Second ||
84 name
!= u
.Zone || offset
!= u
.ZoneOffset
{
87 // Check individual entries.
88 return t
.Year() == u
.Year
&&
89 t
.Month() == u
.Month
&&
92 t
.Minute() == u
.Minute
&&
93 t
.Second() == u
.Second
&&
94 t
.Nanosecond() == u
.Nanosecond
&&
95 t
.Weekday() == u
.Weekday
98 func TestSecondsToUTC(t
*testing
.T
) {
99 for _
, test
:= range utctests
{
101 golden
:= &test
.golden
102 tm
:= Unix(sec
, 0).UTC()
105 t
.Errorf("SecondsToUTC(%d).Seconds() = %d", sec
, newsec
)
107 if !same(tm
, golden
) {
108 t
.Errorf("SecondsToUTC(%d): // %#v", sec
, tm
)
109 t
.Errorf(" want=%+v", *golden
)
110 t
.Errorf(" have=%v", tm
.Format(RFC3339
+" MST"))
115 func TestNanosecondsToUTC(t
*testing
.T
) {
116 for _
, test
:= range nanoutctests
{
117 golden
:= &test
.golden
118 nsec
:= test
.seconds
*1e9
+ int64(golden
.Nanosecond
)
119 tm
:= Unix(0, nsec
).UTC()
120 newnsec
:= tm
.Unix()*1e9
+ int64(tm
.Nanosecond())
122 t
.Errorf("NanosecondsToUTC(%d).Nanoseconds() = %d", nsec
, newnsec
)
124 if !same(tm
, golden
) {
125 t
.Errorf("NanosecondsToUTC(%d):", nsec
)
126 t
.Errorf(" want=%+v", *golden
)
127 t
.Errorf(" have=%+v", tm
.Format(RFC3339
+" MST"))
132 func TestSecondsToLocalTime(t
*testing
.T
) {
133 for _
, test
:= range localtests
{
135 golden
:= &test
.golden
139 t
.Errorf("SecondsToLocalTime(%d).Seconds() = %d", sec
, newsec
)
141 if !same(tm
, golden
) {
142 t
.Errorf("SecondsToLocalTime(%d):", sec
)
143 t
.Errorf(" want=%+v", *golden
)
144 t
.Errorf(" have=%+v", tm
.Format(RFC3339
+" MST"))
149 func TestNanosecondsToLocalTime(t
*testing
.T
) {
150 for _
, test
:= range nanolocaltests
{
151 golden
:= &test
.golden
152 nsec
:= test
.seconds
*1e9
+ int64(golden
.Nanosecond
)
154 newnsec
:= tm
.Unix()*1e9
+ int64(tm
.Nanosecond())
156 t
.Errorf("NanosecondsToLocalTime(%d).Seconds() = %d", nsec
, newnsec
)
158 if !same(tm
, golden
) {
159 t
.Errorf("NanosecondsToLocalTime(%d):", nsec
)
160 t
.Errorf(" want=%+v", *golden
)
161 t
.Errorf(" have=%+v", tm
.Format(RFC3339
+" MST"))
166 func TestSecondsToUTCAndBack(t
*testing
.T
) {
167 f
:= func(sec
int64) bool { return Unix(sec
, 0).UTC().Unix() == sec
}
168 f32
:= func(sec
int32) bool { return f(int64(sec
)) }
169 cfg
:= &quick
.Config
{MaxCount
: 10000}
171 // Try a reasonable date first, then the huge ones.
172 if err
:= quick
.Check(f32
, cfg
); err
!= nil {
175 if err
:= quick
.Check(f
, cfg
); err
!= nil {
180 func TestNanosecondsToUTCAndBack(t
*testing
.T
) {
181 f
:= func(nsec
int64) bool {
182 t
:= Unix(0, nsec
).UTC()
183 ns
:= t
.Unix()*1e9
+ int64(t
.Nanosecond())
186 f32
:= func(nsec
int32) bool { return f(int64(nsec
)) }
187 cfg
:= &quick
.Config
{MaxCount
: 10000}
189 // Try a small date first, then the large ones. (The span is only a few hundred years
190 // for nanoseconds in an int64.)
191 if err
:= quick
.Check(f32
, cfg
); err
!= nil {
194 if err
:= quick
.Check(f
, cfg
); err
!= nil {
199 // The time routines provide no way to get absolute time
200 // (seconds since zero), but we need it to compute the right
201 // answer for bizarre roundings like "to the nearest 3 ns".
202 // Compute as t - year1 = (t - 1970) + (1970 - 2001) + (2001 - 1).
203 // t - 1970 is returned by Unix and Nanosecond.
204 // 1970 - 2001 is -(31*365+8)*86400 = -978307200 seconds.
205 // 2001 - 1 is 2000*365.2425*86400 = 63113904000 seconds.
206 const unixToZero
= -978307200 + 63113904000
208 // abs returns the absolute time stored in t, as seconds and nanoseconds.
209 func abs(t Time
) (sec
, nsec
int64) {
211 nano
:= t
.Nanosecond()
212 return unix
+ unixToZero
, int64(nano
)
215 // absString returns abs as a decimal string.
216 func absString(t Time
) string {
225 return fmt
.Sprintf("-%d%09d", sec
, nsec
)
227 return fmt
.Sprintf("%d%09d", sec
, nsec
)
230 var truncateRoundTests
= []struct {
234 {Date(-1, January
, 1, 12, 15, 30, 5e8
, UTC
), 3},
235 {Date(-1, January
, 1, 12, 15, 31, 5e8
, UTC
), 3},
236 {Date(2012, January
, 1, 12, 15, 30, 5e8
, UTC
), Second
},
237 {Date(2012, January
, 1, 12, 15, 31, 5e8
, UTC
), Second
},
238 {Unix(-19012425939, 649146258), 7435029458905025217}, // 5.8*d rounds to 6*d, but .8*d+.8*d < 0 < d
241 func TestTruncateRound(t
*testing
.T
) {
254 testOne
:= func(ti
, tns
, di
int64) bool {
255 t0
:= Unix(ti
, int64(tns
)).UTC()
264 // Compute bt = absolute nanoseconds.
271 // Compute quotient and remainder mod d.
272 bd
.SetInt64(int64(d
))
273 bq
.DivMod(bt
, bd
, br
)
275 // To truncate, subtract remainder.
276 // br is < d, so it fits in an int64.
278 t1
:= t0
.Add(-Duration(r
))
280 // Check that time.Truncate works.
281 if trunc
:= t0
.Truncate(d
); trunc
!= t1
{
282 t
.Errorf("Time.Truncate(%s, %s) = %s, want %s\n"+
283 "%v trunc %v =\n%v want\n%v",
284 t0
.Format(RFC3339Nano
), d
, trunc
, t1
.Format(RFC3339Nano
),
285 absString(t0
), int64(d
), absString(trunc
), absString(t1
))
289 // To round, add d back if remainder r > d/2 or r == exactly d/2.
290 // The commented out code would round half to even instead of up,
291 // but that makes it time-zone dependent, which is a bit strange.
292 if r
> int64(d
)/2 || r
+r
== int64(d
) /*&& bq.Bit(0) == 1*/ {
293 t1
= t1
.Add(Duration(d
))
296 // Check that time.Round works.
297 if rnd
:= t0
.Round(d
); rnd
!= t1
{
298 t
.Errorf("Time.Round(%s, %s) = %s, want %s\n"+
299 "%v round %v =\n%v want\n%v",
300 t0
.Format(RFC3339Nano
), d
, rnd
, t1
.Format(RFC3339Nano
),
301 absString(t0
), int64(d
), absString(rnd
), absString(t1
))
308 for _
, tt
:= range truncateRoundTests
{
309 testOne(tt
.t
.Unix(), int64(tt
.t
.Nanosecond()), int64(tt
.d
))
313 for i
:= 0; i
< 100; i
++ {
314 for j
:= 1; j
< 100; j
++ {
315 testOne(unixToZero
, int64(i
), int64(j
))
316 testOne(unixToZero
, -int64(i
), int64(j
))
327 // randomly generated test cases
328 cfg
:= &quick
.Config
{MaxCount
: 100000}
333 // divisors of Second
334 f1
:= func(ti
int64, tns
int32, logdi
int32) bool {
336 a
, b
:= uint(logdi%9
), (logdi
>>16)%9
338 for i
:= 0; i
< int(b
); i
++ {
341 return testOne(ti
, int64(tns
), int64(d
))
345 // multiples of Second
346 f2
:= func(ti
int64, tns
int32, di
int32) bool {
347 d
:= Duration(di
) * Second
351 return testOne(ti
, int64(tns
), int64(d
))
356 f3
:= func(tns
, di
int64) bool {
367 return testOne(0, tns
, di
)
372 f4
:= func(ti
int64, tns
int32, di
int64) bool {
373 return testOne(ti
, int64(tns
), di
)
378 type ISOWeekTest
struct {
380 month
, day
int // month and day
381 yex
int // expected year
382 wex
int // expected week
385 var isoWeekTests
= []ISOWeekTest
{
386 {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52},
387 {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1},
388 {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52},
389 {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1},
390 {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1},
391 {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2},
392 {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53},
393 {2000, 1, 1, 1999, 52}, {2001, 1, 1, 2001, 1}, {2002, 1, 1, 2002, 1},
394 {2003, 1, 1, 2003, 1}, {2004, 1, 1, 2004, 1}, {2005, 1, 1, 2004, 53},
395 {2006, 1, 1, 2005, 52}, {2007, 1, 1, 2007, 1}, {2008, 1, 1, 2008, 1},
396 {2009, 1, 1, 2009, 1}, {2010, 1, 1, 2009, 53}, {2010, 1, 1, 2009, 53},
397 {2011, 1, 1, 2010, 52}, {2011, 1, 2, 2010, 52}, {2011, 1, 3, 2011, 1},
398 {2011, 1, 4, 2011, 1}, {2011, 1, 5, 2011, 1}, {2011, 1, 6, 2011, 1},
399 {2011, 1, 7, 2011, 1}, {2011, 1, 8, 2011, 1}, {2011, 1, 9, 2011, 1},
400 {2011, 1, 10, 2011, 2}, {2011, 1, 11, 2011, 2}, {2011, 6, 12, 2011, 23},
401 {2011, 6, 13, 2011, 24}, {2011, 12, 25, 2011, 51}, {2011, 12, 26, 2011, 52},
402 {2011, 12, 27, 2011, 52}, {2011, 12, 28, 2011, 52}, {2011, 12, 29, 2011, 52},
403 {2011, 12, 30, 2011, 52}, {2011, 12, 31, 2011, 52}, {1995, 1, 1, 1994, 52},
404 {2012, 1, 1, 2011, 52}, {2012, 1, 2, 2012, 1}, {2012, 1, 8, 2012, 1},
405 {2012, 1, 9, 2012, 2}, {2012, 12, 23, 2012, 51}, {2012, 12, 24, 2012, 52},
406 {2012, 12, 30, 2012, 52}, {2012, 12, 31, 2013, 1}, {2013, 1, 1, 2013, 1},
407 {2013, 1, 6, 2013, 1}, {2013, 1, 7, 2013, 2}, {2013, 12, 22, 2013, 51},
408 {2013, 12, 23, 2013, 52}, {2013, 12, 29, 2013, 52}, {2013, 12, 30, 2014, 1},
409 {2014, 1, 1, 2014, 1}, {2014, 1, 5, 2014, 1}, {2014, 1, 6, 2014, 2},
410 {2015, 1, 1, 2015, 1}, {2016, 1, 1, 2015, 53}, {2017, 1, 1, 2016, 52},
411 {2018, 1, 1, 2018, 1}, {2019, 1, 1, 2019, 1}, {2020, 1, 1, 2020, 1},
412 {2021, 1, 1, 2020, 53}, {2022, 1, 1, 2021, 52}, {2023, 1, 1, 2022, 52},
413 {2024, 1, 1, 2024, 1}, {2025, 1, 1, 2025, 1}, {2026, 1, 1, 2026, 1},
414 {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1},
415 {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1},
416 {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1},
417 {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53},
418 {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52},
421 func TestISOWeek(t
*testing
.T
) {
422 // Selected dates and corner cases
423 for _
, wt
:= range isoWeekTests
{
424 dt
:= Date(wt
.year
, Month(wt
.month
), wt
.day
, 0, 0, 0, 0, UTC
)
426 if w
!= wt
.wex || y
!= wt
.yex
{
427 t
.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d",
428 y
, w
, wt
.yex
, wt
.wex
, wt
.year
, wt
.month
, wt
.day
)
432 // The only real invariant: Jan 04 is in week 1
433 for year
:= 1950; year
< 2100; year
++ {
434 if y
, w
:= Date(year
, January
, 4, 0, 0, 0, 0, UTC
).ISOWeek(); y
!= year || w
!= 1 {
435 t
.Errorf("got %d/%d; expected %d/1 for Jan 04", y
, w
, year
)
440 type YearDayTest
struct {
445 // Test YearDay in several different scenarios
447 var yearDayTests
= []YearDayTest
{
448 // Non-leap-year tests
468 // Looks like leap-year (but isn't) tests
478 // Year one tests (non-leap)
488 // Year minus one tests (non-leap)
498 // 400 BC tests (leap-year)
510 // Gregorian calendar change (no effect)
515 // Check to see if YearDay is location sensitive
516 var yearDayLocations
= []*Location
{
517 FixedZone("UTC-8", -8*60*60),
518 FixedZone("UTC-4", -4*60*60),
520 FixedZone("UTC+4", 4*60*60),
521 FixedZone("UTC+8", 8*60*60),
524 func TestYearDay(t
*testing
.T
) {
525 for _
, loc
:= range yearDayLocations
{
526 for _
, ydt
:= range yearDayTests
{
527 dt
:= Date(ydt
.year
, Month(ydt
.month
), ydt
.day
, 0, 0, 0, 0, loc
)
529 if yday
!= ydt
.yday
{
530 t
.Errorf("got %d, expected %d for %d-%02d-%02d in %v",
531 yday
, ydt
.yday
, ydt
.year
, ydt
.month
, ydt
.day
, loc
)
537 var durationTests
= []struct {
542 {"1ns", 1 * Nanosecond
},
543 {"1.1µs", 1100 * Nanosecond
},
544 {"2.2ms", 2200 * Microsecond
},
545 {"3.3s", 3300 * Millisecond
},
546 {"4m5s", 4*Minute
+ 5*Second
},
547 {"4m5.001s", 4*Minute
+ 5001*Millisecond
},
548 {"5h6m7.001s", 5*Hour
+ 6*Minute
+ 7001*Millisecond
},
549 {"8m0.000000001s", 8*Minute
+ 1*Nanosecond
},
550 {"2562047h47m16.854775807s", 1<<63 - 1},
551 {"-2562047h47m16.854775808s", -1 << 63},
554 func TestDurationString(t
*testing
.T
) {
555 for _
, tt
:= range durationTests
{
556 if str
:= tt
.d
.String(); str
!= tt
.str
{
557 t
.Errorf("Duration(%d).String() = %s, want %s", int64(tt
.d
), str
, tt
.str
)
560 if str
:= (-tt
.d
).String(); str
!= "-"+tt
.str
{
561 t
.Errorf("Duration(%d).String() = %s, want %s", int64(-tt
.d
), str
, "-"+tt
.str
)
567 var dateTests
= []struct {
568 year
, month
, day
, hour
, min
, sec
, nsec
int
572 {2011, 11, 6, 1, 0, 0, 0, Local
, 1320566400}, // 1:00:00 PDT
573 {2011, 11, 6, 1, 59, 59, 0, Local
, 1320569999}, // 1:59:59 PDT
574 {2011, 11, 6, 2, 0, 0, 0, Local
, 1320573600}, // 2:00:00 PST
576 {2011, 3, 13, 1, 0, 0, 0, Local
, 1300006800}, // 1:00:00 PST
577 {2011, 3, 13, 1, 59, 59, 0, Local
, 1300010399}, // 1:59:59 PST
578 {2011, 3, 13, 3, 0, 0, 0, Local
, 1300010400}, // 3:00:00 PDT
579 {2011, 3, 13, 2, 30, 0, 0, Local
, 1300008600}, // 2:30:00 PDT ≡ 1:30 PST
580 {2012, 12, 24, 0, 0, 0, 0, Local
, 1356336000}, // Leap year
582 // Many names for Fri Nov 18 7:56:35 PST 2011
583 {2011, 11, 18, 7, 56, 35, 0, Local
, 1321631795}, // Nov 18 7:56:35
584 {2011, 11, 19, -17, 56, 35, 0, Local
, 1321631795}, // Nov 19 -17:56:35
585 {2011, 11, 17, 31, 56, 35, 0, Local
, 1321631795}, // Nov 17 31:56:35
586 {2011, 11, 18, 6, 116, 35, 0, Local
, 1321631795}, // Nov 18 6:116:35
587 {2011, 10, 49, 7, 56, 35, 0, Local
, 1321631795}, // Oct 49 7:56:35
588 {2011, 11, 18, 7, 55, 95, 0, Local
, 1321631795}, // Nov 18 7:55:95
589 {2011, 11, 18, 7, 56, 34, 1e9
, Local
, 1321631795}, // Nov 18 7:56:34 + 10⁹ns
590 {2011, 12, -12, 7, 56, 35, 0, Local
, 1321631795}, // Dec -21 7:56:35
591 {2012, 1, -43, 7, 56, 35, 0, Local
, 1321631795}, // Jan -52 7:56:35 2012
592 {2012, int(January
- 2), 18, 7, 56, 35, 0, Local
, 1321631795}, // (Jan-2) 18 7:56:35 2012
593 {2010, int(December
+ 11), 18, 7, 56, 35, 0, Local
, 1321631795}, // (Dec+11) 18 7:56:35 2010
596 func TestDate(t
*testing
.T
) {
597 for _
, tt
:= range dateTests
{
598 time
:= Date(tt
.year
, Month(tt
.month
), tt
.day
, tt
.hour
, tt
.min
, tt
.sec
, tt
.nsec
, tt
.z
)
599 want
:= Unix(tt
.unix
, 0)
600 if !time
.Equal(want
) {
601 t
.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v",
602 tt
.year
, tt
.month
, tt
.day
, tt
.hour
, tt
.min
, tt
.sec
, tt
.nsec
, tt
.z
,
608 // Several ways of getting from
609 // Fri Nov 18 7:56:35 PST 2011
611 // Thu Mar 19 7:56:35 PST 2016
612 var addDateTests
= []struct {
613 years
, months
, days
int
618 {5, -6, -18 - 30 - 12},
621 func TestAddDate(t
*testing
.T
) {
622 t0
:= Date(2011, 11, 18, 7, 56, 35, 0, UTC
)
623 t1
:= Date(2016, 3, 19, 7, 56, 35, 0, UTC
)
624 for _
, at
:= range addDateTests
{
625 time
:= t0
.AddDate(at
.years
, at
.months
, at
.days
)
627 t
.Errorf("AddDate(%d, %d, %d) = %v, want %v",
628 at
.years
, at
.months
, at
.days
,
634 var daysInTests
= []struct {
637 {2011, 1, 31}, // January, first month, 31 days
638 {2011, 2, 28}, // February, non-leap year, 28 days
639 {2012, 2, 29}, // February, leap year, 29 days
640 {2011, 6, 30}, // June, 30 days
641 {2011, 12, 31}, // December, last month, 31 days
644 func TestDaysIn(t
*testing
.T
) {
645 // The daysIn function is not exported.
646 // Test the daysIn function via the `var DaysIn = daysIn`
647 // statement in the internal_test.go file.
648 for _
, tt
:= range daysInTests
{
649 di
:= DaysIn(Month(tt
.month
), tt
.year
)
651 t
.Errorf("got %d; expected %d for %d-%02d",
652 di
, tt
.di
, tt
.year
, tt
.month
)
657 func TestAddToExactSecond(t
*testing
.T
) {
658 // Add an amount to the current time to round it up to the next exact second.
659 // This test checks that the nsec field still lies within the range [0, 999999999].
661 t2
:= t1
.Add(Second
- Duration(t1
.Nanosecond()))
662 sec
:= (t1
.Second() + 1) % 60
663 if t2
.Second() != sec || t2
.Nanosecond() != 0 {
664 t
.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2
.Second(), t2
.Nanosecond(), sec
)
668 func equalTimeAndZone(a
, b Time
) bool {
669 aname
, aoffset
:= a
.Zone()
670 bname
, boffset
:= b
.Zone()
671 return a
.Equal(b
) && aoffset
== boffset
&& aname
== bname
674 var gobTests
= []Time
{
675 Date(0, 1, 2, 3, 4, 5, 6, UTC
),
676 Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
677 Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
679 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
680 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
683 func TestTimeGob(t
*testing
.T
) {
685 enc
:= gob
.NewEncoder(&b
)
686 dec
:= gob
.NewDecoder(&b
)
687 for _
, tt
:= range gobTests
{
689 if err
:= enc
.Encode(&tt
); err
!= nil {
690 t
.Errorf("%v gob Encode error = %q, want nil", tt
, err
)
691 } else if err
:= dec
.Decode(&gobtt
); err
!= nil {
692 t
.Errorf("%v gob Decode error = %q, want nil", tt
, err
)
693 } else if !equalTimeAndZone(gobtt
, tt
) {
694 t
.Errorf("Decoded time = %v, want %v", gobtt
, tt
)
700 var invalidEncodingTests
= []struct {
704 {[]byte{}, "Time.UnmarshalBinary: no data"},
705 {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"},
706 {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"},
709 func TestInvalidTimeGob(t
*testing
.T
) {
710 for _
, tt
:= range invalidEncodingTests
{
712 err
:= ignored
.GobDecode(tt
.bytes
)
713 if err
== nil || err
.Error() != tt
.want
{
714 t
.Errorf("time.GobDecode(%#v) error = %v, want %v", tt
.bytes
, err
, tt
.want
)
716 err
= ignored
.UnmarshalBinary(tt
.bytes
)
717 if err
== nil || err
.Error() != tt
.want
{
718 t
.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v", tt
.bytes
, err
, tt
.want
)
723 var notEncodableTimes
= []struct {
727 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.MarshalBinary: zone offset has fractional minute"},
728 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: unexpected zone offset"},
729 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinary: unexpected zone offset"},
730 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary: unexpected zone offset"},
733 func TestNotGobEncodableTime(t
*testing
.T
) {
734 for _
, tt
:= range notEncodableTimes
{
735 _
, err
:= tt
.time
.GobEncode()
736 if err
== nil || err
.Error() != tt
.want
{
737 t
.Errorf("%v GobEncode error = %v, want %v", tt
.time
, err
, tt
.want
)
739 _
, err
= tt
.time
.MarshalBinary()
740 if err
== nil || err
.Error() != tt
.want
{
741 t
.Errorf("%v MarshalBinary error = %v, want %v", tt
.time
, err
, tt
.want
)
746 var jsonTests
= []struct {
750 {Date(9999, 4, 12, 23, 20, 50, 520*1e6
, UTC
), `"9999-04-12T23:20:50.52Z"`},
751 {Date(1996, 12, 19, 16, 39, 57, 0, Local
), `"1996-12-19T16:39:57-08:00"`},
752 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`},
755 func TestTimeJSON(t
*testing
.T
) {
756 for _
, tt
:= range jsonTests
{
759 if jsonBytes
, err
:= json
.Marshal(tt
.time
); err
!= nil {
760 t
.Errorf("%v json.Marshal error = %v, want nil", tt
.time
, err
)
761 } else if string(jsonBytes
) != tt
.json
{
762 t
.Errorf("%v JSON = %#q, want %#q", tt
.time
, string(jsonBytes
), tt
.json
)
763 } else if err
= json
.Unmarshal(jsonBytes
, &jsonTime
); err
!= nil {
764 t
.Errorf("%v json.Unmarshal error = %v, want nil", tt
.time
, err
)
765 } else if !equalTimeAndZone(jsonTime
, tt
.time
) {
766 t
.Errorf("Unmarshaled time = %v, want %v", jsonTime
, tt
.time
)
771 func TestInvalidTimeJSON(t
*testing
.T
) {
773 err
:= json
.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt
)
774 _
, isParseErr
:= err
.(*ParseError
)
776 t
.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err
)
780 var notJSONEncodableTimes
= []struct {
784 {Date(10000, 1, 1, 0, 0, 0, 0, UTC
), "Time.MarshalJSON: year outside of range [0,9999]"},
785 {Date(-1, 1, 1, 0, 0, 0, 0, UTC
), "Time.MarshalJSON: year outside of range [0,9999]"},
788 func TestNotJSONEncodableTime(t
*testing
.T
) {
789 for _
, tt
:= range notJSONEncodableTimes
{
790 _
, err
:= tt
.time
.MarshalJSON()
791 if err
== nil || err
.Error() != tt
.want
{
792 t
.Errorf("%v MarshalJSON error = %v, want %v", tt
.time
, err
, tt
.want
)
797 var parseDurationTests
= []struct {
804 {"5s", true, 5 * Second
},
805 {"30s", true, 30 * Second
},
806 {"1478s", true, 1478 * Second
},
808 {"-5s", true, -5 * Second
},
809 {"+5s", true, 5 * Second
},
813 {"5.0s", true, 5 * Second
},
814 {"5.6s", true, 5*Second
+ 600*Millisecond
},
815 {"5.s", true, 5 * Second
},
816 {".5s", true, 500 * Millisecond
},
817 {"1.0s", true, 1 * Second
},
818 {"1.00s", true, 1 * Second
},
819 {"1.004s", true, 1*Second
+ 4*Millisecond
},
820 {"1.0040s", true, 1*Second
+ 4*Millisecond
},
821 {"100.00100s", true, 100*Second
+ 1*Millisecond
},
823 {"10ns", true, 10 * Nanosecond
},
824 {"11us", true, 11 * Microsecond
},
825 {"12µs", true, 12 * Microsecond
}, // U+00B5
826 {"12μs", true, 12 * Microsecond
}, // U+03BC
827 {"13ms", true, 13 * Millisecond
},
828 {"14s", true, 14 * Second
},
829 {"15m", true, 15 * Minute
},
830 {"16h", true, 16 * Hour
},
831 // composite durations
832 {"3h30m", true, 3*Hour
+ 30*Minute
},
833 {"10.5s4m", true, 4*Minute
+ 10*Second
+ 500*Millisecond
},
834 {"-2m3.4s", true, -(2*Minute
+ 3*Second
+ 400*Millisecond
)},
835 {"1h2m3s4ms5us6ns", true, 1*Hour
+ 2*Minute
+ 3*Second
+ 4*Millisecond
+ 5*Microsecond
+ 6*Nanosecond
},
836 {"39h9m14.425s", true, 39*Hour
+ 9*Minute
+ 14*Second
+ 425*Millisecond
},
838 {"52763797000ns", true, 52763797000 * Nanosecond
},
839 // more than 9 digits after decimal point, see https://golang.org/issue/6617
840 {"0.3333333333333333333h", true, 20 * Minute
},
841 // 9007199254740993 = 1<<53+1 cannot be stored precisely in a float64
842 {"9007199254740993ns", true, (1<<53 + 1) * Nanosecond
},
843 // largest duration that can be represented by int64 in nanoseconds
844 {"9223372036854775807ns", true, (1<<63 - 1) * Nanosecond
},
845 {"9223372036854775.807us", true, (1<<63 - 1) * Nanosecond
},
846 {"9223372036s854ms775us807ns", true, (1<<63 - 1) * Nanosecond
},
847 // large negative value
848 {"-9223372036854775807ns", true, -1<<63 + 1*Nanosecond
},
849 // huge string; issue 15011.
850 {"0.100000000000000000000h", true, 6 * Minute
},
851 // This value tests the first overflow check in leadingFraction.
852 {"0.830103483285477580700h", true, 49*Minute
+ 48*Second
+ 372539827*Nanosecond
},
863 {"3000000h", false, 0}, // overflow
864 {"9223372036854775808ns", false, 0}, // overflow
865 {"9223372036854775.808us", false, 0}, // overflow
866 {"9223372036854ms775us808ns", false, 0}, // overflow
867 // largest negative value of type int64 in nanoseconds should fail
868 // see https://go-review.googlesource.com/#/c/2461/
869 {"-9223372036854775808ns", false, 0},
872 func TestParseDuration(t
*testing
.T
) {
873 for _
, tc
:= range parseDurationTests
{
874 d
, err
:= ParseDuration(tc
.in
)
875 if tc
.ok
&& (err
!= nil || d
!= tc
.want
) {
876 t
.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc
.in
, d
, err
, tc
.want
)
877 } else if !tc
.ok
&& err
== nil {
878 t
.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc
.in
)
883 func TestParseDurationRoundTrip(t
*testing
.T
) {
884 for i
:= 0; i
< 100; i
++ {
885 // Resolutions finer than milliseconds will result in
886 // imprecise round-trips.
887 d0
:= Duration(rand
.Int31()) * Millisecond
889 d1
, err
:= ParseDuration(s
)
890 if err
!= nil || d0
!= d1
{
891 t
.Errorf("round-trip failed: %d => %q => %d, %v", d0
, s
, d1
, err
)
896 // golang.org/issue/4622
897 func TestLocationRace(t
*testing
.T
) {
898 ResetLocalOnceForTest() // reset the Once to trigger the race
900 c
:= make(chan string, 1)
906 Sleep(100 * Millisecond
)
908 // Back to Los Angeles for subsequent tests:
909 ForceUSPacificForTesting()
917 var mallocTest
= []struct {
922 {0, `time.Now()`, func() { t
= Now() }},
923 {0, `time.Now().UnixNano()`, func() { u
= Now().UnixNano() }},
926 func TestCountMallocs(t
*testing
.T
) {
928 t
.Skip("skipping malloc count in short mode")
930 if runtime
.GOMAXPROCS(0) > 1 {
931 t
.Skip("skipping; GOMAXPROCS>1")
933 for _
, mt
:= range mallocTest
{
934 allocs
:= int(testing
.AllocsPerRun(100, mt
.fn
))
935 if allocs
> mt
.count
{
936 t
.Errorf("%s: %d allocs, want %d", mt
.desc
, allocs
, mt
.count
)
941 func TestLoadFixed(t
*testing
.T
) {
942 // Issue 4064: handle locations without any zone transitions.
943 loc
, err
:= LoadLocation("Etc/GMT+1")
948 // The tzdata name Etc/GMT+1 uses "east is negative",
949 // but Go and most other systems use "east is positive".
950 // So GMT+1 corresponds to -3600 in the Go zone, not +3600.
951 name
, offset
:= Now().In(loc
).Zone()
952 // The zone abbreviation is "-01" since tzdata-2016g, and "GMT+1"
953 // on earlier versions; we accept both. (Issue #17276).
954 if !(name
== "GMT+1" || name
== "-01") || offset
!= -1*60*60 {
955 t
.Errorf("Now().In(loc).Zone() = %q, %d, want %q or %q, %d",
956 name
, offset
, "GMT+1", "-01", -1*60*60)
961 minDuration Duration
= -1 << 63
962 maxDuration Duration
= 1<<63 - 1
965 var subTests
= []struct {
970 {Time
{}, Time
{}, Duration(0)},
971 {Date(2009, 11, 23, 0, 0, 0, 1, UTC
), Date(2009, 11, 23, 0, 0, 0, 0, UTC
), Duration(1)},
972 {Date(2009, 11, 23, 0, 0, 0, 0, UTC
), Date(2009, 11, 24, 0, 0, 0, 0, UTC
), -24 * Hour
},
973 {Date(2009, 11, 24, 0, 0, 0, 0, UTC
), Date(2009, 11, 23, 0, 0, 0, 0, UTC
), 24 * Hour
},
974 {Date(-2009, 11, 24, 0, 0, 0, 0, UTC
), Date(-2009, 11, 23, 0, 0, 0, 0, UTC
), 24 * Hour
},
975 {Time
{}, Date(2109, 11, 23, 0, 0, 0, 0, UTC
), Duration(minDuration
)},
976 {Date(2109, 11, 23, 0, 0, 0, 0, UTC
), Time
{}, Duration(maxDuration
)},
977 {Time
{}, Date(-2109, 11, 23, 0, 0, 0, 0, UTC
), Duration(maxDuration
)},
978 {Date(-2109, 11, 23, 0, 0, 0, 0, UTC
), Time
{}, Duration(minDuration
)},
979 {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
},
980 {Date(2300, 1, 1, 0, 0, 0, 0, UTC
), Date(2000, 1, 1, 0, 0, 0, 0, UTC
), Duration(maxDuration
)},
981 {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
},
982 {Date(2000, 1, 1, 0, 0, 0, 0, UTC
), Date(2300, 1, 1, 0, 0, 0, 0, UTC
), Duration(minDuration
)},
983 {MinMonoTime
, MaxMonoTime
, minDuration
},
984 {MaxMonoTime
, MinMonoTime
, maxDuration
},
987 func TestSub(t
*testing
.T
) {
988 for i
, st
:= range subTests
{
989 got
:= st
.t
.Sub(st
.u
)
991 t
.Errorf("#%d: Sub(%v, %v): got %v; want %v", i
, st
.t
, st
.u
, got
, st
.d
)
996 var nsDurationTests
= []struct {
1000 {Duration(-1000), -1000},
1003 {Duration(1000), 1000},
1006 func TestDurationNanoseconds(t
*testing
.T
) {
1007 for _
, tt
:= range nsDurationTests
{
1008 if got
:= tt
.d
.Nanoseconds(); got
!= tt
.want
{
1009 t
.Errorf("d.Nanoseconds() = %d; want: %d", got
, tt
.want
)
1014 var secDurationTests
= []struct {
1018 {Duration(300000000), 0.3},
1021 func TestDurationSeconds(t
*testing
.T
) {
1022 for _
, tt
:= range secDurationTests
{
1023 if got
:= tt
.d
.Seconds(); got
!= tt
.want
{
1024 t
.Errorf("d.Seconds() = %g; want: %g", got
, tt
.want
)
1029 var minDurationTests
= []struct {
1033 {Duration(-60000000000), -1},
1034 {Duration(-1), -1 / 60e9
},
1035 {Duration(1), 1 / 60e9
},
1036 {Duration(60000000000), 1},
1037 {Duration(3000), 5e-8},
1040 func TestDurationMinutes(t
*testing
.T
) {
1041 for _
, tt
:= range minDurationTests
{
1042 if got
:= tt
.d
.Minutes(); got
!= tt
.want
{
1043 t
.Errorf("d.Minutes() = %g; want: %g", got
, tt
.want
)
1048 var hourDurationTests
= []struct {
1052 {Duration(-3600000000000), -1},
1053 {Duration(-1), -1 / 3600e9
},
1054 {Duration(1), 1 / 3600e9
},
1055 {Duration(3600000000000), 1},
1056 {Duration(36), 1e-11},
1059 func TestDurationHours(t
*testing
.T
) {
1060 for _
, tt
:= range hourDurationTests
{
1061 if got
:= tt
.d
.Hours(); got
!= tt
.want
{
1062 t
.Errorf("d.Hours() = %g; want: %g", got
, tt
.want
)
1067 var durationTruncateTests
= []struct {
1073 {Minute
, -7 * Second
, Minute
},
1074 {Minute
, 0, Minute
},
1075 {Minute
, 1, Minute
},
1076 {Minute
+ 10*Second
, 10 * Second
, Minute
+ 10*Second
},
1077 {2*Minute
+ 10*Second
, Minute
, 2 * Minute
},
1078 {10*Minute
+ 10*Second
, 3 * Minute
, 9 * Minute
},
1079 {Minute
+ 10*Second
, Minute
+ 10*Second
+ 1, 0},
1080 {Minute
+ 10*Second
, Hour
, 0},
1081 {-Minute
, Second
, -Minute
},
1082 {-10 * Minute
, 3 * Minute
, -9 * Minute
},
1083 {-10 * Minute
, Hour
, 0},
1086 func TestDurationTruncate(t
*testing
.T
) {
1087 for _
, tt
:= range durationTruncateTests
{
1088 if got
:= tt
.d
.Truncate(tt
.m
); got
!= tt
.want
{
1089 t
.Errorf("Duration(%s).Truncate(%s) = %s; want: %s", tt
.d
, tt
.m
, got
, tt
.want
)
1094 var durationRoundTests
= []struct {
1100 {Minute
, -11 * Second
, Minute
},
1101 {Minute
, 0, Minute
},
1102 {Minute
, 1, Minute
},
1103 {2 * Minute
, Minute
, 2 * Minute
},
1104 {2*Minute
+ 10*Second
, Minute
, 2 * Minute
},
1105 {2*Minute
+ 30*Second
, Minute
, 3 * Minute
},
1106 {2*Minute
+ 50*Second
, Minute
, 3 * Minute
},
1107 {-Minute
, 1, -Minute
},
1108 {-2 * Minute
, Minute
, -2 * Minute
},
1109 {-2*Minute
- 10*Second
, Minute
, -2 * Minute
},
1110 {-2*Minute
- 30*Second
, Minute
, -3 * Minute
},
1111 {-2*Minute
- 50*Second
, Minute
, -3 * Minute
},
1113 {9e18
, 5e18
, 1<<63 - 1},
1114 {-8e18
, 3e18
, -9e18
},
1115 {-9e18
, 5e18
, -1 << 63},
1116 {3<<61 - 1, 3 << 61, 3 << 61},
1119 func TestDurationRound(t
*testing
.T
) {
1120 for _
, tt
:= range durationRoundTests
{
1121 if got
:= tt
.d
.Round(tt
.m
); got
!= tt
.want
{
1122 t
.Errorf("Duration(%s).Round(%s) = %s; want: %s", tt
.d
, tt
.m
, got
, tt
.want
)
1127 var defaultLocTests
= []struct {
1129 f
func(t1
, t2 Time
) bool
1131 {"After", func(t1
, t2 Time
) bool { return t1
.After(t2
) == t2
.After(t1
) }},
1132 {"Before", func(t1
, t2 Time
) bool { return t1
.Before(t2
) == t2
.Before(t1
) }},
1133 {"Equal", func(t1
, t2 Time
) bool { return t1
.Equal(t2
) == t2
.Equal(t1
) }},
1135 {"IsZero", func(t1
, t2 Time
) bool { return t1
.IsZero() == t2
.IsZero() }},
1136 {"Date", func(t1
, t2 Time
) bool {
1137 a1
, b1
, c1
:= t1
.Date()
1138 a2
, b2
, c2
:= t2
.Date()
1139 return a1
== a2
&& b1
== b2
&& c1
== c2
1141 {"Year", func(t1
, t2 Time
) bool { return t1
.Year() == t2
.Year() }},
1142 {"Month", func(t1
, t2 Time
) bool { return t1
.Month() == t2
.Month() }},
1143 {"Day", func(t1
, t2 Time
) bool { return t1
.Day() == t2
.Day() }},
1144 {"Weekday", func(t1
, t2 Time
) bool { return t1
.Weekday() == t2
.Weekday() }},
1145 {"ISOWeek", func(t1
, t2 Time
) bool {
1146 a1
, b1
:= t1
.ISOWeek()
1147 a2
, b2
:= t2
.ISOWeek()
1148 return a1
== a2
&& b1
== b2
1150 {"Clock", func(t1
, t2 Time
) bool {
1151 a1
, b1
, c1
:= t1
.Clock()
1152 a2
, b2
, c2
:= t2
.Clock()
1153 return a1
== a2
&& b1
== b2
&& c1
== c2
1155 {"Hour", func(t1
, t2 Time
) bool { return t1
.Hour() == t2
.Hour() }},
1156 {"Minute", func(t1
, t2 Time
) bool { return t1
.Minute() == t2
.Minute() }},
1157 {"Second", func(t1
, t2 Time
) bool { return t1
.Second() == t2
.Second() }},
1158 {"Nanosecond", func(t1
, t2 Time
) bool { return t1
.Hour() == t2
.Hour() }},
1159 {"YearDay", func(t1
, t2 Time
) bool { return t1
.YearDay() == t2
.YearDay() }},
1161 // Using Equal since Add don't modify loc using "==" will cause a fail
1162 {"Add", func(t1
, t2 Time
) bool { return t1
.Add(Hour
).Equal(t2
.Add(Hour
)) }},
1163 {"Sub", func(t1
, t2 Time
) bool { return t1
.Sub(t2
) == t2
.Sub(t1
) }},
1165 //Original caus for this test case bug 15852
1166 {"AddDate", func(t1
, t2 Time
) bool { return t1
.AddDate(1991, 9, 3) == t2
.AddDate(1991, 9, 3) }},
1168 {"UTC", func(t1
, t2 Time
) bool { return t1
.UTC() == t2
.UTC() }},
1169 {"Local", func(t1
, t2 Time
) bool { return t1
.Local() == t2
.Local() }},
1170 {"In", func(t1
, t2 Time
) bool { return t1
.In(UTC
) == t2
.In(UTC
) }},
1172 {"Local", func(t1
, t2 Time
) bool { return t1
.Local() == t2
.Local() }},
1173 {"Zone", func(t1
, t2 Time
) bool {
1176 return a1
== a2
&& b1
== b2
1179 {"Unix", func(t1
, t2 Time
) bool { return t1
.Unix() == t2
.Unix() }},
1180 {"UnixNano", func(t1
, t2 Time
) bool { return t1
.UnixNano() == t2
.UnixNano() }},
1182 {"MarshalBinary", func(t1
, t2 Time
) bool {
1183 a1
, b1
:= t1
.MarshalBinary()
1184 a2
, b2
:= t2
.MarshalBinary()
1185 return bytes
.Equal(a1
, a2
) && b1
== b2
1187 {"GobEncode", func(t1
, t2 Time
) bool {
1188 a1
, b1
:= t1
.GobEncode()
1189 a2
, b2
:= t2
.GobEncode()
1190 return bytes
.Equal(a1
, a2
) && b1
== b2
1192 {"MarshalJSON", func(t1
, t2 Time
) bool {
1193 a1
, b1
:= t1
.MarshalJSON()
1194 a2
, b2
:= t2
.MarshalJSON()
1195 return bytes
.Equal(a1
, a2
) && b1
== b2
1197 {"MarshalText", func(t1
, t2 Time
) bool {
1198 a1
, b1
:= t1
.MarshalText()
1199 a2
, b2
:= t2
.MarshalText()
1200 return bytes
.Equal(a1
, a2
) && b1
== b2
1203 {"Truncate", func(t1
, t2 Time
) bool { return t1
.Truncate(Hour
).Equal(t2
.Truncate(Hour
)) }},
1204 {"Round", func(t1
, t2 Time
) bool { return t1
.Round(Hour
).Equal(t2
.Round(Hour
)) }},
1206 {"== Time{}", func(t1
, t2 Time
) bool { return (t1
== Time
{}) == (t2
== Time
{}) }},
1209 func TestDefaultLoc(t
*testing
.T
) {
1210 // Verify that all of Time's methods behave identically if loc is set to
1212 for _
, tt
:= range defaultLocTests
{
1216 t
.Errorf("Time{} and Time{}.UTC() behave differently for %s", tt
.name
)
1221 func BenchmarkNow(b
*testing
.B
) {
1222 for i
:= 0; i
< b
.N
; i
++ {
1227 func BenchmarkNowUnixNano(b
*testing
.B
) {
1228 for i
:= 0; i
< b
.N
; i
++ {
1229 u
= Now().UnixNano()
1233 func BenchmarkFormat(b
*testing
.B
) {
1234 t
:= Unix(1265346057, 0)
1235 for i
:= 0; i
< b
.N
; i
++ {
1236 t
.Format("Mon Jan 2 15:04:05 2006")
1240 func BenchmarkFormatNow(b
*testing
.B
) {
1241 // Like BenchmarkFormat, but easier, because the time zone
1242 // lookup cache is optimized for the present.
1244 for i
:= 0; i
< b
.N
; i
++ {
1245 t
.Format("Mon Jan 2 15:04:05 2006")
1249 func BenchmarkMarshalJSON(b
*testing
.B
) {
1251 for i
:= 0; i
< b
.N
; i
++ {
1256 func BenchmarkMarshalText(b
*testing
.B
) {
1258 for i
:= 0; i
< b
.N
; i
++ {
1263 func BenchmarkParse(b
*testing
.B
) {
1264 for i
:= 0; i
< b
.N
; i
++ {
1265 Parse(ANSIC
, "Mon Jan 2 15:04:05 2006")
1269 func BenchmarkParseDuration(b
*testing
.B
) {
1270 for i
:= 0; i
< b
.N
; i
++ {
1271 ParseDuration("9007199254.740993ms")
1272 ParseDuration("9007199254740993ns")
1276 func BenchmarkHour(b
*testing
.B
) {
1278 for i
:= 0; i
< b
.N
; i
++ {
1283 func BenchmarkSecond(b
*testing
.B
) {
1285 for i
:= 0; i
< b
.N
; i
++ {
1290 func BenchmarkYear(b
*testing
.B
) {
1292 for i
:= 0; i
< b
.N
; i
++ {
1297 func BenchmarkDay(b
*testing
.B
) {
1299 for i
:= 0; i
< b
.N
; i
++ {
1304 func TestMarshalBinaryZeroTime(t
*testing
.T
) {
1306 enc
, err
:= t0
.MarshalBinary()
1310 t1
:= Now() // not zero
1311 if err
:= t1
.UnmarshalBinary(enc
); err
!= nil {
1315 t
.Errorf("t0=%#v\nt1=%#v\nwant identical structures", t0
, t1
)
1319 // Issue 17720: Zero value of time.Month fails to print
1320 func TestZeroMonthString(t
*testing
.T
) {
1321 if got
, want
:= Month(0).String(), "%!Month(0)"; got
!= want
{
1322 t
.Errorf("zero month = %q; want %q", got
, want
)
1326 // Issue 24692: Out of range weekday panics
1327 func TestWeekdayString(t
*testing
.T
) {
1328 if got
, want
:= Weekday(Tuesday
).String(), "Tuesday"; got
!= want
{
1329 t
.Errorf("Tuesday weekday = %q; want %q", got
, want
)
1331 if got
, want
:= Weekday(14).String(), "%!Weekday(14)"; got
!= want
{
1332 t
.Errorf("14th weekday = %q; want %q", got
, want
)
1336 func TestReadFileLimit(t
*testing
.T
) {
1337 const zero
= "/dev/zero"
1338 if _
, err
:= os
.Stat(zero
); err
!= nil {
1339 t
.Skip("skipping test without a /dev/zero")
1341 _
, err
:= ReadFile(zero
)
1342 if err
== nil ||
!strings
.Contains(err
.Error(), "is too large") {
1343 t
.Errorf("readFile(%q) error = %v; want error containing 'is too large'", zero
, err
)
1347 // Issue 25686: hard crash on concurrent timer access.
1348 // This test deliberately invokes a race condition.
1349 // We are testing that we don't crash with "fatal error: panic holding locks".
1350 func TestConcurrentTimerReset(t
*testing
.T
) {
1352 t
.Skip("skipping test under race detector")
1355 // We expect this code to panic rather than crash.
1356 // Don't worry if it doesn't panic.
1357 catch
:= func(i
int) {
1358 if e
:= recover(); e
!= nil {
1359 t
.Logf("panic in goroutine %d, as expected, with %q", i
, e
)
1361 t
.Logf("no panic in goroutine %d", i
)
1365 const goroutines
= 8
1367 var wg sync
.WaitGroup
1369 timer
:= NewTimer(Hour
)
1370 for i
:= 0; i
< goroutines
; i
++ {
1374 for j
:= 0; j
< tries
; j
++ {
1375 timer
.Reset(Hour
+ Duration(i
*j
))