Source file src/time/format_test.go

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

View as plain text