Debian Codegraph @

libgo/go/time/format_test.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package time_test
6
7 import (
8 "fmt"
9 "strconv"
10 "strings"
11 "testing"
12 "testing/quick"
13 . "time"
14 )
15
16 var nextStdChunkTests = []string{
17 "(2006)-(01)-(02)T(15):(04):(05)(Z07:00)",
18 "(2006)-(01)-(02) (002) (15):(04):(05)",
19 "(2006)-(01) (002) (15):(04):(05)",
20 "(2006)-(002) (15):(04):(05)",
21 "(2006)(002)(01) (15):(04):(05)",
22 "(2006)(002)(04) (15):(04):(05)",
23 }
24
25 func TestNextStdChunk(t *testing.T) {
26 // Most bugs in Parse or Format boil down to problems with
27 // the exact detection of format chunk boundaries in the
28 // helper function nextStdChunk (here called as NextStdChunk).
29 // This test checks nextStdChunk's behavior directly,
30 // instead of needing to test it only indirectly through Parse/Format.
31
32 // markChunks returns format with each detected
33 // 'format chunk' parenthesized.
34 // For example showChunks("2006-01-02") == "(2006)-(01)-(02)".
35 markChunks := func(format string) string {
36 // Note that NextStdChunk and StdChunkNames
37 // are not part of time's public API.
38 // They are exported in export_test for this test.
39 out := ""
40 for s := format; s != ""; {
41 prefix, std, suffix := NextStdChunk(s)
42 out += prefix
43 if std > 0 {
44 out += "(" + StdChunkNames[std] + ")"
45 }
46 s = suffix
47 }
48 return out
49 }
50
51 noParens := func(r rune) rune {
52 if r == '(' || r == ')' {
53 return -1
54 }
55 return r
56 }
57
58 for _, marked := range nextStdChunkTests {
59 // marked is an expected output from markChunks.
60 // If we delete the parens and pass it through markChunks,
61 // we should get the original back.
62 format := strings.Map(noParens, marked)
63 out := markChunks(format)
64 if out != marked {
65 t.Errorf("nextStdChunk parses %q as %q, want %q", format, out, marked)
66 }
67 }
68 }
69
70 type TimeFormatTest struct {
71 time Time
72 formattedValue string
73 }
74
75 var rfc3339Formats = []TimeFormatTest{
76 {Date(2008, 9, 17, 20, 4, 26, 0, UTC), "2008-09-17T20:04:26Z"},
77 {Date(1994, 9, 17, 20, 4, 26, 0, FixedZone("EST", -18000)), "1994-09-17T20:04:26-05:00"},
78 {Date(2000, 12, 26, 1, 15, 6, 0, FixedZone("OTO", 15600)), "2000-12-26T01:15:06+04:20"},
79 }
80
81 func TestRFC3339Conversion(t *testing.T) {
82 for _, f := range rfc3339Formats {
83 if f.time.Format(RFC3339) != f.formattedValue {
84 t.Error("RFC3339:")
85 t.Errorf(" want=%+v", f.formattedValue)
86 t.Errorf(" have=%+v", f.time.Format(RFC3339))
87 }
88 }
89 }
90
91 type FormatTest struct {
92 name string
93 format string
94 result string
95 }
96
97 var formatTests = []FormatTest{
98 {"ANSIC", ANSIC, "Wed Feb 4 21:00:57 2009"},
99 {"UnixDate", UnixDate, "Wed Feb 4 21:00:57 PST 2009"},
100 {"RubyDate", RubyDate, "Wed Feb 04 21:00:57 -0800 2009"},
101 {"RFC822", RFC822, "04 Feb 09 21:00 PST"},
102 {"RFC850", RFC850, "Wednesday, 04-Feb-09 21:00:57 PST"},
103 {"RFC1123", RFC1123, "Wed, 04 Feb 2009 21:00:57 PST"},
104 {"RFC1123Z", RFC1123Z, "Wed, 04 Feb 2009 21:00:57 -0800"},
105 {"RFC3339", RFC3339, "2009-02-04T21:00:57-08:00"},
106 {"RFC3339Nano", RFC3339Nano, "2009-02-04T21:00:57.0123456-08:00"},
107 {"Kitchen", Kitchen, "9:00PM"},
108 {"am/pm", "3pm", "9pm"},
109 {"AM/PM", "3PM", "9PM"},
110 {"two-digit year", "06 01 02", "09 02 04"},
111 // Three-letter months and days must not be followed by lower-case letter.
112 {"Janet", "Hi Janet, the Month is January", "Hi Janet, the Month is February"},
113 // Time stamps, Fractional seconds.
114 {"Stamp", Stamp, "Feb 4 21:00:57"},
115 {"StampMilli", StampMilli, "Feb 4 21:00:57.012"},
116 {"StampMicro", StampMicro, "Feb 4 21:00:57.012345"},
117 {"StampNano", StampNano, "Feb 4 21:00:57.012345600"},
118 {"YearDay", "Jan 2 002 __2 2", "Feb 4 035 35 4"},
119 }
120
121 func TestFormat(t *testing.T) {
122 // The numeric time represents Thu Feb 4 21:00:57.012345600 PST 2009
123 time := Unix(0, 1233810057012345600)
124 for _, test := range formatTests {
125 result := time.Format(test.format)
126 if result != test.result {
127 t.Errorf("%s expected %q got %q", test.name, test.result, result)
128 }
129 }
130 }
131
132 var goStringTests = []struct {
133 in Time
134 want string
135 }{
136 {Date(2009, February, 5, 5, 0, 57, 12345600, UTC),
137 "time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.UTC)"},
138 {Date(2009, February, 5, 5, 0, 57, 12345600, Local),
139 "time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.Local)"},
140 {Date(2009, February, 5, 5, 0, 57, 12345600, FixedZone("Europe/Berlin", 3*60*60)),
141 `time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.Location("Europe/Berlin"))`,
142 },
143 {Date(2009, February, 5, 5, 0, 57, 12345600, FixedZone("Non-ASCII character ⏰", 3*60*60)),
144 `time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.Location("Non-ASCII character \xe2\x8f\xb0"))`,
145 },
146 }
147
148 func TestGoString(t *testing.T) {
149 // The numeric time represents Thu Feb 4 21:00:57.012345600 PST 2009
150 for _, tt := range goStringTests {
151 if tt.in.GoString() != tt.want {
152 t.Errorf("GoString (%q): got %q want %q", tt.in, tt.in.GoString(), tt.want)
153 }
154 }
155 }
156
157 // issue 12440.
158 func TestFormatSingleDigits(t *testing.T) {
159 time := Date(2001, 2, 3, 4, 5, 6, 700000000, UTC)
160 test := FormatTest{"single digit format", "3:4:5", "4:5:6"}
161 result := time.Format(test.format)
162 if result != test.result {
163 t.Errorf("%s expected %q got %q", test.name, test.result, result)
164 }
165 }
166
167 func TestFormatShortYear(t *testing.T) {
168 years := []int{
169 -100001, -100000, -99999,
170 -10001, -10000, -9999,
171 -1001, -1000, -999,
172 -101, -100, -99,
173 -11, -10, -9,
174 -1, 0, 1,
175 9, 10, 11,
176 99, 100, 101,
177 999, 1000, 1001,
178 9999, 10000, 10001,
179 99999, 100000, 100001,
180 }
181
182 for _, y := range years {
183 time := Date(y, January, 1, 0, 0, 0, 0, UTC)
184 result := time.Format("2006.01.02")
185 var want string
186 if y < 0 {
187 // The 4 in %04d counts the - sign, so print -y instead
188 // and introduce our own - sign.
189 want = fmt.Sprintf("-%04d.%02d.%02d", -y, 1, 1)
190 } else {
191 want = fmt.Sprintf("%04d.%02d.%02d", y, 1, 1)
192 }
193 if result != want {
194 t.Errorf("(jan 1 %d).Format(\"2006.01.02\") = %q, want %q", y, result, want)
195 }
196 }
197 }
198
199 type ParseTest struct {
200 name string
201 format string
202 value string
203 hasTZ bool // contains a time zone
204 hasWD bool // contains a weekday
205 yearSign int // sign of year, -1 indicates the year is not present in the format
206 fracDigits int // number of digits of fractional second
207 }
208
209 var parseTests = []ParseTest{
210 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
211 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0},
212 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
213 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0},
214 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0},
215 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 22:00:57 PDT", true, true, 1, 0},
216 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0},
217 {"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00", true, false, 1, 0},
218 {"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},
219 // Optional fractional seconds.
220 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57.0 2010", false, true, 1, 1},
221 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57.01 PST 2010", true, true, 1, 2},
222 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57.012 -0800 2010", true, true, 1, 3},
223 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57.0123 PST", true, true, 1, 4},
224 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57.01234 PST", true, true, 1, 5},
225 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57.01234 -0800", true, true, 1, 5},
226 {"RFC3339", RFC3339, "2010-02-04T21:00:57.012345678-08:00", true, false, 1, 9},
227 {"custom: \"2006-01-02 15:04:05\"", "2006-01-02 15:04:05", "2010-02-04 21:00:57.0", false, false, 1, 0},
228 // Amount of white space should not matter.
229 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
230 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
231 // Case should not matter
232 {"ANSIC", ANSIC, "THU FEB 4 21:00:57 2010", false, true, 1, 0},
233 {"ANSIC", ANSIC, "thu feb 4 21:00:57 2010", false, true, 1, 0},
234 // Fractional seconds.
235 {"millisecond:: dot separator", "Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 21:00:57.012 2010", false, true, 1, 3},
236 {"microsecond:: dot separator", "Mon Jan _2 15:04:05.000000 2006", "Thu Feb 4 21:00:57.012345 2010", false, true, 1, 6},
237 {"nanosecond:: dot separator", "Mon Jan _2 15:04:05.000000000 2006", "Thu Feb 4 21:00:57.012345678 2010", false, true, 1, 9},
238 {"millisecond:: comma separator", "Mon Jan _2 15:04:05,000 2006", "Thu Feb 4 21:00:57.012 2010", false, true, 1, 3},
239 {"microsecond:: comma separator", "Mon Jan _2 15:04:05,000000 2006", "Thu Feb 4 21:00:57.012345 2010", false, true, 1, 6},
240 {"nanosecond:: comma separator", "Mon Jan _2 15:04:05,000000000 2006", "Thu Feb 4 21:00:57.012345678 2010", false, true, 1, 9},
241
242 // Leading zeros in other places should not be taken as fractional seconds.
243 {"zero1", "2006.01.02.15.04.05.0", "2010.02.04.21.00.57.0", false, false, 1, 1},
244 {"zero2", "2006.01.02.15.04.05.00", "2010.02.04.21.00.57.01", false, false, 1, 2},
245 // Month and day names only match when not followed by a lower-case letter.
246 {"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},
247
248 // GMT with offset.
249 {"GMT-8", UnixDate, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0},
250
251 // Accept any number of fractional second digits (including none) for .999...
252 // In Go 1, .999... was completely ignored in the format, meaning the first two
253 // cases would succeed, but the next four would not. Go 1.1 accepts all six.
254 // decimal "." separator.
255 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
256 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
257 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
258 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
259 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
260 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
261 // comma "," separator.
262 {"", "2006-01-02 15:04:05,9999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
263 {"", "2006-01-02 15:04:05,999999999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
264 {"", "2006-01-02 15:04:05,9999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
265 {"", "2006-01-02 15:04:05,999999999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
266 {"", "2006-01-02 15:04:05,9999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
267 {"", "2006-01-02 15:04:05,999999999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
268
269 // issue 4502.
270 {"", StampNano, "Feb 4 21:00:57.012345678", false, false, -1, 9},
271 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012300000", false, false, -1, 4},
272 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
273 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.0123", false, false, -1, 4},
274 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
275
276 // Day of year.
277 {"", "2006-01-02 002 15:04:05", "2010-02-04 035 21:00:57", false, false, 1, 0},
278 {"", "2006-01 002 15:04:05", "2010-02 035 21:00:57", false, false, 1, 0},
279 {"", "2006-002 15:04:05", "2010-035 21:00:57", false, false, 1, 0},
280 {"", "200600201 15:04:05", "201003502 21:00:57", false, false, 1, 0},
281 {"", "200600204 15:04:05", "201003504 21:00:57", false, false, 1, 0},
282 }
283
284 func TestParse(t *testing.T) {
285 for _, test := range parseTests {
286 time, err := Parse(test.format, test.value)
287 if err != nil {
288 t.Errorf("%s error: %v", test.name, err)
289 } else {
290 checkTime(time, &test, t)
291 }
292 }
293 }
294
295 // All parsed with ANSIC.
296 var dayOutOfRangeTests = []struct {
297 date string
298 ok bool
299 }{
300 {"Thu Jan 99 21:00:57 2010", false},
301 {"Thu Jan 31 21:00:57 2010", true},
302 {"Thu Jan 32 21:00:57 2010", false},
303 {"Thu Feb 28 21:00:57 2012", true},
304 {"Thu Feb 29 21:00:57 2012", true},
305 {"Thu Feb 29 21:00:57 2010", false},
306 {"Thu Mar 31 21:00:57 2010", true},
307 {"Thu Mar 32 21:00:57 2010", false},
308 {"Thu Apr 30 21:00:57 2010", true},
309 {"Thu Apr 31 21:00:57 2010", false},
310 {"Thu May 31 21:00:57 2010", true},
311 {"Thu May 32 21:00:57 2010", false},
312 {"Thu Jun 30 21:00:57 2010", true},
313 {"Thu Jun 31 21:00:57 2010", false},
314 {"Thu Jul 31 21:00:57 2010", true},
315 {"Thu Jul 32 21:00:57 2010", false},
316 {"Thu Aug 31 21:00:57 2010", true},
317 {"Thu Aug 32 21:00:57 2010", false},
318 {"Thu Sep 30 21:00:57 2010", true},
319 {"Thu Sep 31 21:00:57 2010", false},
320 {"Thu Oct 31 21:00:57 2010", true},
321 {"Thu Oct 32 21:00:57 2010", false},
322 {"Thu Nov 30 21:00:57 2010", true},
323 {"Thu Nov 31 21:00:57 2010", false},
324 {"Thu Dec 31 21:00:57 2010", true},
325 {"Thu Dec 32 21:00:57 2010", false},
326 {"Thu Dec 00 21:00:57 2010", false},
327 }
328
329 func TestParseDayOutOfRange(t *testing.T) {
330 for _, test := range dayOutOfRangeTests {
331 _, err := Parse(ANSIC, test.date)
332 switch {
333 case test.ok && err == nil:
334 // OK
335 case !test.ok && err != nil:
336 if !strings.Contains(err.Error(), "day out of range") {
337 t.Errorf("%q: expected 'day' error, got %v", test.date, err)
338 }
339 case test.ok && err != nil:
340 t.Errorf("%q: unexpected error: %v", test.date, err)
341 case !test.ok && err == nil:
342 t.Errorf("%q: expected 'day' error, got none", test.date)
343 }
344 }
345 }
346
347 // TestParseInLocation checks that the Parse and ParseInLocation
348 // functions do not get confused by the fact that AST (Arabia Standard
349 // Time) and AST (Atlantic Standard Time) are different time zones,
350 // even though they have the same abbreviation.
351 //
352 // ICANN has been slowly phasing out invented abbreviation in favor of
353 // numeric time zones (for example, the Asia/Baghdad time zone
354 // abbreviation got changed from AST to +03 in the 2017a tzdata
355 // release); but we still want to make sure that the time package does
356 // not get confused on systems with slightly older tzdata packages.
357 func TestParseInLocation(t *testing.T) {
358
359 baghdad, err := LoadLocation("Asia/Baghdad")
360 if err != nil {
361 t.Fatal(err)
362 }
363
364 var t1, t2 Time
365
366 t1, err = ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", baghdad)
367 if err != nil {
368 t.Fatal(err)
369 }
370
371 _, offset := t1.Zone()
372
373 // A zero offset means that ParseInLocation did not recognize the
374 // 'AST' abbreviation as matching the current location (Baghdad,
375 // where we'd expect a +03 hrs offset); likely because we're using
376 // a recent tzdata release (2017a or newer).
377 // If it happens, skip the Baghdad test.
378 if offset != 0 {
379 t2 = Date(2013, February, 1, 00, 00, 00, 0, baghdad)
380 if t1 != t2 {
381 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad) = %v, want %v", t1, t2)
382 }
383 if offset != 3*60*60 {
384 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad).Zone = _, %d, want _, %d", offset, 3*60*60)
385 }
386 }
387
388 blancSablon, err := LoadLocation("America/Blanc-Sablon")
389 if err != nil {
390 t.Fatal(err)
391 }
392
393 // In this case 'AST' means 'Atlantic Standard Time', and we
394 // expect the abbreviation to correctly match the american
395 // location.
396 t1, err = ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", blancSablon)
397 if err != nil {
398 t.Fatal(err)
399 }
400 t2 = Date(2013, February, 1, 00, 00, 00, 0, blancSablon)
401 if t1 != t2 {
402 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon) = %v, want %v", t1, t2)
403 }
404 _, offset = t1.Zone()
405 if offset != -4*60*60 {
406 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon).Zone = _, %d, want _, %d", offset, -4*60*60)
407 }
408 }
409
410 func TestLoadLocationZipFile(t *testing.T) {
411 t.Skip("gccgo does not use the zip file")
412
413 ForceZipFileForTesting(true)
414 defer ForceZipFileForTesting(false)
415
416 _, err := LoadLocation("Australia/Sydney")
417 if err != nil {
418 t.Fatal(err)
419 }
420 }
421
422 var rubyTests = []ParseTest{
423 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
424 // Ignore the time zone in the test. If it parses, it'll be OK.
425 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0000 2010", false, true, 1, 0},
426 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +0000 2010", false, true, 1, 0},
427 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +1130 2010", false, true, 1, 0},
428 }
429
430 // Problematic time zone format needs special tests.
431 func TestRubyParse(t *testing.T) {
432 for _, test := range rubyTests {
433 time, err := Parse(test.format, test.value)
434 if err != nil {
435 t.Errorf("%s error: %v", test.name, err)
436 } else {
437 checkTime(time, &test, t)
438 }
439 }
440 }
441
442 func checkTime(time Time, test *ParseTest, t *testing.T) {
443 // The time should be Thu Feb 4 21:00:57 PST 2010
444 if test.yearSign >= 0 && test.yearSign*time.Year() != 2010 {
445 t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010)
446 }
447 if time.Month() != February {
448 t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), February)
449 }
450 if time.Day() != 4 {
451 t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4)
452 }
453 if time.Hour() != 21 {
454 t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21)
455 }
456 if time.Minute() != 0 {
457 t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute(), 0)
458 }
459 if time.Second() != 57 {
460 t.Errorf("%s: bad second: %d not %d", test.name, time.Second(), 57)
461 }
462 // Nanoseconds must be checked against the precision of the input.
463 nanosec, err := strconv.ParseUint("012345678"[:test.fracDigits]+"000000000"[:9-test.fracDigits], 10, 0)
464 if err != nil {
465 panic(err)
466 }
467 if time.Nanosecond() != int(nanosec) {
468 t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanosecond(), nanosec)
469 }
470 name, offset := time.Zone()
471 if test.hasTZ && offset != -28800 {
472 t.Errorf("%s: bad tz offset: %s %d not %d", test.name, name, offset, -28800)
473 }
474 if test.hasWD && time.Weekday() != Thursday {
475 t.Errorf("%s: bad weekday: %s not %s", test.name, time.Weekday(), Thursday)
476 }
477 }
478
479 func TestFormatAndParse(t *testing.T) {
480 const fmt = "Mon MST " + RFC3339 // all fields
481 f := func(sec int64) bool {
482 t1 := Unix(sec/2, 0)
483 if t1.Year() < 1000 || t1.Year() > 9999 || t1.Unix() != sec {
484 // not required to work
485 return true
486 }
487 t2, err := Parse(fmt, t1.Format(fmt))
488 if err != nil {
489 t.Errorf("error: %s", err)
490 return false
491 }
492 if t1.Unix() != t2.Unix() || t1.Nanosecond() != t2.Nanosecond() {
493 t.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec, t1, t1.Unix(), t2, t2.Unix())
494 return false
495 }
496 return true
497 }
498 f32 := func(sec int32) bool { return f(int64(sec)) }
499 cfg := &quick.Config{MaxCount: 10000}
500
501 // Try a reasonable date first, then the huge ones.
502 if err := quick.Check(f32, cfg); err != nil {
503 t.Fatal(err)
504 }
505 if err := quick.Check(f, cfg); err != nil {
506 t.Fatal(err)
507 }
508 }
509
510 type ParseTimeZoneTest struct {
511 value string
512 length int
513 ok bool
514 }
515
516 var parseTimeZoneTests = []ParseTimeZoneTest{
517 {"gmt hi there", 0, false},
518 {"GMT hi there", 3, true},
519 {"GMT+12 hi there", 6, true},
520 {"GMT+00 hi there", 6, true},
521 {"GMT+", 3, true},
522 {"GMT+3", 5, true},
523 {"GMT+a", 3, true},
524 {"GMT+3a", 5, true},
525 {"GMT-5 hi there", 5, true},
526 {"GMT-51 hi there", 3, true},
527 {"ChST hi there", 4, true},
528 {"MeST hi there", 4, true},
529 {"MSDx", 3, true},
530 {"MSDY", 0, false}, // four letters must end in T.
531 {"ESAST hi", 5, true},
532 {"ESASTT hi", 0, false}, // run of upper-case letters too long.
533 {"ESATY hi", 0, false}, // five letters must end in T.
534 {"WITA hi", 4, true}, // Issue #18251
535 // Issue #24071
536 {"+03 hi", 3, true},
537 {"-04 hi", 3, true},
538 // Issue #26032
539 {"+00", 3, true},
540 {"-11", 3, true},
541 {"-12", 3, true},
542 {"-23", 3, true},
543 {"-24", 0, false},
544 {"+13", 3, true},
545 {"+14", 3, true},
546 {"+23", 3, true},
547 {"+24", 0, false},
548 }
549
550 func TestParseTimeZone(t *testing.T) {
551 for _, test := range parseTimeZoneTests {
552 length, ok := ParseTimeZone(test.value)
553 if ok != test.ok {
554 t.Errorf("expected %t for %q got %t", test.ok, test.value, ok)
555 } else if length != test.length {
556 t.Errorf("expected %d for %q got %d", test.length, test.value, length)
557 }
558 }
559 }
560
561 type ParseErrorTest struct {
562 format string
563 value string
564 expect string // must appear within the error
565 }
566
567 var parseErrorTests = []ParseErrorTest{
568 {ANSIC, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon
569 {ANSIC, "Thu Feb 4 21:00:57 @2010", "cannot parse"},
570 {ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"},
571 {ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"},
572 {ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"},
573 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot parse"},
574 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot parse"},
575 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fractional second out of range"},
576 // issue 4502. StampNano requires exactly 9 digits of precision.
577 {StampNano, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`},
578 {StampNano, "Dec 7 11:22:01.0000000000", `extra text: "0"`},
579 // issue 4493. Helpful errors.
580 {RFC3339, "2006-01-02T15:04:05Z07:00", `parsing time "2006-01-02T15:04:05Z07:00": extra text: "07:00"`},
581 {RFC3339, "2006-01-02T15:04_abc", `parsing time "2006-01-02T15:04_abc" as "2006-01-02T15:04:05Z07:00": cannot parse "_abc" as ":"`},
582 {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"`},
583 {RFC3339, "2006-01-02T15:04:05Z_abc", `parsing time "2006-01-02T15:04:05Z_abc": extra text: "_abc"`},
584 // invalid second followed by optional fractional seconds
585 {RFC3339, "2010-02-04T21:00:67.012345678-08:00", "second out of range"},
586 // issue 21113
587 {"_2 Jan 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"},
588 {"_2 January 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"},
589
590 // invalid or mismatched day-of-year
591 {"Jan _2 002 2006", "Feb 4 034 2006", "day-of-year does not match day"},
592 {"Jan _2 002 2006", "Feb 4 004 2006", "day-of-year does not match month"},
593
594 // issue 45391.
595 {`"2006-01-02T15:04:05Z07:00"`, "0", `parsing time "0" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "0" as "\""`},
596 {RFC3339, "\"", `parsing time "\"" as "2006-01-02T15:04:05Z07:00": cannot parse "\"" as "2006"`},
597 }
598
599 func TestParseErrors(t *testing.T) {
600 for _, test := range parseErrorTests {
601 _, err := Parse(test.format, test.value)
602 if err == nil {
603 t.Errorf("expected error for %q %q", test.format, test.value)
604 } else if !strings.Contains(err.Error(), test.expect) {
605 t.Errorf("expected error with %q for %q %q; got %s", test.expect, test.format, test.value, err)
606 }
607 }
608 }
609
610 func TestNoonIs12PM(t *testing.T) {
611 noon := Date(0, January, 1, 12, 0, 0, 0, UTC)
612 const expect = "12:00PM"
613 got := noon.Format("3:04PM")
614 if got != expect {
615 t.Errorf("got %q; expect %q", got, expect)
616 }
617 got = noon.Format("03:04PM")
618 if got != expect {
619 t.Errorf("got %q; expect %q", got, expect)
620 }
621 }
622
623 func TestMidnightIs12AM(t *testing.T) {
624 midnight := Date(0, January, 1, 0, 0, 0, 0, UTC)
625 expect := "12:00AM"
626 got := midnight.Format("3:04PM")
627 if got != expect {
628 t.Errorf("got %q; expect %q", got, expect)
629 }
630 got = midnight.Format("03:04PM")
631 if got != expect {
632 t.Errorf("got %q; expect %q", got, expect)
633 }
634 }
635
636 func Test12PMIsNoon(t *testing.T) {
637 noon, err := Parse("3:04PM", "12:00PM")
638 if err != nil {
639 t.Fatal("error parsing date:", err)
640 }
641 if noon.Hour() != 12 {
642 t.Errorf("got %d; expect 12", noon.Hour())
643 }
644 noon, err = Parse("03:04PM", "12:00PM")
645 if err != nil {
646 t.Fatal("error parsing date:", err)
647 }
648 if noon.Hour() != 12 {
649 t.Errorf("got %d; expect 12", noon.Hour())
650 }
651 }
652
653 func Test12AMIsMidnight(t *testing.T) {
654 midnight, err := Parse("3:04PM", "12:00AM")
655 if err != nil {
656 t.Fatal("error parsing date:", err)
657 }
658 if midnight.Hour() != 0 {
659 t.Errorf("got %d; expect 0", midnight.Hour())
660 }
661 midnight, err = Parse("03:04PM", "12:00AM")
662 if err != nil {
663 t.Fatal("error parsing date:", err)
664 }
665 if midnight.Hour() != 0 {
666 t.Errorf("got %d; expect 0", midnight.Hour())
667 }
668 }
669
670 // Check that a time without a Zone still produces a (numeric) time zone
671 // when formatted with MST as a requested zone.
672 func TestMissingZone(t *testing.T) {
673 time, err := Parse(RubyDate, "Thu Feb 02 16:10:03 -0500 2006")
674 if err != nil {
675 t.Fatal("error parsing date:", err)
676 }
677 expect := "Thu Feb 2 16:10:03 -0500 2006" // -0500 not EST
678 str := time.Format(UnixDate) // uses MST as its time zone
679 if str != expect {
680 t.Errorf("got %s; expect %s", str, expect)
681 }
682 }
683
684 func TestMinutesInTimeZone(t *testing.T) {
685 time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006")
686 if err != nil {
687 t.Fatal("error parsing date:", err)
688 }
689 expected := (1*60 + 23) * 60
690 _, offset := time.Zone()
691 if offset != expected {
692 t.Errorf("ZoneOffset = %d, want %d", offset, expected)
693 }
694 }
695
696 type SecondsTimeZoneOffsetTest struct {
697 format string
698 value string
699 expectedoffset int
700 }
701
702 var secondsTimeZoneOffsetTests = []SecondsTimeZoneOffsetTest{
703 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
704 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02-00:34:08", -(34*60 + 8)},
705 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02+003408", 34*60 + 8},
706 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
707 {"2006-01-02T15:04:05Z070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
708 {"2006-01-02T15:04:05Z07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
709 {"2006-01-02T15:04:05-07", "1871-01-01T05:33:02+01", 1 * 60 * 60},
710 {"2006-01-02T15:04:05-07", "1871-01-01T05:33:02-02", -2 * 60 * 60},
711 {"2006-01-02T15:04:05Z07", "1871-01-01T05:33:02-02", -2 * 60 * 60},
712 }
713
714 func TestParseSecondsInTimeZone(t *testing.T) {
715 // should accept timezone offsets with seconds like: Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
716 for _, test := range secondsTimeZoneOffsetTests {
717 time, err := Parse(test.format, test.value)
718 if err != nil {
719 t.Fatal("error parsing date:", err)
720 }
721 _, offset := time.Zone()
722 if offset != test.expectedoffset {
723 t.Errorf("ZoneOffset = %d, want %d", offset, test.expectedoffset)
724 }
725 }
726 }
727
728 func TestFormatSecondsInTimeZone(t *testing.T) {
729 for _, test := range secondsTimeZoneOffsetTests {
730 d := Date(1871, 1, 1, 5, 33, 2, 0, FixedZone("LMT", test.expectedoffset))
731 timestr := d.Format(test.format)
732 if timestr != test.value {
733 t.Errorf("Format = %s, want %s", timestr, test.value)
734 }
735 }
736 }
737
738 // Issue 11334.
739 func TestUnderscoreTwoThousand(t *testing.T) {
740 format := "15:04_20060102"
741 input := "14:38_20150618"
742 time, err := Parse(format, input)
743 if err != nil {
744 t.Error(err)
745 }
746 if y, m, d := time.Date(); y != 2015 || m != 6 || d != 18 {
747 t.Errorf("Incorrect y/m/d, got %d/%d/%d", y, m, d)
748 }
749 if h := time.Hour(); h != 14 {
750 t.Errorf("Incorrect hour, got %d", h)
751 }
752 if m := time.Minute(); m != 38 {
753 t.Errorf("Incorrect minute, got %d", m)
754 }
755 }
756
757 // Issue 29918, 29916
758 func TestStd0xParseError(t *testing.T) {
759 tests := []struct {
760 format, value, valueElemPrefix string
761 }{
762 {"01 MST", "0 MST", "0"},
763 {"01 MST", "1 MST", "1"},
764 {RFC850, "Thursday, 04-Feb-1 21:00:57 PST", "1"},
765 }
766 for _, tt := range tests {
767 _, err := Parse(tt.format, tt.value)
768 if err == nil {
769 t.Errorf("Parse(%q, %q) did not fail as expected", tt.format, tt.value)
770 } else if perr, ok := err.(*ParseError); !ok {
771 t.Errorf("Parse(%q, %q) returned error type %T, expected ParseError", tt.format, tt.value, perr)
772 } else if !strings.Contains(perr.Error(), "cannot parse") || !strings.HasPrefix(perr.ValueElem, tt.valueElemPrefix) {
773 t.Errorf("Parse(%q, %q) returned wrong parsing error message: %v", tt.format, tt.value, perr)
774 }
775 }
776 }
777
778 var monthOutOfRangeTests = []struct {
779 value string
780 ok bool
781 }{
782 {"00-01", false},
783 {"13-01", false},
784 {"01-01", true},
785 }
786
787 func TestParseMonthOutOfRange(t *testing.T) {
788 for _, test := range monthOutOfRangeTests {
789 _, err := Parse("01-02", test.value)
790 switch {
791 case !test.ok && err != nil:
792 if !strings.Contains(err.Error(), "month out of range") {
793 t.Errorf("%q: expected 'month' error, got %v", test.value, err)
794 }
795 case test.ok && err != nil:
796 t.Errorf("%q: unexpected error: %v", test.value, err)
797 case !test.ok && err == nil:
798 t.Errorf("%q: expected 'month' error, got none", test.value)
799 }
800 }
801 }
802
803 // Issue 37387.
804 func TestParseYday(t *testing.T) {
805 t.Parallel()
806 for i := 1; i <= 365; i++ {
807 d := fmt.Sprintf("2020-%03d", i)
808 tm, err := Parse("2006-002", d)
809 if err != nil {
810 t.Errorf("unexpected error for %s: %v", d, err)
811 } else if tm.Year() != 2020 || tm.YearDay() != i {
812 t.Errorf("got year %d yearday %d, want %d %d", tm.Year(), tm.YearDay(), 2020, i)
813 }
814 }
815 }
816
817 // Issue 45391.
818 func TestQuote(t *testing.T) {
819 tests := []struct {
820 s, want string
821 }{
822 {`"`, `"\""`},
823 {`abc"xyz"`, `"abc\"xyz\""`},
824 {"", `""`},
825 {"abc", `"abc"`},
826 {`☺`, `"\xe2\x98\xba"`},
827 {`☺ hello ☺ hello`, `"\xe2\x98\xba hello \xe2\x98\xba hello"`},
828 {"\x04", `"\x04"`},
829 }
830 for _, tt := range tests {
831 if q := Quote(tt.s); q != tt.want {
832 t.Errorf("Quote(%q) = got %q, want %q", tt.s, q, tt.want)
833 }
834 }
835
836 }
837
838 // Issue 48037
839 func TestFormatFractionalSecondSeparators(t *testing.T) {
840 tests := []struct {
841 s, want string
842 }{
843 {`15:04:05.000`, `21:00:57.012`},
844 {`15:04:05.999`, `21:00:57.012`},
845 {`15:04:05,000`, `21:00:57,012`},
846 {`15:04:05,999`, `21:00:57,012`},
847 }
848
849 // The numeric time represents Thu Feb 4 21:00:57.012345600 PST 2009
850 time := Unix(0, 1233810057012345600)
851 for _, tt := range tests {
852 if q := time.Format(tt.s); q != tt.want {
853 t.Errorf("Format(%q) = got %q, want %q", tt.s, q, tt.want)
854 }
855 }
856 }
857
858 // Issue 48685
859 func TestParseFractionalSecondsLongerThanNineDigits(t *testing.T) {
860 tests := []struct {
861 s string
862 want int
863 }{
864 // 9 digits
865 {"2021-09-29T16:04:33.000000000Z", 0},
866 {"2021-09-29T16:04:33.000000001Z", 1},
867 {"2021-09-29T16:04:33.100000000Z", 100_000_000},
868 {"2021-09-29T16:04:33.100000001Z", 100_000_001},
869 {"2021-09-29T16:04:33.999999999Z", 999_999_999},
870 {"2021-09-29T16:04:33.012345678Z", 12_345_678},
871 // 10 digits, truncates
872 {"2021-09-29T16:04:33.0000000000Z", 0},
873 {"2021-09-29T16:04:33.0000000001Z", 0},
874 {"2021-09-29T16:04:33.1000000000Z", 100_000_000},
875 {"2021-09-29T16:04:33.1000000009Z", 100_000_000},
876 {"2021-09-29T16:04:33.9999999999Z", 999_999_999},
877 {"2021-09-29T16:04:33.0123456789Z", 12_345_678},
878 // 11 digits, truncates
879 {"2021-09-29T16:04:33.10000000000Z", 100_000_000},
880 {"2021-09-29T16:04:33.00123456789Z", 1_234_567},
881 // 12 digits, truncates
882 {"2021-09-29T16:04:33.000123456789Z", 123_456},
883 // 15 digits, truncates
884 {"2021-09-29T16:04:33.9999999999999999Z", 999_999_999},
885 }
886
887 for _, tt := range tests {
888 tm, err := Parse(RFC3339, tt.s)
889 if err != nil {
890 t.Errorf("Unexpected error: %v", err)
891 continue
892 }
893 if got := tm.Nanosecond(); got != tt.want {
894 t.Errorf("Parse(%q) = got %d, want %d", tt.s, got, tt.want)
895 }
896 }
897 }