OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package time_test | 5 package time_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/gob" | 9 "encoding/gob" |
10 "encoding/json" | 10 "encoding/json" |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 } | 462 } |
463 } | 463 } |
464 } | 464 } |
465 | 465 |
466 type ParseTest struct { | 466 type ParseTest struct { |
467 name string | 467 name string |
468 format string | 468 format string |
469 value string | 469 value string |
470 hasTZ bool // contains a time zone | 470 hasTZ bool // contains a time zone |
471 hasWD bool // contains a weekday | 471 hasWD bool // contains a weekday |
472 » yearSign int // sign of year | 472 » yearSign int // sign of year, -1 indicates the year is not present in
the format |
473 fracDigits int // number of digits of fractional second | 473 fracDigits int // number of digits of fractional second |
474 } | 474 } |
475 | 475 |
476 var parseTests = []ParseTest{ | 476 var parseTests = []ParseTest{ |
477 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0}, | 477 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0}, |
478 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0}
, | 478 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0}
, |
479 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1,
0}, | 479 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1,
0}, |
480 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0}
, | 480 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0}
, |
481 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0}, | 481 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0}, |
482 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1,
0}, | 482 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1,
0}, |
(...skipping 24 matching lines...) Expand all Loading... |
507 | 507 |
508 // Accept any number of fractional second digits (including none) for .9
99... | 508 // Accept any number of fractional second digits (including none) for .9
99... |
509 // In Go 1, .999... was completely ignored in the format, meaning the fi
rst two | 509 // In Go 1, .999... was completely ignored in the format, meaning the fi
rst two |
510 // cases would succeed, but the next four would not. Go 1.1 accepts all
six. | 510 // cases would succeed, but the next four would not. Go 1.1 accepts all
six. |
511 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57 -0800 PS
T", true, false, 1, 0}, | 511 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57 -0800 PS
T", true, false, 1, 0}, |
512 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57 -08
00 PST", true, false, 1, 0}, | 512 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57 -08
00 PST", true, false, 1, 0}, |
513 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.0123 -08
00 PST", true, false, 1, 4}, | 513 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.0123 -08
00 PST", true, false, 1, 4}, |
514 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012
3 -0800 PST", true, false, 1, 4}, | 514 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012
3 -0800 PST", true, false, 1, 4}, |
515 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.01234567
8 -0800 PST", true, false, 1, 9}, | 515 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.01234567
8 -0800 PST", true, false, 1, 9}, |
516 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012
345678 -0800 PST", true, false, 1, 9}, | 516 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012
345678 -0800 PST", true, false, 1, 9}, |
| 517 |
| 518 // issue 4502. |
| 519 {"", StampNano, "Feb 4 21:00:57.012345678", false, false, -1, 9}, |
| 520 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012300000", false, false, -
1, 4}, |
| 521 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.012345678", false, fa
lse, -1, 9}, |
517 } | 522 } |
518 | 523 |
519 func TestParse(t *testing.T) { | 524 func TestParse(t *testing.T) { |
520 for _, test := range parseTests { | 525 for _, test := range parseTests { |
521 time, err := Parse(test.format, test.value) | 526 time, err := Parse(test.format, test.value) |
522 if err != nil { | 527 if err != nil { |
523 t.Errorf("%s error: %v", test.name, err) | 528 t.Errorf("%s error: %v", test.name, err) |
524 } else { | 529 } else { |
525 checkTime(time, &test, t) | 530 checkTime(time, &test, t) |
526 } | 531 } |
(...skipping 15 matching lines...) Expand all Loading... |
542 if err != nil { | 547 if err != nil { |
543 t.Errorf("%s error: %v", test.name, err) | 548 t.Errorf("%s error: %v", test.name, err) |
544 } else { | 549 } else { |
545 checkTime(time, &test, t) | 550 checkTime(time, &test, t) |
546 } | 551 } |
547 } | 552 } |
548 } | 553 } |
549 | 554 |
550 func checkTime(time Time, test *ParseTest, t *testing.T) { | 555 func checkTime(time Time, test *ParseTest, t *testing.T) { |
551 // The time should be Thu Feb 4 21:00:57 PST 2010 | 556 // The time should be Thu Feb 4 21:00:57 PST 2010 |
552 » if test.yearSign*time.Year() != 2010 { | 557 » if test.yearSign >= 0 && test.yearSign*time.Year() != 2010 { |
553 t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010
) | 558 t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010
) |
554 } | 559 } |
555 if time.Month() != February { | 560 if time.Month() != February { |
556 t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), Fe
bruary) | 561 t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), Fe
bruary) |
557 } | 562 } |
558 if time.Day() != 4 { | 563 if time.Day() != 4 { |
559 t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4) | 564 t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4) |
560 } | 565 } |
561 if time.Hour() != 21 { | 566 if time.Hour() != 21 { |
562 t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21) | 567 t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 | 628 |
624 var parseErrorTests = []ParseErrorTest{ | 629 var parseErrorTests = []ParseErrorTest{ |
625 {ANSIC, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as
Mon | 630 {ANSIC, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as
Mon |
626 {ANSIC, "Thu Feb 4 21:00:57 @2010", "cannot parse"}, | 631 {ANSIC, "Thu Feb 4 21:00:57 @2010", "cannot parse"}, |
627 {ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"}, | 632 {ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"}, |
628 {ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"}, | 633 {ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"}, |
629 {ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"}, | 634 {ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"}, |
630 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot
parse"}, | 635 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot
parse"}, |
631 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot
parse"}, | 636 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot
parse"}, |
632 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fract
ional second out of range"}, | 637 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fract
ional second out of range"}, |
| 638 // issue 4502. StampNano requires exactly 9 digits of precision. |
| 639 {StampNano, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".00000
0000"`}, |
| 640 {StampNano, "Dec 7 11:22:01.0000000000", "extra text: 0"}, |
633 } | 641 } |
634 | 642 |
635 func TestParseErrors(t *testing.T) { | 643 func TestParseErrors(t *testing.T) { |
636 for _, test := range parseErrorTests { | 644 for _, test := range parseErrorTests { |
637 _, err := Parse(test.format, test.value) | 645 _, err := Parse(test.format, test.value) |
638 if err == nil { | 646 if err == nil { |
639 t.Errorf("expected error for %q %q", test.format, test.v
alue) | 647 t.Errorf("expected error for %q %q", test.format, test.v
alue) |
640 } else if strings.Index(err.Error(), test.expect) < 0 { | 648 } else if strings.Index(err.Error(), test.expect) < 0 { |
641 t.Errorf("expected error with %q for %q %q; got %s", tes
t.expect, test.format, test.value, err) | 649 t.Errorf("expected error with %q for %q %q; got %s", tes
t.expect, test.format, test.value, err) |
642 } | 650 } |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1303 _ = t.Year() | 1311 _ = t.Year() |
1304 } | 1312 } |
1305 } | 1313 } |
1306 | 1314 |
1307 func BenchmarkDay(b *testing.B) { | 1315 func BenchmarkDay(b *testing.B) { |
1308 t := Now() | 1316 t := Now() |
1309 for i := 0; i < b.N; i++ { | 1317 for i := 0; i < b.N; i++ { |
1310 _ = t.Day() | 1318 _ = t.Day() |
1311 } | 1319 } |
1312 } | 1320 } |
OLD | NEW |