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.
16 type TimeFormatTest
struct {
21 var rfc3339Formats
= []TimeFormatTest
{
22 {Date(2008, 9, 17, 20, 4, 26, 0, UTC
), "2008-09-17T20:04:26Z"},
23 {Date(1994, 9, 17, 20, 4, 26, 0, FixedZone("EST", -18000)), "1994-09-17T20:04:26-05:00"},
24 {Date(2000, 12, 26, 1, 15, 6, 0, FixedZone("OTO", 15600)), "2000-12-26T01:15:06+04:20"},
27 func TestRFC3339Conversion(t
*testing
.T
) {
28 for _
, f
:= range rfc3339Formats
{
29 if f
.time
.Format(RFC3339
) != f
.formattedValue
{
31 t
.Errorf(" want=%+v", f
.formattedValue
)
32 t
.Errorf(" have=%+v", f
.time
.Format(RFC3339
))
37 type FormatTest
struct {
43 var formatTests
= []FormatTest
{
44 {"ANSIC", ANSIC
, "Wed Feb 4 21:00:57 2009"},
45 {"UnixDate", UnixDate
, "Wed Feb 4 21:00:57 PST 2009"},
46 {"RubyDate", RubyDate
, "Wed Feb 04 21:00:57 -0800 2009"},
47 {"RFC822", RFC822
, "04 Feb 09 21:00 PST"},
48 {"RFC850", RFC850
, "Wednesday, 04-Feb-09 21:00:57 PST"},
49 {"RFC1123", RFC1123
, "Wed, 04 Feb 2009 21:00:57 PST"},
50 {"RFC1123Z", RFC1123Z
, "Wed, 04 Feb 2009 21:00:57 -0800"},
51 {"RFC3339", RFC3339
, "2009-02-04T21:00:57-08:00"},
52 {"RFC3339Nano", RFC3339Nano
, "2009-02-04T21:00:57.0123456-08:00"},
53 {"Kitchen", Kitchen
, "9:00PM"},
54 {"am/pm", "3pm", "9pm"},
55 {"AM/PM", "3PM", "9PM"},
56 {"two-digit year", "06 01 02", "09 02 04"},
57 // Three-letter months and days must not be followed by lower-case letter.
58 {"Janet", "Hi Janet, the Month is January", "Hi Janet, the Month is February"},
59 // Time stamps, Fractional seconds.
60 {"Stamp", Stamp
, "Feb 4 21:00:57"},
61 {"StampMilli", StampMilli
, "Feb 4 21:00:57.012"},
62 {"StampMicro", StampMicro
, "Feb 4 21:00:57.012345"},
63 {"StampNano", StampNano
, "Feb 4 21:00:57.012345600"},
66 func TestFormat(t
*testing
.T
) {
67 // The numeric time represents Thu Feb 4 21:00:57.012345600 PST 2010
68 time
:= Unix(0, 1233810057012345600)
69 for _
, test
:= range formatTests
{
70 result
:= time
.Format(test
.format
)
71 if result
!= test
.result
{
72 t
.Errorf("%s expected %q got %q", test
.name
, test
.result
, result
)
77 func TestFormatShortYear(t
*testing
.T
) {
79 -100001, -100000, -99999,
80 -10001, -10000, -9999,
89 99999, 100000, 100001,
92 for _
, y
:= range years
{
93 time
:= Date(y
, January
, 1, 0, 0, 0, 0, UTC
)
94 result
:= time
.Format("2006.01.02")
97 // The 4 in %04d counts the - sign, so print -y instead
98 // and introduce our own - sign.
99 want
= fmt
.Sprintf("-%04d.%02d.%02d", -y
, 1, 1)
101 want
= fmt
.Sprintf("%04d.%02d.%02d", y
, 1, 1)
104 t
.Errorf("(jan 1 %d).Format(\"2006.01.02\") = %q, want %q", y
, result
, want
)
109 type ParseTest
struct {
113 hasTZ
bool // contains a time zone
114 hasWD
bool // contains a weekday
115 yearSign
int // sign of year, -1 indicates the year is not present in the format
116 fracDigits
int // number of digits of fractional second
119 var parseTests
= []ParseTest
{
120 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
121 {"UnixDate", UnixDate
, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0},
122 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
123 {"RFC850", RFC850
, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0},
124 {"RFC1123", RFC1123
, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0},
125 {"RFC1123", RFC1123
, "Thu, 04 Feb 2010 22:00:57 PDT", true, true, 1, 0},
126 {"RFC1123Z", RFC1123Z
, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0},
127 {"RFC3339", RFC3339
, "2010-02-04T21:00:57-08:00", true, false, 1, 0},
128 {"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},
129 // Optional fractional seconds.
130 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57.0 2010", false, true, 1, 1},
131 {"UnixDate", UnixDate
, "Thu Feb 4 21:00:57.01 PST 2010", true, true, 1, 2},
132 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57.012 -0800 2010", true, true, 1, 3},
133 {"RFC850", RFC850
, "Thursday, 04-Feb-10 21:00:57.0123 PST", true, true, 1, 4},
134 {"RFC1123", RFC1123
, "Thu, 04 Feb 2010 21:00:57.01234 PST", true, true, 1, 5},
135 {"RFC1123Z", RFC1123Z
, "Thu, 04 Feb 2010 21:00:57.01234 -0800", true, true, 1, 5},
136 {"RFC3339", RFC3339
, "2010-02-04T21:00:57.012345678-08:00", true, false, 1, 9},
137 {"custom: \"2006-01-02 15:04:05\"", "2006-01-02 15:04:05", "2010-02-04 21:00:57.0", false, false, 1, 0},
138 // Amount of white space should not matter.
139 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
140 {"ANSIC", ANSIC
, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
141 // Case should not matter
142 {"ANSIC", ANSIC
, "THU FEB 4 21:00:57 2010", false, true, 1, 0},
143 {"ANSIC", ANSIC
, "thu feb 4 21:00:57 2010", false, true, 1, 0},
144 // Fractional seconds.
145 {"millisecond", "Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 21:00:57.012 2010", false, true, 1, 3},
146 {"microsecond", "Mon Jan _2 15:04:05.000000 2006", "Thu Feb 4 21:00:57.012345 2010", false, true, 1, 6},
147 {"nanosecond", "Mon Jan _2 15:04:05.000000000 2006", "Thu Feb 4 21:00:57.012345678 2010", false, true, 1, 9},
148 // Leading zeros in other places should not be taken as fractional seconds.
149 {"zero1", "2006.01.02.15.04.05.0", "2010.02.04.21.00.57.0", false, false, 1, 1},
150 {"zero2", "2006.01.02.15.04.05.00", "2010.02.04.21.00.57.01", false, false, 1, 2},
151 // Month and day names only match when not followed by a lower-case letter.
152 {"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},
155 {"GMT-8", UnixDate
, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0},
157 // Accept any number of fractional second digits (including none) for .999...
158 // In Go 1, .999... was completely ignored in the format, meaning the first two
159 // cases would succeed, but the next four would not. Go 1.1 accepts all six.
160 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
161 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
162 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
163 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
164 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
165 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
168 {"", StampNano
, "Feb 4 21:00:57.012345678", false, false, -1, 9},
169 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012300000", false, false, -1, 4},
170 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
171 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.0123", false, false, -1, 4},
172 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
175 func TestParse(t
*testing
.T
) {
176 for _
, test
:= range parseTests
{
177 time
, err
:= Parse(test
.format
, test
.value
)
179 t
.Errorf("%s error: %v", test
.name
, err
)
181 checkTime(time
, &test
, t
)
186 func TestParseInLocation(t
*testing
.T
) {
187 // Check that Parse (and ParseInLocation) understand that
188 // Feb 01 AST (Arabia Standard Time) and Feb 01 AST (Atlantic Standard Time)
189 // are in different time zones even though both are called AST
191 baghdad
, err
:= LoadLocation("Asia/Baghdad")
196 t1
, err
:= ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", baghdad
)
200 t2
:= Date(2013, February
, 1, 00, 00, 00, 0, baghdad
)
202 t
.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad) = %v, want %v", t1
, t2
)
204 _
, offset
:= t1
.Zone()
205 if offset
!= 3*60*60 {
206 t
.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad).Zone = _, %d, want _, %d", offset
, 3*60*60)
209 blancSablon
, err
:= LoadLocation("America/Blanc-Sablon")
214 t1
, err
= ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", blancSablon
)
218 t2
= Date(2013, February
, 1, 00, 00, 00, 0, blancSablon
)
220 t
.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon) = %v, want %v", t1
, t2
)
222 _
, offset
= t1
.Zone()
223 if offset
!= -4*60*60 {
224 t
.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon).Zone = _, %d, want _, %d", offset
, -4*60*60)
228 func TestLoadLocationZipFile(t
*testing
.T
) {
229 t
.Skip("gccgo does not use the zip file")
231 ForceZipFileForTesting(true)
232 defer ForceZipFileForTesting(false)
234 _
, err
:= LoadLocation("Australia/Sydney")
240 var rubyTests
= []ParseTest
{
241 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
242 // Ignore the time zone in the test. If it parses, it'll be OK.
243 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 -0000 2010", false, true, 1, 0},
244 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 +0000 2010", false, true, 1, 0},
245 {"RubyDate", RubyDate
, "Thu Feb 04 21:00:57 +1130 2010", false, true, 1, 0},
248 // Problematic time zone format needs special tests.
249 func TestRubyParse(t
*testing
.T
) {
250 for _
, test
:= range rubyTests
{
251 time
, err
:= Parse(test
.format
, test
.value
)
253 t
.Errorf("%s error: %v", test
.name
, err
)
255 checkTime(time
, &test
, t
)
260 func checkTime(time Time
, test
*ParseTest
, t
*testing
.T
) {
261 // The time should be Thu Feb 4 21:00:57 PST 2010
262 if test
.yearSign
>= 0 && test
.yearSign
*time
.Year() != 2010 {
263 t
.Errorf("%s: bad year: %d not %d", test
.name
, time
.Year(), 2010)
265 if time
.Month() != February
{
266 t
.Errorf("%s: bad month: %s not %s", test
.name
, time
.Month(), February
)
269 t
.Errorf("%s: bad day: %d not %d", test
.name
, time
.Day(), 4)
271 if time
.Hour() != 21 {
272 t
.Errorf("%s: bad hour: %d not %d", test
.name
, time
.Hour(), 21)
274 if time
.Minute() != 0 {
275 t
.Errorf("%s: bad minute: %d not %d", test
.name
, time
.Minute(), 0)
277 if time
.Second() != 57 {
278 t
.Errorf("%s: bad second: %d not %d", test
.name
, time
.Second(), 57)
280 // Nanoseconds must be checked against the precision of the input.
281 nanosec
, err
:= strconv
.ParseUint("012345678"[:test
.fracDigits
]+"000000000"[:9-test
.fracDigits
], 10, 0)
285 if time
.Nanosecond() != int(nanosec
) {
286 t
.Errorf("%s: bad nanosecond: %d not %d", test
.name
, time
.Nanosecond(), nanosec
)
288 name
, offset
:= time
.Zone()
289 if test
.hasTZ
&& offset
!= -28800 {
290 t
.Errorf("%s: bad tz offset: %s %d not %d", test
.name
, name
, offset
, -28800)
292 if test
.hasWD
&& time
.Weekday() != Thursday
{
293 t
.Errorf("%s: bad weekday: %s not %s", test
.name
, time
.Weekday(), Thursday
)
297 func TestFormatAndParse(t
*testing
.T
) {
298 const fmt
= "Mon MST " + RFC3339
// all fields
299 f
:= func(sec
int64) bool {
301 if t1
.Year() < 1000 || t1
.Year() > 9999 {
302 // not required to work
305 t2
, err
:= Parse(fmt
, t1
.Format(fmt
))
307 t
.Errorf("error: %s", err
)
310 if t1
.Unix() != t2
.Unix() || t1
.Nanosecond() != t2
.Nanosecond() {
311 t
.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec
, t1
, t1
.Unix(), t2
, t2
.Unix())
316 f32
:= func(sec
int32) bool { return f(int64(sec
)) }
317 cfg
:= &quick
.Config
{MaxCount
: 10000}
319 // Try a reasonable date first, then the huge ones.
320 if err
:= quick
.Check(f32
, cfg
); err
!= nil {
323 if err
:= quick
.Check(f
, cfg
); err
!= nil {
328 type ParseTimeZoneTest
struct {
334 var parseTimeZoneTests
= []ParseTimeZoneTest
{
335 {"gmt hi there", 0, false},
336 {"GMT hi there", 3, true},
337 {"GMT+12 hi there", 6, true},
338 {"GMT+00 hi there", 3, true}, // 0 or 00 is not a legal offset.
339 {"GMT-5 hi there", 5, true},
340 {"GMT-51 hi there", 3, true},
341 {"ChST hi there", 4, true},
342 {"MeST hi there", 4, true},
344 {"MSDY", 0, false}, // four letters must end in T.
345 {"ESAST hi", 5, true},
346 {"ESASTT hi", 0, false}, // run of upper-case letters too long.
347 {"ESATY hi", 0, false}, // five letters must end in T.
350 func TestParseTimeZone(t
*testing
.T
) {
351 for _
, test
:= range parseTimeZoneTests
{
352 length
, ok
:= ParseTimeZone(test
.value
)
354 t
.Errorf("expected %t for %q got %t", test
.ok
, test
.value
, ok
)
355 } else if length
!= test
.length
{
356 t
.Errorf("expected %d for %q got %d", test
.length
, test
.value
, length
)
361 type ParseErrorTest
struct {
364 expect
string // must appear within the error
367 var parseErrorTests
= []ParseErrorTest
{
368 {ANSIC
, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon
369 {ANSIC
, "Thu Feb 4 21:00:57 @2010", "cannot parse"},
370 {ANSIC
, "Thu Feb 4 21:00:60 2010", "second out of range"},
371 {ANSIC
, "Thu Feb 4 21:61:57 2010", "minute out of range"},
372 {ANSIC
, "Thu Feb 4 24:00:60 2010", "hour out of range"},
373 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot parse"},
374 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot parse"},
375 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fractional second out of range"},
376 // issue 4502. StampNano requires exactly 9 digits of precision.
377 {StampNano
, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`},
378 {StampNano
, "Dec 7 11:22:01.0000000000", "extra text: 0"},
379 // issue 4493. Helpful errors.
380 {RFC3339
, "2006-01-02T15:04:05Z07:00", `parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00`},
381 {RFC3339
, "2006-01-02T15:04_abc", `parsing time "2006-01-02T15:04_abc" as "2006-01-02T15:04:05Z07:00": cannot parse "_abc" as ":"`},
382 {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"`},
383 {RFC3339
, "2006-01-02T15:04:05Z_abc", `parsing time "2006-01-02T15:04:05Z_abc": extra text: _abc`},
386 func TestParseErrors(t
*testing
.T
) {
387 for _
, test
:= range parseErrorTests
{
388 _
, err
:= Parse(test
.format
, test
.value
)
390 t
.Errorf("expected error for %q %q", test
.format
, test
.value
)
391 } else if strings
.Index(err
.Error(), test
.expect
) < 0 {
392 t
.Errorf("expected error with %q for %q %q; got %s", test
.expect
, test
.format
, test
.value
, err
)
397 func TestNoonIs12PM(t
*testing
.T
) {
398 noon
:= Date(0, January
, 1, 12, 0, 0, 0, UTC
)
399 const expect
= "12:00PM"
400 got
:= noon
.Format("3:04PM")
402 t
.Errorf("got %q; expect %q", got
, expect
)
404 got
= noon
.Format("03:04PM")
406 t
.Errorf("got %q; expect %q", got
, expect
)
410 func TestMidnightIs12AM(t
*testing
.T
) {
411 midnight
:= Date(0, January
, 1, 0, 0, 0, 0, UTC
)
413 got
:= midnight
.Format("3:04PM")
415 t
.Errorf("got %q; expect %q", got
, expect
)
417 got
= midnight
.Format("03:04PM")
419 t
.Errorf("got %q; expect %q", got
, expect
)
423 func Test12PMIsNoon(t
*testing
.T
) {
424 noon
, err
:= Parse("3:04PM", "12:00PM")
426 t
.Fatal("error parsing date:", err
)
428 if noon
.Hour() != 12 {
429 t
.Errorf("got %d; expect 12", noon
.Hour())
431 noon
, err
= Parse("03:04PM", "12:00PM")
433 t
.Fatal("error parsing date:", err
)
435 if noon
.Hour() != 12 {
436 t
.Errorf("got %d; expect 12", noon
.Hour())
440 func Test12AMIsMidnight(t
*testing
.T
) {
441 midnight
, err
:= Parse("3:04PM", "12:00AM")
443 t
.Fatal("error parsing date:", err
)
445 if midnight
.Hour() != 0 {
446 t
.Errorf("got %d; expect 0", midnight
.Hour())
448 midnight
, err
= Parse("03:04PM", "12:00AM")
450 t
.Fatal("error parsing date:", err
)
452 if midnight
.Hour() != 0 {
453 t
.Errorf("got %d; expect 0", midnight
.Hour())
457 // Check that a time without a Zone still produces a (numeric) time zone
458 // when formatted with MST as a requested zone.
459 func TestMissingZone(t
*testing
.T
) {
460 time
, err
:= Parse(RubyDate
, "Thu Feb 02 16:10:03 -0500 2006")
462 t
.Fatal("error parsing date:", err
)
464 expect
:= "Thu Feb 2 16:10:03 -0500 2006" // -0500 not EST
465 str
:= time
.Format(UnixDate
) // uses MST as its time zone
467 t
.Errorf("got %s; expect %s", str
, expect
)
471 func TestMinutesInTimeZone(t
*testing
.T
) {
472 time
, err
:= Parse(RubyDate
, "Mon Jan 02 15:04:05 +0123 2006")
474 t
.Fatal("error parsing date:", err
)
476 expected
:= (1*60 + 23) * 60
477 _
, offset
:= time
.Zone()
478 if offset
!= expected
{
479 t
.Errorf("ZoneOffset = %d, want %d", offset
, expected
)
483 type SecondsTimeZoneOffsetTest
struct {
489 var secondsTimeZoneOffsetTests
= []SecondsTimeZoneOffsetTest
{
490 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
491 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02-00:34:08", -(34*60 + 8)},
492 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02+003408", 34*60 + 8},
493 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
494 {"2006-01-02T15:04:05Z070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
495 {"2006-01-02T15:04:05Z07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
498 func TestParseSecondsInTimeZone(t
*testing
.T
) {
499 // should accept timezone offsets with seconds like: Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
500 for _
, test
:= range secondsTimeZoneOffsetTests
{
501 time
, err
:= Parse(test
.format
, test
.value
)
503 t
.Fatal("error parsing date:", err
)
505 _
, offset
:= time
.Zone()
506 if offset
!= test
.expectedoffset
{
507 t
.Errorf("ZoneOffset = %d, want %d", offset
, test
.expectedoffset
)
512 func TestFormatSecondsInTimeZone(t
*testing
.T
) {
513 for _
, test
:= range secondsTimeZoneOffsetTests
{
514 d
:= Date(1871, 1, 1, 5, 33, 2, 0, FixedZone("LMT", test
.expectedoffset
))
515 timestr
:= d
.Format(test
.format
)
516 if timestr
!= test
.value
{
517 t
.Errorf("Format = %s, want %s", timestr
, test
.value
)