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