github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/src/time/format_test.go (about)

     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  type TimeFormatTest struct {
    17  	time           Time
    18  	formattedValue string
    19  }
    20  
    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"},
    25  }
    26  
    27  func TestRFC3339Conversion(t *testing.T) {
    28  	for _, f := range rfc3339Formats {
    29  		if f.time.Format(RFC3339) != f.formattedValue {
    30  			t.Error("RFC3339:")
    31  			t.Errorf("  want=%+v", f.formattedValue)
    32  			t.Errorf("  have=%+v", f.time.Format(RFC3339))
    33  		}
    34  	}
    35  }
    36  
    37  type FormatTest struct {
    38  	name   string
    39  	format string
    40  	result string
    41  }
    42  
    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"},
    64  }
    65  
    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)
    73  		}
    74  	}
    75  }
    76  
    77  func TestFormatShortYear(t *testing.T) {
    78  	years := []int{
    79  		-100001, -100000, -99999,
    80  		-10001, -10000, -9999,
    81  		-1001, -1000, -999,
    82  		-101, -100, -99,
    83  		-11, -10, -9,
    84  		-1, 0, 1,
    85  		9, 10, 11,
    86  		99, 100, 101,
    87  		999, 1000, 1001,
    88  		9999, 10000, 10001,
    89  		99999, 100000, 100001,
    90  	}
    91  
    92  	for _, y := range years {
    93  		time := Date(y, January, 1, 0, 0, 0, 0, UTC)
    94  		result := time.Format("2006.01.02")
    95  		var want string
    96  		if y < 0 {
    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)
   100  		} else {
   101  			want = fmt.Sprintf("%04d.%02d.%02d", y, 1, 1)
   102  		}
   103  		if result != want {
   104  			t.Errorf("(jan 1 %d).Format(\"2006.01.02\") = %q, want %q", y, result, want)
   105  		}
   106  	}
   107  }
   108  
   109  type ParseTest struct {
   110  	name       string
   111  	format     string
   112  	value      string
   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
   117  }
   118  
   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},
   153  
   154  	// GMT with offset.
   155  	{"GMT-8", UnixDate, "Fri Feb  5 05:00:57 GMT-8 2010", true, true, 1, 0},
   156  
   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},
   166  
   167  	// issue 4502.
   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},
   173  }
   174  
   175  func TestParse(t *testing.T) {
   176  	for _, test := range parseTests {
   177  		time, err := Parse(test.format, test.value)
   178  		if err != nil {
   179  			t.Errorf("%s error: %v", test.name, err)
   180  		} else {
   181  			checkTime(time, &test, t)
   182  		}
   183  	}
   184  }
   185  
   186  func TestParseInLocation(t *testing.T) {
   187  	t.Skip("skipping test for Go 1.4; Issue 19457")
   188  
   189  	// Check that Parse (and ParseInLocation) understand that
   190  	// Feb 01 AST (Arabia Standard Time) and Feb 01 AST (Atlantic Standard Time)
   191  	// are in different time zones even though both are called AST
   192  
   193  	baghdad, err := LoadLocation("Asia/Baghdad")
   194  	if err != nil {
   195  		t.Fatal(err)
   196  	}
   197  
   198  	t1, err := ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", baghdad)
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  	t2 := Date(2013, February, 1, 00, 00, 00, 0, baghdad)
   203  	if t1 != t2 {
   204  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad) = %v, want %v", t1, t2)
   205  	}
   206  	_, offset := t1.Zone()
   207  	if offset != 3*60*60 {
   208  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad).Zone = _, %d, want _, %d", offset, 3*60*60)
   209  	}
   210  
   211  	blancSablon, err := LoadLocation("America/Blanc-Sablon")
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  
   216  	t1, err = ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", blancSablon)
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  	t2 = Date(2013, February, 1, 00, 00, 00, 0, blancSablon)
   221  	if t1 != t2 {
   222  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon) = %v, want %v", t1, t2)
   223  	}
   224  	_, offset = t1.Zone()
   225  	if offset != -4*60*60 {
   226  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon).Zone = _, %d, want _, %d", offset, -4*60*60)
   227  	}
   228  }
   229  
   230  func TestLoadLocationZipFile(t *testing.T) {
   231  	ForceZipFileForTesting(true)
   232  	defer ForceZipFileForTesting(false)
   233  
   234  	_, err := LoadLocation("Australia/Sydney")
   235  	if err != nil {
   236  		t.Fatal(err)
   237  	}
   238  }
   239  
   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},
   246  }
   247  
   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)
   252  		if err != nil {
   253  			t.Errorf("%s error: %v", test.name, err)
   254  		} else {
   255  			checkTime(time, &test, t)
   256  		}
   257  	}
   258  }
   259  
   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)
   264  	}
   265  	if time.Month() != February {
   266  		t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), February)
   267  	}
   268  	if time.Day() != 4 {
   269  		t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4)
   270  	}
   271  	if time.Hour() != 21 {
   272  		t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21)
   273  	}
   274  	if time.Minute() != 0 {
   275  		t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute(), 0)
   276  	}
   277  	if time.Second() != 57 {
   278  		t.Errorf("%s: bad second: %d not %d", test.name, time.Second(), 57)
   279  	}
   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)
   282  	if err != nil {
   283  		panic(err)
   284  	}
   285  	if time.Nanosecond() != int(nanosec) {
   286  		t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanosecond(), nanosec)
   287  	}
   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)
   291  	}
   292  	if test.hasWD && time.Weekday() != Thursday {
   293  		t.Errorf("%s: bad weekday: %s not %s", test.name, time.Weekday(), Thursday)
   294  	}
   295  }
   296  
   297  func TestFormatAndParse(t *testing.T) {
   298  	const fmt = "Mon MST " + RFC3339 // all fields
   299  	f := func(sec int64) bool {
   300  		t1 := Unix(sec, 0)
   301  		if t1.Year() < 1000 || t1.Year() > 9999 {
   302  			// not required to work
   303  			return true
   304  		}
   305  		t2, err := Parse(fmt, t1.Format(fmt))
   306  		if err != nil {
   307  			t.Errorf("error: %s", err)
   308  			return false
   309  		}
   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())
   312  			return false
   313  		}
   314  		return true
   315  	}
   316  	f32 := func(sec int32) bool { return f(int64(sec)) }
   317  	cfg := &quick.Config{MaxCount: 10000}
   318  
   319  	// Try a reasonable date first, then the huge ones.
   320  	if err := quick.Check(f32, cfg); err != nil {
   321  		t.Fatal(err)
   322  	}
   323  	if err := quick.Check(f, cfg); err != nil {
   324  		t.Fatal(err)
   325  	}
   326  }
   327  
   328  type ParseTimeZoneTest struct {
   329  	value  string
   330  	length int
   331  	ok     bool
   332  }
   333  
   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},
   343  	{"MSDx", 3, 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.
   348  }
   349  
   350  func TestParseTimeZone(t *testing.T) {
   351  	for _, test := range parseTimeZoneTests {
   352  		length, ok := ParseTimeZone(test.value)
   353  		if ok != test.ok {
   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)
   357  		}
   358  	}
   359  }
   360  
   361  type ParseErrorTest struct {
   362  	format string
   363  	value  string
   364  	expect string // must appear within the error
   365  }
   366  
   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`},
   384  }
   385  
   386  func TestParseErrors(t *testing.T) {
   387  	for _, test := range parseErrorTests {
   388  		_, err := Parse(test.format, test.value)
   389  		if err == nil {
   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)
   393  		}
   394  	}
   395  }
   396  
   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")
   401  	if got != expect {
   402  		t.Errorf("got %q; expect %q", got, expect)
   403  	}
   404  	got = noon.Format("03:04PM")
   405  	if got != expect {
   406  		t.Errorf("got %q; expect %q", got, expect)
   407  	}
   408  }
   409  
   410  func TestMidnightIs12AM(t *testing.T) {
   411  	midnight := Date(0, January, 1, 0, 0, 0, 0, UTC)
   412  	expect := "12:00AM"
   413  	got := midnight.Format("3:04PM")
   414  	if got != expect {
   415  		t.Errorf("got %q; expect %q", got, expect)
   416  	}
   417  	got = midnight.Format("03:04PM")
   418  	if got != expect {
   419  		t.Errorf("got %q; expect %q", got, expect)
   420  	}
   421  }
   422  
   423  func Test12PMIsNoon(t *testing.T) {
   424  	noon, err := Parse("3:04PM", "12:00PM")
   425  	if err != nil {
   426  		t.Fatal("error parsing date:", err)
   427  	}
   428  	if noon.Hour() != 12 {
   429  		t.Errorf("got %d; expect 12", noon.Hour())
   430  	}
   431  	noon, err = Parse("03:04PM", "12:00PM")
   432  	if err != nil {
   433  		t.Fatal("error parsing date:", err)
   434  	}
   435  	if noon.Hour() != 12 {
   436  		t.Errorf("got %d; expect 12", noon.Hour())
   437  	}
   438  }
   439  
   440  func Test12AMIsMidnight(t *testing.T) {
   441  	midnight, err := Parse("3:04PM", "12:00AM")
   442  	if err != nil {
   443  		t.Fatal("error parsing date:", err)
   444  	}
   445  	if midnight.Hour() != 0 {
   446  		t.Errorf("got %d; expect 0", midnight.Hour())
   447  	}
   448  	midnight, err = Parse("03:04PM", "12:00AM")
   449  	if err != nil {
   450  		t.Fatal("error parsing date:", err)
   451  	}
   452  	if midnight.Hour() != 0 {
   453  		t.Errorf("got %d; expect 0", midnight.Hour())
   454  	}
   455  }
   456  
   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")
   461  	if err != nil {
   462  		t.Fatal("error parsing date:", err)
   463  	}
   464  	expect := "Thu Feb  2 16:10:03 -0500 2006" // -0500 not EST
   465  	str := time.Format(UnixDate)               // uses MST as its time zone
   466  	if str != expect {
   467  		t.Errorf("got %s; expect %s", str, expect)
   468  	}
   469  }
   470  
   471  func TestMinutesInTimeZone(t *testing.T) {
   472  	time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006")
   473  	if err != nil {
   474  		t.Fatal("error parsing date:", err)
   475  	}
   476  	expected := (1*60 + 23) * 60
   477  	_, offset := time.Zone()
   478  	if offset != expected {
   479  		t.Errorf("ZoneOffset = %d, want %d", offset, expected)
   480  	}
   481  }
   482  
   483  type SecondsTimeZoneOffsetTest struct {
   484  	format         string
   485  	value          string
   486  	expectedoffset int
   487  }
   488  
   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},
   496  }
   497  
   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)
   502  		if err != nil {
   503  			t.Fatal("error parsing date:", err)
   504  		}
   505  		_, offset := time.Zone()
   506  		if offset != test.expectedoffset {
   507  			t.Errorf("ZoneOffset = %d, want %d", offset, test.expectedoffset)
   508  		}
   509  	}
   510  }
   511  
   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)
   518  		}
   519  	}
   520  }