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
},
238 func TestTruncateRound(t
*testing
.T
) {
251 testOne
:= func(ti
, tns
, di
int64) bool {
252 t0
:= Unix(ti
, int64(tns
)).UTC()
261 // Compute bt = absolute nanoseconds.
268 // Compute quotient and remainder mod d.
269 bd
.SetInt64(int64(d
))
270 bq
.DivMod(bt
, bd
, br
)
272 // To truncate, subtract remainder.
273 // br is < d, so it fits in an int64.
275 t1
:= t0
.Add(-Duration(r
))
277 // Check that time.Truncate works.
278 if trunc
:= t0
.Truncate(d
); trunc
!= t1
{
279 t
.Errorf("Time.Truncate(%s, %s) = %s, want %s\n"+
280 "%v trunc %v =\n%v want\n%v",
281 t0
.Format(RFC3339Nano
), d
, trunc
, t1
.Format(RFC3339Nano
),
282 absString(t0
), int64(d
), absString(trunc
), absString(t1
))
286 // To round, add d back if remainder r > d/2 or r == exactly d/2.
287 // The commented out code would round half to even instead of up,
288 // but that makes it time-zone dependent, which is a bit strange.
289 if r
> int64(d
)/2 || r
+r
== int64(d
) /*&& bq.Bit(0) == 1*/ {
290 t1
= t1
.Add(Duration(d
))
293 // Check that time.Round works.
294 if rnd
:= t0
.Round(d
); rnd
!= t1
{
295 t
.Errorf("Time.Round(%s, %s) = %s, want %s\n"+
296 "%v round %v =\n%v want\n%v",
297 t0
.Format(RFC3339Nano
), d
, rnd
, t1
.Format(RFC3339Nano
),
298 absString(t0
), int64(d
), absString(rnd
), absString(t1
))
305 for _
, tt
:= range truncateRoundTests
{
306 testOne(tt
.t
.Unix(), int64(tt
.t
.Nanosecond()), int64(tt
.d
))
310 for i
:= 0; i
< 100; i
++ {
311 for j
:= 1; j
< 100; j
++ {
312 testOne(unixToZero
, int64(i
), int64(j
))
313 testOne(unixToZero
, -int64(i
), int64(j
))
324 // randomly generated test cases
325 cfg
:= &quick
.Config
{MaxCount
: 100000}
330 // divisors of Second
331 f1
:= func(ti
int64, tns
int32, logdi
int32) bool {
333 a
, b
:= uint(logdi%9
), (logdi
>>16)%9
335 for i
:= 0; i
< int(b
); i
++ {
338 return testOne(ti
, int64(tns
), int64(d
))
342 // multiples of Second
343 f2
:= func(ti
int64, tns
int32, di
int32) bool {
344 d
:= Duration(di
) * Second
348 return testOne(ti
, int64(tns
), int64(d
))
353 f3
:= func(tns
, di
int64) bool {
364 return testOne(0, tns
, di
)
369 f4
:= func(ti
int64, tns
int32, di
int64) bool {
370 return testOne(ti
, int64(tns
), di
)
375 type TimeFormatTest
struct {
377 formattedValue
string
380 var rfc3339Formats
= []TimeFormatTest
{
381 {Date(2008, 9, 17, 20, 4, 26, 0, UTC
), "2008-09-17T20:04:26Z"},
382 {Date(1994, 9, 17, 20, 4, 26, 0, FixedZone("EST", -18000)), "1994-09-17T20:04:26-05:00"},
383 {Date(2000, 12, 26, 1, 15, 6, 0, FixedZone("OTO", 15600)), "2000-12-26T01:15:06+04:20"},
386 func TestRFC3339Conversion(t
*testing
.T
) {
387 for _
, f
:= range rfc3339Formats
{
388 if f
.time
.Format(RFC3339
) != f
.formattedValue
{
390 t
.Errorf(" want=%+v", f
.formattedValue
)
391 t
.Errorf(" have=%+v", f
.time
.Format(RFC3339
))
396 type FormatTest
struct {
402 var formatTests
= []FormatTest
{
403 {"ANSIC", ANSIC
, "Wed Feb 4 21:00:57 2009"},
404 {"UnixDate", UnixDate
, "Wed Feb 4 21:00:57 PST 2009"},
405 {"RubyDate", RubyDate
, "Wed Feb 04 21:00:57 -0800 2009"},
406 {"RFC822", RFC822
, "04 Feb 09 21:00 PST"},
407 {"RFC850", RFC850
, "Wednesday, 04-Feb-09 21:00:57 PST"},
408 {"RFC1123", RFC1123
, "Wed, 04 Feb 2009 21:00:57 PST"},
409 {"RFC1123Z", RFC1123Z
, "Wed, 04 Feb 2009 21:00:57 -0800"},
410 {"RFC3339", RFC3339
, "2009-02-04T21:00:57-08:00"},
411 {"RFC3339Nano", RFC3339Nano
, "2009-02-04T21:00:57.0123456-08:00"},
412 {"Kitchen", Kitchen
, "9:00PM"},
413 {"am/pm", "3pm", "9pm"},
414 {"AM/PM", "3PM", "9PM"},
415 {"two-digit year", "06 01 02", "09 02 04"},
416 // Three-letter months and days must not be followed by lower-case letter.
417 {"Janet", "Hi Janet, the Month is January", "Hi Janet, the Month is February"},
418 // Time stamps, Fractional seconds.
419 {"Stamp", Stamp
, "Feb 4 21:00:57"},
420 {"StampMilli", StampMilli
, "Feb 4 21:00:57.012"},
421 {"StampMicro", StampMicro
, "Feb 4 21:00:57.012345"},
422 {"StampNano", StampNano
, "Feb 4 21:00:57.012345600"},
425 func TestFormat(t
*testing
.T
) {
426 // The numeric time represents Thu Feb 4 21:00:57.012345600 PST 2010
427 time
:= Unix(0, 1233810057012345600)
428 for _
, test
:= range formatTests
{
429 result
:= time
.Format(test
.format
)
430 if result
!= test
.result
{
431 t
.Errorf("%s expected %q got %q", test
.name
, test
.result
, result
)
436 func TestFormatShortYear(t
*testing
.T
) {
438 -100001, -100000, -99999,
439 -10001, -10000, -9999,
448 99999, 100000, 100001,
451 for _
, y
:= range years
{
452 time
:= Date(y
, January
, 1, 0, 0, 0, 0, UTC
)
453 result
:= time
.Format("2006.01.02")
456 // The 4 in %04d counts the - sign, so print -y instead
457 // and introduce our own - sign.
458 want
= fmt
.Sprintf("-%04d.%02d.%02d", -y
, 1, 1)
460 want
= fmt
.Sprintf("%04d.%02d.%02d", y
, 1, 1)
463 t
.Errorf("(jan 1 %d).Format(\"2006.01.02\") = %q, want %q", y
, result
, want
)
468 type ParseTest
struct {
472 hasTZ
bool // contains a time zone
473 hasWD
bool // contains a weekday
474 yearSign
int // sign of year, -1 indicates the year is not present in the format
475 fracDigits
int // number of digits of fractional second
478 var parseTests
= []ParseTest
{
479 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
480 {"UnixDate", UnixDate
, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0},
481 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
482 {"RFC850", RFC850
, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0},
483 {"RFC1123", RFC1123
, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0},
484 {"RFC1123", RFC1123
, "Thu, 04 Feb 2010 22:00:57 PDT", true, true, 1, 0},
485 {"RFC1123Z", RFC1123Z
, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0},
486 {"RFC3339", RFC3339
, "2010-02-04T21:00:57-08:00", true, false, 1, 0},
487 {"custom: \"2006-01-02 15:04:05-07\"", "2006-01-02 15:04:05-07", "2010-02-04 21:00:57-08", true, false, 1, 0},
488 // Optional fractional seconds.
489 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57.0 2010", false, true, 1, 1},
490 {"UnixDate", UnixDate
, "Thu Feb 4 21:00:57.01 PST 2010", true, true, 1, 2},
491 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57.012 -0800 2010", true, true, 1, 3},
492 {"RFC850", RFC850
, "Thursday, 04-Feb-10 21:00:57.0123 PST", true, true, 1, 4},
493 {"RFC1123", RFC1123
, "Thu, 04 Feb 2010 21:00:57.01234 PST", true, true, 1, 5},
494 {"RFC1123Z", RFC1123Z
, "Thu, 04 Feb 2010 21:00:57.01234 -0800", true, true, 1, 5},
495 {"RFC3339", RFC3339
, "2010-02-04T21:00:57.012345678-08:00", true, false, 1, 9},
496 {"custom: \"2006-01-02 15:04:05\"", "2006-01-02 15:04:05", "2010-02-04 21:00:57.0", false, false, 1, 0},
497 // Amount of white space should not matter.
498 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
499 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
500 // Case should not matter
501 {"ANSIC", ANSIC
, "THU FEB 4 21:00:57 2010", false, true, 1, 0},
502 {"ANSIC", ANSIC
, "thu feb 4 21:00:57 2010", false, true, 1, 0},
503 // Fractional seconds.
504 {"millisecond", "Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 21:00:57.012 2010", false, true, 1, 3},
505 {"microsecond", "Mon Jan _2 15:04:05.000000 2006", "Thu Feb 4 21:00:57.012345 2010", false, true, 1, 6},
506 {"nanosecond", "Mon Jan _2 15:04:05.000000000 2006", "Thu Feb 4 21:00:57.012345678 2010", false, true, 1, 9},
507 // Leading zeros in other places should not be taken as fractional seconds.
508 {"zero1", "2006.01.02.15.04.05.0", "2010.02.04.21.00.57.0", false, false, 1, 1},
509 {"zero2", "2006.01.02.15.04.05.00", "2010.02.04.21.00.57.01", false, false, 1, 2},
510 // Month and day names only match when not followed by a lower-case letter.
511 {"Janet", "Hi Janet, the Month is January: Jan _2 15:04:05 2006", "Hi Janet, the Month is February: Feb 4 21:00:57 2010", false, true, 1, 0},
514 {"GMT-8", UnixDate
, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0},
516 // Accept any number of fractional second digits (including none) for .999...
517 // In Go 1, .999... was completely ignored in the format, meaning the first two
518 // cases would succeed, but the next four would not. Go 1.1 accepts all six.
519 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
520 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
521 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
522 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
523 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
524 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
527 {"", StampNano
, "Feb 4 21:00:57.012345678", false, false, -1, 9},
528 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012300000", false, false, -1, 4},
529 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
530 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.0123", false, false, -1, 4},
531 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
534 func TestParse(t
*testing
.T
) {
535 for _
, test
:= range parseTests
{
536 time
, err
:= Parse(test
.format
, test
.value
)
538 t
.Errorf("%s error: %v", test
.name
, err
)
540 checkTime(time
, &test
, t
)
545 func TestParseInSydney(t
*testing
.T
) {
546 loc
, err
:= LoadLocation("Australia/Sydney")
551 // Check that Parse (and ParseInLocation) understand
552 // that Feb EST and Aug EST are different time zones in Sydney
553 // even though both are called EST.
554 t1
, err
:= ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 EST", loc
)
558 t2
:= Date(2013, February
, 1, 00, 00, 00, 0, loc
)
560 t
.Fatalf("ParseInLocation(Feb 01 2013 EST, Sydney) = %v, want %v", t1
, t2
)
562 _
, offset
:= t1
.Zone()
563 if offset
!= 11*60*60 {
564 t
.Fatalf("ParseInLocation(Feb 01 2013 EST, Sydney).Zone = _, %d, want _, %d", offset
, 11*60*60)
567 t1
, err
= ParseInLocation("Jan 02 2006 MST", "Aug 01 2013 EST", loc
)
571 t2
= Date(2013, August
, 1, 00, 00, 00, 0, loc
)
573 t
.Fatalf("ParseInLocation(Aug 01 2013 EST, Sydney) = %v, want %v", t1
, t2
)
575 _
, offset
= t1
.Zone()
576 if offset
!= 10*60*60 {
577 t
.Fatalf("ParseInLocation(Aug 01 2013 EST, Sydney).Zone = _, %d, want _, %d", offset
, 10*60*60)
581 func TestLoadLocationZipFile(t
*testing
.T
) {
582 t
.Skip("gccgo does not use the zip file")
584 ForceZipFileForTesting(true)
585 defer ForceZipFileForTesting(false)
587 _
, err
:= LoadLocation("Australia/Sydney")
593 var rubyTests
= []ParseTest
{
594 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
595 // Ignore the time zone in the test. If it parses, it'll be OK.
596 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 -0000 2010", false, true, 1, 0},
597 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 +0000 2010", false, true, 1, 0},
598 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 +1130 2010", false, true, 1, 0},
601 // Problematic time zone format needs special tests.
602 func TestRubyParse(t
*testing
.T
) {
603 for _
, test
:= range rubyTests
{
604 time
, err
:= Parse(test
.format
, test
.value
)
606 t
.Errorf("%s error: %v", test
.name
, err
)
608 checkTime(time
, &test
, t
)
613 func checkTime(time Time
, test
*ParseTest
, t
*testing
.T
) {
614 // The time should be Thu Feb 4 21:00:57 PST 2010
615 if test
.yearSign
>= 0 && test
.yearSign
*time
.Year() != 2010 {
616 t
.Errorf("%s: bad year: %d not %d", test
.name
, time
.Year(), 2010)
618 if time
.Month() != February
{
619 t
.Errorf("%s: bad month: %s not %s", test
.name
, time
.Month(), February
)
622 t
.Errorf("%s: bad day: %d not %d", test
.name
, time
.Day(), 4)
624 if time
.Hour() != 21 {
625 t
.Errorf("%s: bad hour: %d not %d", test
.name
, time
.Hour(), 21)
627 if time
.Minute() != 0 {
628 t
.Errorf("%s: bad minute: %d not %d", test
.name
, time
.Minute(), 0)
630 if time
.Second() != 57 {
631 t
.Errorf("%s: bad second: %d not %d", test
.name
, time
.Second(), 57)
633 // Nanoseconds must be checked against the precision of the input.
634 nanosec
, err
:= strconv
.ParseUint("012345678"[:test
.fracDigits
]+"000000000"[:9-test
.fracDigits
], 10, 0)
638 if time
.Nanosecond() != int(nanosec
) {
639 t
.Errorf("%s: bad nanosecond: %d not %d", test
.name
, time
.Nanosecond(), nanosec
)
641 name
, offset
:= time
.Zone()
642 if test
.hasTZ
&& offset
!= -28800 {
643 t
.Errorf("%s: bad tz offset: %s %d not %d", test
.name
, name
, offset
, -28800)
645 if test
.hasWD
&& time
.Weekday() != Thursday
{
646 t
.Errorf("%s: bad weekday: %s not %s", test
.name
, time
.Weekday(), Thursday
)
650 func TestFormatAndParse(t
*testing
.T
) {
651 const fmt
= "Mon MST " + RFC3339
// all fields
652 f
:= func(sec
int64) bool {
654 if t1
.Year() < 1000 || t1
.Year() > 9999 {
655 // not required to work
658 t2
, err
:= Parse(fmt
, t1
.Format(fmt
))
660 t
.Errorf("error: %s", err
)
663 if t1
.Unix() != t2
.Unix() || t1
.Nanosecond() != t2
.Nanosecond() {
664 t
.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec
, t1
, t1
.Unix(), t2
, t2
.Unix())
669 f32
:= func(sec
int32) bool { return f(int64(sec
)) }
670 cfg
:= &quick
.Config
{MaxCount
: 10000}
672 // Try a reasonable date first, then the huge ones.
673 if err
:= quick
.Check(f32
, cfg
); err
!= nil {
676 if err
:= quick
.Check(f
, cfg
); err
!= nil {
681 type ParseTimeZoneTest
struct {
687 var parseTimeZoneTests
= []ParseTimeZoneTest
{
688 {"gmt hi there", 0, false},
689 {"GMT hi there", 3, true},
690 {"GMT+12 hi there", 6, true},
691 {"GMT+00 hi there", 3, true}, // 0 or 00 is not a legal offset.
692 {"GMT-5 hi there", 5, true},
693 {"GMT-51 hi there", 3, true},
694 {"ChST hi there", 4, true},
696 {"MSDY", 0, false}, // four letters must end in T.
697 {"ESAST hi", 5, true},
698 {"ESASTT hi", 0, false}, // run of upper-case letters too long.
699 {"ESATY hi", 0, false}, // five letters must end in T.
702 func TestParseTimeZone(t
*testing
.T
) {
703 for _
, test
:= range parseTimeZoneTests
{
704 length
, ok
:= ParseTimeZone(test
.value
)
706 t
.Errorf("expected %t for %q got %t", test
.ok
, test
.value
, ok
)
707 } else if length
!= test
.length
{
708 t
.Errorf("expected %d for %q got %d", test
.length
, test
.value
, length
)
713 type ParseErrorTest
struct {
716 expect
string // must appear within the error
719 var parseErrorTests
= []ParseErrorTest
{
720 {ANSIC
, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon
721 {ANSIC
, "Thu Feb 4 21:00:57 @2010", "cannot parse"},
722 {ANSIC
, "Thu Feb 4 21:00:60 2010", "second out of range"},
723 {ANSIC
, "Thu Feb 4 21:61:57 2010", "minute out of range"},
724 {ANSIC
, "Thu Feb 4 24:00:60 2010", "hour out of range"},
725 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot parse"},
726 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot parse"},
727 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fractional second out of range"},
728 // issue 4502. StampNano requires exactly 9 digits of precision.
729 {StampNano
, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`},
730 {StampNano
, "Dec 7 11:22:01.0000000000", "extra text: 0"},
731 // issue 4493. Helpful errors.
732 {RFC3339
, "2006-01-02T15:04:05Z07:00", `parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00`},
733 {RFC3339
, "2006-01-02T15:04_abc", `parsing time "2006-01-02T15:04_abc" as "2006-01-02T15:04:05Z07:00": cannot parse "_abc" as ":"`},
734 {RFC3339
, "2006-01-02T15:04:05_abc", `parsing time "2006-01-02T15:04:05_abc" as "2006-01-02T15:04:05Z07:00": cannot parse "_abc" as "Z07:00"`},
735 {RFC3339
, "2006-01-02T15:04:05Z_abc", `parsing time "2006-01-02T15:04:05Z_abc": extra text: _abc`},
738 func TestParseErrors(t
*testing
.T
) {
739 for _
, test
:= range parseErrorTests
{
740 _
, err
:= Parse(test
.format
, test
.value
)
742 t
.Errorf("expected error for %q %q", test
.format
, test
.value
)
743 } else if strings
.Index(err
.Error(), test
.expect
) < 0 {
744 t
.Errorf("expected error with %q for %q %q; got %s", test
.expect
, test
.format
, test
.value
, err
)
749 func TestNoonIs12PM(t
*testing
.T
) {
750 noon
:= Date(0, January
, 1, 12, 0, 0, 0, UTC
)
751 const expect
= "12:00PM"
752 got
:= noon
.Format("3:04PM")
754 t
.Errorf("got %q; expect %q", got
, expect
)
756 got
= noon
.Format("03:04PM")
758 t
.Errorf("got %q; expect %q", got
, expect
)
762 func TestMidnightIs12AM(t
*testing
.T
) {
763 midnight
:= Date(0, January
, 1, 0, 0, 0, 0, UTC
)
765 got
:= midnight
.Format("3:04PM")
767 t
.Errorf("got %q; expect %q", got
, expect
)
769 got
= midnight
.Format("03:04PM")
771 t
.Errorf("got %q; expect %q", got
, expect
)
775 func Test12PMIsNoon(t
*testing
.T
) {
776 noon
, err
:= Parse("3:04PM", "12:00PM")
778 t
.Fatal("error parsing date:", err
)
780 if noon
.Hour() != 12 {
781 t
.Errorf("got %d; expect 12", noon
.Hour())
783 noon
, err
= Parse("03:04PM", "12:00PM")
785 t
.Fatal("error parsing date:", err
)
787 if noon
.Hour() != 12 {
788 t
.Errorf("got %d; expect 12", noon
.Hour())
792 func Test12AMIsMidnight(t
*testing
.T
) {
793 midnight
, err
:= Parse("3:04PM", "12:00AM")
795 t
.Fatal("error parsing date:", err
)
797 if midnight
.Hour() != 0 {
798 t
.Errorf("got %d; expect 0", midnight
.Hour())
800 midnight
, err
= Parse("03:04PM", "12:00AM")
802 t
.Fatal("error parsing date:", err
)
804 if midnight
.Hour() != 0 {
805 t
.Errorf("got %d; expect 0", midnight
.Hour())
809 // Check that a time without a Zone still produces a (numeric) time zone
810 // when formatted with MST as a requested zone.
811 func TestMissingZone(t
*testing
.T
) {
812 time
, err
:= Parse(RubyDate
, "Thu Feb 02 16:10:03 -0500 2006")
814 t
.Fatal("error parsing date:", err
)
816 expect
:= "Thu Feb 2 16:10:03 -0500 2006" // -0500 not EST
817 str
:= time
.Format(UnixDate
) // uses MST as its time zone
819 t
.Errorf("got %s; expect %s", str
, expect
)
823 func TestMinutesInTimeZone(t
*testing
.T
) {
824 time
, err
:= Parse(RubyDate
, "Mon Jan 02 15:04:05 +0123 2006")
826 t
.Fatal("error parsing date:", err
)
828 expected
:= (1*60 + 23) * 60
829 _
, offset
:= time
.Zone()
830 if offset
!= expected
{
831 t
.Errorf("ZoneOffset = %d, want %d", offset
, expected
)
835 type SecondsTimeZoneOffsetTest
struct {
841 var secondsTimeZoneOffsetTests
= []SecondsTimeZoneOffsetTest
{
842 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
843 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02-00:34:08", -(34*60 + 8)},
844 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02+003408", 34*60 + 8},
845 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
846 {"2006-01-02T15:04:05Z070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
847 {"2006-01-02T15:04:05Z07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
850 func TestParseSecondsInTimeZone(t
*testing
.T
) {
851 // should accept timezone offsets with seconds like: Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
852 for _
, test
:= range secondsTimeZoneOffsetTests
{
853 time
, err
:= Parse(test
.format
, test
.value
)
855 t
.Fatal("error parsing date:", err
)
857 _
, offset
:= time
.Zone()
858 if offset
!= test
.expectedoffset
{
859 t
.Errorf("ZoneOffset = %d, want %d", offset
, test
.expectedoffset
)
864 func TestFormatSecondsInTimeZone(t
*testing
.T
) {
865 d
:= Date(1871, 9, 17, 20, 4, 26, 0, FixedZone("LMT", -(34*60+8)))
866 timestr
:= d
.Format("2006-01-02T15:04:05Z070000")
867 expected
:= "1871-09-17T20:04:26-003408"
868 if timestr
!= expected
{
869 t
.Errorf("Got %s, want %s", timestr
, expected
)
873 type ISOWeekTest
struct {
875 month
, day
int // month and day
876 yex
int // expected year
877 wex
int // expected week
880 var isoWeekTests
= []ISOWeekTest
{
881 {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52},
882 {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1},
883 {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52},
884 {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1},
885 {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1},
886 {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2},
887 {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53},
888 {2000, 1, 1, 1999, 52}, {2001, 1, 1, 2001, 1}, {2002, 1, 1, 2002, 1},
889 {2003, 1, 1, 2003, 1}, {2004, 1, 1, 2004, 1}, {2005, 1, 1, 2004, 53},
890 {2006, 1, 1, 2005, 52}, {2007, 1, 1, 2007, 1}, {2008, 1, 1, 2008, 1},
891 {2009, 1, 1, 2009, 1}, {2010, 1, 1, 2009, 53}, {2010, 1, 1, 2009, 53},
892 {2011, 1, 1, 2010, 52}, {2011, 1, 2, 2010, 52}, {2011, 1, 3, 2011, 1},
893 {2011, 1, 4, 2011, 1}, {2011, 1, 5, 2011, 1}, {2011, 1, 6, 2011, 1},
894 {2011, 1, 7, 2011, 1}, {2011, 1, 8, 2011, 1}, {2011, 1, 9, 2011, 1},
895 {2011, 1, 10, 2011, 2}, {2011, 1, 11, 2011, 2}, {2011, 6, 12, 2011, 23},
896 {2011, 6, 13, 2011, 24}, {2011, 12, 25, 2011, 51}, {2011, 12, 26, 2011, 52},
897 {2011, 12, 27, 2011, 52}, {2011, 12, 28, 2011, 52}, {2011, 12, 29, 2011, 52},
898 {2011, 12, 30, 2011, 52}, {2011, 12, 31, 2011, 52}, {1995, 1, 1, 1994, 52},
899 {2012, 1, 1, 2011, 52}, {2012, 1, 2, 2012, 1}, {2012, 1, 8, 2012, 1},
900 {2012, 1, 9, 2012, 2}, {2012, 12, 23, 2012, 51}, {2012, 12, 24, 2012, 52},
901 {2012, 12, 30, 2012, 52}, {2012, 12, 31, 2013, 1}, {2013, 1, 1, 2013, 1},
902 {2013, 1, 6, 2013, 1}, {2013, 1, 7, 2013, 2}, {2013, 12, 22, 2013, 51},
903 {2013, 12, 23, 2013, 52}, {2013, 12, 29, 2013, 52}, {2013, 12, 30, 2014, 1},
904 {2014, 1, 1, 2014, 1}, {2014, 1, 5, 2014, 1}, {2014, 1, 6, 2014, 2},
905 {2015, 1, 1, 2015, 1}, {2016, 1, 1, 2015, 53}, {2017, 1, 1, 2016, 52},
906 {2018, 1, 1, 2018, 1}, {2019, 1, 1, 2019, 1}, {2020, 1, 1, 2020, 1},
907 {2021, 1, 1, 2020, 53}, {2022, 1, 1, 2021, 52}, {2023, 1, 1, 2022, 52},
908 {2024, 1, 1, 2024, 1}, {2025, 1, 1, 2025, 1}, {2026, 1, 1, 2026, 1},
909 {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1},
910 {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1},
911 {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1},
912 {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53},
913 {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52},
916 func TestISOWeek(t
*testing
.T
) {
917 // Selected dates and corner cases
918 for _
, wt
:= range isoWeekTests
{
919 dt
:= Date(wt
.year
, Month(wt
.month
), wt
.day
, 0, 0, 0, 0, UTC
)
921 if w
!= wt
.wex || y
!= wt
.yex
{
922 t
.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d",
923 y
, w
, wt
.yex
, wt
.wex
, wt
.year
, wt
.month
, wt
.day
)
927 // The only real invariant: Jan 04 is in week 1
928 for year
:= 1950; year
< 2100; year
++ {
929 if y
, w
:= Date(year
, January
, 4, 0, 0, 0, 0, UTC
).ISOWeek(); y
!= year || w
!= 1 {
930 t
.Errorf("got %d/%d; expected %d/1 for Jan 04", y
, w
, year
)
935 type YearDayTest
struct {
940 // Test YearDay in several different scenarios
942 var yearDayTests
= []YearDayTest
{
943 // Non-leap-year tests
963 // Looks like leap-year (but isn't) tests
973 // Year one tests (non-leap)
983 // Year minus one tests (non-leap)
993 // 400 BC tests (leap-year)
1001 {-400, 12, 31, 366},
1005 // Gregorian calendar change (no effect)
1007 {1582, 10, 15, 288},
1010 // Check to see if YearDay is location sensitive
1011 var yearDayLocations
= []*Location
{
1012 FixedZone("UTC-8", -8*60*60),
1013 FixedZone("UTC-4", -4*60*60),
1015 FixedZone("UTC+4", 4*60*60),
1016 FixedZone("UTC+8", 8*60*60),
1019 func TestYearDay(t
*testing
.T
) {
1020 for _
, loc
:= range yearDayLocations
{
1021 for _
, ydt
:= range yearDayTests
{
1022 dt
:= Date(ydt
.year
, Month(ydt
.month
), ydt
.day
, 0, 0, 0, 0, loc
)
1023 yday
:= dt
.YearDay()
1024 if yday
!= ydt
.yday
{
1025 t
.Errorf("got %d, expected %d for %d-%02d-%02d in %v",
1026 yday
, ydt
.yday
, ydt
.year
, ydt
.month
, ydt
.day
, loc
)
1032 var durationTests
= []struct {
1037 {"1ns", 1 * Nanosecond
},
1038 {"1.1us", 1100 * Nanosecond
},
1039 {"2.2ms", 2200 * Microsecond
},
1040 {"3.3s", 3300 * Millisecond
},
1041 {"4m5s", 4*Minute
+ 5*Second
},
1042 {"4m5.001s", 4*Minute
+ 5001*Millisecond
},
1043 {"5h6m7.001s", 5*Hour
+ 6*Minute
+ 7001*Millisecond
},
1044 {"8m0.000000001s", 8*Minute
+ 1*Nanosecond
},
1045 {"2562047h47m16.854775807s", 1<<63 - 1},
1046 {"-2562047h47m16.854775808s", -1 << 63},
1049 func TestDurationString(t
*testing
.T
) {
1050 for _
, tt
:= range durationTests
{
1051 if str
:= tt
.d
.String(); str
!= tt
.str
{
1052 t
.Errorf("Duration(%d).String() = %s, want %s", int64(tt
.d
), str
, tt
.str
)
1055 if str
:= (-tt
.d
).String(); str
!= "-"+tt
.str
{
1056 t
.Errorf("Duration(%d).String() = %s, want %s", int64(-tt
.d
), str
, "-"+tt
.str
)
1062 var dateTests
= []struct {
1063 year
, month
, day
, hour
, min
, sec
, nsec
int
1067 {2011, 11, 6, 1, 0, 0, 0, Local
, 1320566400}, // 1:00:00 PDT
1068 {2011, 11, 6, 1, 59, 59, 0, Local
, 1320569999}, // 1:59:59 PDT
1069 {2011, 11, 6, 2, 0, 0, 0, Local
, 1320573600}, // 2:00:00 PST
1071 {2011, 3, 13, 1, 0, 0, 0, Local
, 1300006800}, // 1:00:00 PST
1072 {2011, 3, 13, 1, 59, 59, 0, Local
, 1300010399}, // 1:59:59 PST
1073 {2011, 3, 13, 3, 0, 0, 0, Local
, 1300010400}, // 3:00:00 PDT
1074 {2011, 3, 13, 2, 30, 0, 0, Local
, 1300008600}, // 2:30:00 PDT ≡ 1:30 PST
1076 // Many names for Fri Nov 18 7:56:35 PST 2011
1077 {2011, 11, 18, 7, 56, 35, 0, Local
, 1321631795}, // Nov 18 7:56:35
1078 {2011, 11, 19, -17, 56, 35, 0, Local
, 1321631795}, // Nov 19 -17:56:35
1079 {2011, 11, 17, 31, 56, 35, 0, Local
, 1321631795}, // Nov 17 31:56:35
1080 {2011, 11, 18, 6, 116, 35, 0, Local
, 1321631795}, // Nov 18 6:116:35
1081 {2011, 10, 49, 7, 56, 35, 0, Local
, 1321631795}, // Oct 49 7:56:35
1082 {2011, 11, 18, 7, 55, 95, 0, Local
, 1321631795}, // Nov 18 7:55:95
1083 {2011, 11, 18, 7, 56, 34, 1e9
, Local
, 1321631795}, // Nov 18 7:56:34 + 10⁹ns
1084 {2011, 12, -12, 7, 56, 35, 0, Local
, 1321631795}, // Dec -21 7:56:35
1085 {2012, 1, -43, 7, 56, 35, 0, Local
, 1321631795}, // Jan -52 7:56:35 2012
1086 {2012, int(January
- 2), 18, 7, 56, 35, 0, Local
, 1321631795}, // (Jan-2) 18 7:56:35 2012
1087 {2010, int(December
+ 11), 18, 7, 56, 35, 0, Local
, 1321631795}, // (Dec+11) 18 7:56:35 2010
1090 func TestDate(t
*testing
.T
) {
1091 for _
, tt
:= range dateTests
{
1092 time
:= Date(tt
.year
, Month(tt
.month
), tt
.day
, tt
.hour
, tt
.min
, tt
.sec
, tt
.nsec
, tt
.z
)
1093 want
:= Unix(tt
.unix
, 0)
1094 if !time
.Equal(want
) {
1095 t
.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v",
1096 tt
.year
, tt
.month
, tt
.day
, tt
.hour
, tt
.min
, tt
.sec
, tt
.nsec
, tt
.z
,
1102 // Several ways of getting from
1103 // Fri Nov 18 7:56:35 PST 2011
1105 // Thu Mar 19 7:56:35 PST 2016
1106 var addDateTests
= []struct {
1107 years
, months
, days
int
1112 {5, -6, -18 - 30 - 12},
1115 func TestAddDate(t
*testing
.T
) {
1116 t0
:= Date(2011, 11, 18, 7, 56, 35, 0, UTC
)
1117 t1
:= Date(2016, 3, 19, 7, 56, 35, 0, UTC
)
1118 for _
, at
:= range addDateTests
{
1119 time
:= t0
.AddDate(at
.years
, at
.months
, at
.days
)
1120 if !time
.Equal(t1
) {
1121 t
.Errorf("AddDate(%d, %d, %d) = %v, want %v",
1122 at
.years
, at
.months
, at
.days
,
1128 var daysInTests
= []struct {
1131 {2011, 1, 31}, // January, first month, 31 days
1132 {2011, 2, 28}, // February, non-leap year, 28 days
1133 {2012, 2, 29}, // February, leap year, 29 days
1134 {2011, 6, 30}, // June, 30 days
1135 {2011, 12, 31}, // December, last month, 31 days
1138 func TestDaysIn(t
*testing
.T
) {
1139 // The daysIn function is not exported.
1140 // Test the daysIn function via the `var DaysIn = daysIn`
1141 // statement in the internal_test.go file.
1142 for _
, tt
:= range daysInTests
{
1143 di
:= DaysIn(Month(tt
.month
), tt
.year
)
1145 t
.Errorf("got %d; expected %d for %d-%02d",
1146 di
, tt
.di
, tt
.year
, tt
.month
)
1151 func TestAddToExactSecond(t
*testing
.T
) {
1152 // Add an amount to the current time to round it up to the next exact second.
1153 // This test checks that the nsec field still lies within the range [0, 999999999].
1155 t2
:= t1
.Add(Second
- Duration(t1
.Nanosecond()))
1156 sec
:= (t1
.Second() + 1) % 60
1157 if t2
.Second() != sec || t2
.Nanosecond() != 0 {
1158 t
.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2
.Second(), t2
.Nanosecond(), sec
)
1162 func equalTimeAndZone(a
, b Time
) bool {
1163 aname
, aoffset
:= a
.Zone()
1164 bname
, boffset
:= b
.Zone()
1165 return a
.Equal(b
) && aoffset
== boffset
&& aname
== bname
1168 var gobTests
= []Time
{
1169 Date(0, 1, 2, 3, 4, 5, 6, UTC
),
1170 Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
1171 Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
1173 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
1174 Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
1177 func TestTimeGob(t
*testing
.T
) {
1179 enc
:= gob
.NewEncoder(&b
)
1180 dec
:= gob
.NewDecoder(&b
)
1181 for _
, tt
:= range gobTests
{
1183 if err
:= enc
.Encode(&tt
); err
!= nil {
1184 t
.Errorf("%v gob Encode error = %q, want nil", tt
, err
)
1185 } else if err
:= dec
.Decode(&gobtt
); err
!= nil {
1186 t
.Errorf("%v gob Decode error = %q, want nil", tt
, err
)
1187 } else if !equalTimeAndZone(gobtt
, tt
) {
1188 t
.Errorf("Decoded time = %v, want %v", gobtt
, tt
)
1194 var invalidEncodingTests
= []struct {
1198 {[]byte{}, "Time.UnmarshalBinary: no data"},
1199 {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"},
1200 {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"},
1203 func TestInvalidTimeGob(t
*testing
.T
) {
1204 for _
, tt
:= range invalidEncodingTests
{
1206 err
:= ignored
.GobDecode(tt
.bytes
)
1207 if err
== nil || err
.Error() != tt
.want
{
1208 t
.Errorf("time.GobDecode(%#v) error = %v, want %v", tt
.bytes
, err
, tt
.want
)
1210 err
= ignored
.UnmarshalBinary(tt
.bytes
)
1211 if err
== nil || err
.Error() != tt
.want
{
1212 t
.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v", tt
.bytes
, err
, tt
.want
)
1217 var notEncodableTimes
= []struct {
1221 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.MarshalBinary: zone offset has fractional minute"},
1222 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: unexpected zone offset"},
1223 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinary: unexpected zone offset"},
1224 {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary: unexpected zone offset"},
1227 func TestNotGobEncodableTime(t
*testing
.T
) {
1228 for _
, tt
:= range notEncodableTimes
{
1229 _
, err
:= tt
.time
.GobEncode()
1230 if err
== nil || err
.Error() != tt
.want
{
1231 t
.Errorf("%v GobEncode error = %v, want %v", tt
.time
, err
, tt
.want
)
1233 _
, err
= tt
.time
.MarshalBinary()
1234 if err
== nil || err
.Error() != tt
.want
{
1235 t
.Errorf("%v MarshalBinary error = %v, want %v", tt
.time
, err
, tt
.want
)
1240 var jsonTests
= []struct {
1244 {Date(9999, 4, 12, 23, 20, 50, 520*1e6
, UTC
), `"9999-04-12T23:20:50.52Z"`},
1245 {Date(1996, 12, 19, 16, 39, 57, 0, Local
), `"1996-12-19T16:39:57-08:00"`},
1246 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`},
1249 func TestTimeJSON(t
*testing
.T
) {
1250 for _
, tt
:= range jsonTests
{
1253 if jsonBytes
, err
:= json
.Marshal(tt
.time
); err
!= nil {
1254 t
.Errorf("%v json.Marshal error = %v, want nil", tt
.time
, err
)
1255 } else if string(jsonBytes
) != tt
.json
{
1256 t
.Errorf("%v JSON = %#q, want %#q", tt
.time
, string(jsonBytes
), tt
.json
)
1257 } else if err
= json
.Unmarshal(jsonBytes
, &jsonTime
); err
!= nil {
1258 t
.Errorf("%v json.Unmarshal error = %v, want nil", tt
.time
, err
)
1259 } else if !equalTimeAndZone(jsonTime
, tt
.time
) {
1260 t
.Errorf("Unmarshaled time = %v, want %v", jsonTime
, tt
.time
)
1265 func TestInvalidTimeJSON(t
*testing
.T
) {
1267 err
:= json
.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt
)
1268 _
, isParseErr
:= err
.(*ParseError
)
1270 t
.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err
)
1274 var notJSONEncodableTimes
= []struct {
1278 {Date(10000, 1, 1, 0, 0, 0, 0, UTC
), "Time.MarshalJSON: year outside of range [0,9999]"},
1279 {Date(-1, 1, 1, 0, 0, 0, 0, UTC
), "Time.MarshalJSON: year outside of range [0,9999]"},
1282 func TestNotJSONEncodableTime(t
*testing
.T
) {
1283 for _
, tt
:= range notJSONEncodableTimes
{
1284 _
, err
:= tt
.time
.MarshalJSON()
1285 if err
== nil || err
.Error() != tt
.want
{
1286 t
.Errorf("%v MarshalJSON error = %v, want %v", tt
.time
, err
, tt
.want
)
1291 var parseDurationTests
= []struct {
1298 {"5s", true, 5 * Second
},
1299 {"30s", true, 30 * Second
},
1300 {"1478s", true, 1478 * Second
},
1302 {"-5s", true, -5 * Second
},
1303 {"+5s", true, 5 * Second
},
1307 {"5.0s", true, 5 * Second
},
1308 {"5.6s", true, 5*Second
+ 600*Millisecond
},
1309 {"5.s", true, 5 * Second
},
1310 {".5s", true, 500 * Millisecond
},
1311 {"1.0s", true, 1 * Second
},
1312 {"1.00s", true, 1 * Second
},
1313 {"1.004s", true, 1*Second
+ 4*Millisecond
},
1314 {"1.0040s", true, 1*Second
+ 4*Millisecond
},
1315 {"100.00100s", true, 100*Second
+ 1*Millisecond
},
1317 {"10ns", true, 10 * Nanosecond
},
1318 {"11us", true, 11 * Microsecond
},
1319 {"12µs", true, 12 * Microsecond
}, // U+00B5
1320 {"12μs", true, 12 * Microsecond
}, // U+03BC
1321 {"13ms", true, 13 * Millisecond
},
1322 {"14s", true, 14 * Second
},
1323 {"15m", true, 15 * Minute
},
1324 {"16h", true, 16 * Hour
},
1325 // composite durations
1326 {"3h30m", true, 3*Hour
+ 30*Minute
},
1327 {"10.5s4m", true, 4*Minute
+ 10*Second
+ 500*Millisecond
},
1328 {"-2m3.4s", true, -(2*Minute
+ 3*Second
+ 400*Millisecond
)},
1329 {"1h2m3s4ms5us6ns", true, 1*Hour
+ 2*Minute
+ 3*Second
+ 4*Millisecond
+ 5*Microsecond
+ 6*Nanosecond
},
1330 {"39h9m14.425s", true, 39*Hour
+ 9*Minute
+ 14*Second
+ 425*Millisecond
},
1332 {"52763797000ns", true, 52763797000 * Nanosecond
},
1333 // more than 9 digits after decimal point, see http://golang.org/issue/6617
1334 {"0.3333333333333333333h", true, 20 * Minute
},
1347 func TestParseDuration(t
*testing
.T
) {
1348 for _
, tc
:= range parseDurationTests
{
1349 d
, err
:= ParseDuration(tc
.in
)
1350 if tc
.ok
&& (err
!= nil || d
!= tc
.want
) {
1351 t
.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc
.in
, d
, err
, tc
.want
)
1352 } else if !tc
.ok
&& err
== nil {
1353 t
.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc
.in
)
1358 func TestParseDurationRoundTrip(t
*testing
.T
) {
1359 for i
:= 0; i
< 100; i
++ {
1360 // Resolutions finer than milliseconds will result in
1361 // imprecise round-trips.
1362 d0
:= Duration(rand
.Int31()) * Millisecond
1364 d1
, err
:= ParseDuration(s
)
1365 if err
!= nil || d0
!= d1
{
1366 t
.Errorf("round-trip failed: %d => %q => %d, %v", d0
, s
, d1
, err
)
1371 // golang.org/issue/4622
1372 func TestLocationRace(t
*testing
.T
) {
1373 ResetLocalOnceForTest() // reset the Once to trigger the race
1375 c
:= make(chan string, 1)
1381 Sleep(100 * Millisecond
)
1383 // Back to Los Angeles for subsequent tests:
1384 ForceUSPacificForTesting()
1392 var mallocTest
= []struct {
1397 {0, `time.Now()`, func() { t
= Now() }},
1398 {0, `time.Now().UnixNano()`, func() { u
= Now().UnixNano() }},
1401 func TestCountMallocs(t
*testing
.T
) {
1402 if testing
.Short() {
1403 t
.Skip("skipping malloc count in short mode")
1405 if runtime
.GOMAXPROCS(0) > 1 {
1406 t
.Skip("skipping; GOMAXPROCS>1")
1408 for _
, mt
:= range mallocTest
{
1409 allocs
:= int(testing
.AllocsPerRun(100, mt
.fn
))
1410 if allocs
> mt
.count
{
1411 t
.Errorf("%s: %d allocs, want %d", mt
.desc
, allocs
, mt
.count
)
1416 func TestLoadFixed(t
*testing
.T
) {
1417 // Issue 4064: handle locations without any zone transitions.
1418 loc
, err
:= LoadLocation("Etc/GMT+1")
1423 // The tzdata name Etc/GMT+1 uses "east is negative",
1424 // but Go and most other systems use "east is positive".
1425 // So GMT+1 corresponds to -3600 in the Go zone, not +3600.
1426 name
, offset
:= Now().In(loc
).Zone()
1427 if name
!= "GMT+1" || offset
!= -1*60*60 {
1428 t
.Errorf("Now().In(loc).Zone() = %q, %d, want %q, %d", name
, offset
, "GMT+1", -1*60*60)
1433 minDuration Duration
= -1 << 63
1434 maxDuration Duration
= 1<<63 - 1
1437 var subTests
= []struct {
1442 {Time
{}, Time
{}, Duration(0)},
1443 {Date(2009, 11, 23, 0, 0, 0, 1, UTC
), Date(2009, 11, 23, 0, 0, 0, 0, UTC
), Duration(1)},
1444 {Date(2009, 11, 23, 0, 0, 0, 0, UTC
), Date(2009, 11, 24, 0, 0, 0, 0, UTC
), -24 * Hour
},
1445 {Date(2009, 11, 24, 0, 0, 0, 0, UTC
), Date(2009, 11, 23, 0, 0, 0, 0, UTC
), 24 * Hour
},
1446 {Date(-2009, 11, 24, 0, 0, 0, 0, UTC
), Date(-2009, 11, 23, 0, 0, 0, 0, UTC
), 24 * Hour
},
1447 {Time
{}, Date(2109, 11, 23, 0, 0, 0, 0, UTC
), Duration(minDuration
)},
1448 {Date(2109, 11, 23, 0, 0, 0, 0, UTC
), Time
{}, Duration(maxDuration
)},
1449 {Time
{}, Date(-2109, 11, 23, 0, 0, 0, 0, UTC
), Duration(maxDuration
)},
1450 {Date(-2109, 11, 23, 0, 0, 0, 0, UTC
), Time
{}, Duration(minDuration
)},
1451 {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
},
1452 {Date(2300, 1, 1, 0, 0, 0, 0, UTC
), Date(2000, 1, 1, 0, 0, 0, 0, UTC
), Duration(maxDuration
)},
1453 {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
},
1454 {Date(2000, 1, 1, 0, 0, 0, 0, UTC
), Date(2300, 1, 1, 0, 0, 0, 0, UTC
), Duration(minDuration
)},
1457 func TestSub(t
*testing
.T
) {
1458 for i
, st
:= range subTests
{
1459 got
:= st
.t
.Sub(st
.u
)
1461 t
.Errorf("#%d: Sub(%v, %v): got %v; want %v", i
, st
.t
, st
.u
, got
, st
.d
)
1466 var nsDurationTests
= []struct {
1470 {Duration(-1000), -1000},
1473 {Duration(1000), 1000},
1476 func TestDurationNanoseconds(t
*testing
.T
) {
1477 for _
, tt
:= range nsDurationTests
{
1478 if got
:= tt
.d
.Nanoseconds(); got
!= tt
.want
{
1479 t
.Errorf("d.Nanoseconds() = %d; want: %d", got
, tt
.want
)
1484 var minDurationTests
= []struct {
1488 {Duration(-60000000000), -1},
1489 {Duration(-1), -1 / 60e9
},
1490 {Duration(1), 1 / 60e9
},
1491 {Duration(60000000000), 1},
1494 func TestDurationMinutes(t
*testing
.T
) {
1495 for _
, tt
:= range minDurationTests
{
1496 if got
:= tt
.d
.Minutes(); got
!= tt
.want
{
1497 t
.Errorf("d.Minutes() = %d; want: %d", got
, tt
.want
)
1502 var hourDurationTests
= []struct {
1506 {Duration(-3600000000000), -1},
1507 {Duration(-1), -1 / 3600e9
},
1508 {Duration(1), 1 / 3600e9
},
1509 {Duration(3600000000000), 1},
1512 func TestDurationHours(t
*testing
.T
) {
1513 for _
, tt
:= range hourDurationTests
{
1514 if got
:= tt
.d
.Hours(); got
!= tt
.want
{
1515 t
.Errorf("d.Hours() = %d; want: %d", got
, tt
.want
)
1520 func BenchmarkNow(b
*testing
.B
) {
1521 for i
:= 0; i
< b
.N
; i
++ {
1526 func BenchmarkNowUnixNano(b
*testing
.B
) {
1527 for i
:= 0; i
< b
.N
; i
++ {
1528 u
= Now().UnixNano()
1532 func BenchmarkFormat(b
*testing
.B
) {
1533 t
:= Unix(1265346057, 0)
1534 for i
:= 0; i
< b
.N
; i
++ {
1535 t
.Format("Mon Jan 2 15:04:05 2006")
1539 func BenchmarkFormatNow(b
*testing
.B
) {
1540 // Like BenchmarkFormat, but easier, because the time zone
1541 // lookup cache is optimized for the present.
1543 for i
:= 0; i
< b
.N
; i
++ {
1544 t
.Format("Mon Jan 2 15:04:05 2006")
1548 func BenchmarkParse(b
*testing
.B
) {
1549 for i
:= 0; i
< b
.N
; i
++ {
1550 Parse(ANSIC
, "Mon Jan 2 15:04:05 2006")
1554 func BenchmarkHour(b
*testing
.B
) {
1556 for i
:= 0; i
< b
.N
; i
++ {
1561 func BenchmarkSecond(b
*testing
.B
) {
1563 for i
:= 0; i
< b
.N
; i
++ {
1568 func BenchmarkYear(b
*testing
.B
) {
1570 for i
:= 0; i
< b
.N
; i
++ {
1575 func BenchmarkDay(b
*testing
.B
) {
1577 for i
:= 0; i
< b
.N
; i
++ {