github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/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  	// Check that Parse (and ParseInLocation) understand that
   188  	// Feb 01 AST (Arabia Standard Time) and Feb 01 AST (Atlantic Standard Time)
   189  	// are in different time zones even though both are called AST
   190  
   191  	baghdad, err := LoadLocation("Asia/Baghdad")
   192  	if err != nil {
   193  		t.Fatal(err)
   194  	}
   195  
   196  	t1, err := ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", baghdad)
   197  	if err != nil {
   198  		t.Fatal(err)
   199  	}
   200  	t2 := Date(2013, February, 1, 00, 00, 00, 0, baghdad)
   201  	if t1 != t2 {
   202  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad) = %v, want %v", t1, t2)
   203  	}
   204  	_, offset := t1.Zone()
   205  	if offset != 3*60*60 {
   206  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad).Zone = _, %d, want _, %d", offset, 3*60*60)
   207  	}
   208  
   209  	blancSablon, err := LoadLocation("America/Blanc-Sablon")
   210  	if err != nil {
   211  		t.Fatal(err)
   212  	}
   213  
   214  	t1, err = ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", blancSablon)
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  	t2 = Date(2013, February, 1, 00, 00, 00, 0, blancSablon)
   219  	if t1 != t2 {
   220  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon) = %v, want %v", t1, t2)
   221  	}
   222  	_, offset = t1.Zone()
   223  	if offset != -4*60*60 {
   224  		t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon).Zone = _, %d, want _, %d", offset, -4*60*60)
   225  	}
   226  }
   227  
   228  func TestLoadLocationZipFile(t *testing.T) {
   229  	ForceZipFileForTesting(true)
   230  	defer ForceZipFileForTesting(false)
   231  
   232  	_, err := LoadLocation("Australia/Sydney")
   233  	if err != nil {
   234  		t.Fatal(err)
   235  	}
   236  }
   237  
   238  var rubyTests = []ParseTest{
   239  	{"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
   240  	// Ignore the time zone in the test. If it parses, it'll be OK.
   241  	{"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0000 2010", false, true, 1, 0},
   242  	{"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +0000 2010", false, true, 1, 0},
   243  	{"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +1130 2010", false, true, 1, 0},
   244  }
   245  
   246  // Problematic time zone format needs special tests.
   247  func TestRubyParse(t *testing.T) {
   248  	for _, test := range rubyTests {
   249  		time, err := Parse(test.format, test.value)
   250  		if err != nil {
   251  			t.Errorf("%s error: %v", test.name, err)
   252  		} else {
   253  			checkTime(time, &test, t)
   254  		}
   255  	}
   256  }
   257  
   258  func checkTime(time Time, test *ParseTest, t *testing.T) {
   259  	// The time should be Thu Feb  4 21:00:57 PST 2010
   260  	if test.yearSign >= 0 && test.yearSign*time.Year() != 2010 {
   261  		t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010)
   262  	}
   263  	if time.Month() != February {
   264  		t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), February)
   265  	}
   266  	if time.Day() != 4 {
   267  		t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4)
   268  	}
   269  	if time.Hour() != 21 {
   270  		t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21)
   271  	}
   272  	if time.Minute() != 0 {
   273  		t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute(), 0)
   274  	}
   275  	if time.Second() != 57 {
   276  		t.Errorf("%s: bad second: %d not %d", test.name, time.Second(), 57)
   277  	}
   278  	// Nanoseconds must be checked against the precision of the input.
   279  	nanosec, err := strconv.ParseUint("012345678"[:test.fracDigits]+"000000000"[:9-test.fracDigits], 10, 0)
   280  	if err != nil {
   281  		panic(err)
   282  	}
   283  	if time.Nanosecond() != int(nanosec) {
   284  		t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanosecond(), nanosec)
   285  	}
   286  	name, offset := time.Zone()
   287  	if test.hasTZ && offset != -28800 {
   288  		t.Errorf("%s: bad tz offset: %s %d not %d", test.name, name, offset, -28800)
   289  	}
   290  	if test.hasWD && time.Weekday() != Thursday {
   291  		t.Errorf("%s: bad weekday: %s not %s", test.name, time.Weekday(), Thursday)
   292  	}
   293  }
   294  
   295  func TestFormatAndParse(t *testing.T) {
   296  	const fmt = "Mon MST " + RFC3339 // all fields
   297  	f := func(sec int64) bool {
   298  		t1 := Unix(sec, 0)
   299  		if t1.Year() < 1000 || t1.Year() > 9999 {
   300  			// not required to work
   301  			return true
   302  		}
   303  		t2, err := Parse(fmt, t1.Format(fmt))
   304  		if err != nil {
   305  			t.Errorf("error: %s", err)
   306  			return false
   307  		}
   308  		if t1.Unix() != t2.Unix() || t1.Nanosecond() != t2.Nanosecond() {
   309  			t.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec, t1, t1.Unix(), t2, t2.Unix())
   310  			return false
   311  		}
   312  		return true
   313  	}
   314  	f32 := func(sec int32) bool { return f(int64(sec)) }
   315  	cfg := &quick.Config{MaxCount: 10000}
   316  
   317  	// Try a reasonable date first, then the huge ones.
   318  	if err := quick.Check(f32, cfg); err != nil {
   319  		t.Fatal(err)
   320  	}
   321  	if err := quick.Check(f, cfg); err != nil {
   322  		t.Fatal(err)
   323  	}
   324  }
   325  
   326  type ParseTimeZoneTest struct {
   327  	value  string
   328  	length int
   329  	ok     bool
   330  }
   331  
   332  var parseTimeZoneTests = []ParseTimeZoneTest{
   333  	{"gmt hi there", 0, false},
   334  	{"GMT hi there", 3, true},
   335  	{"GMT+12 hi there", 6, true},
   336  	{"GMT+00 hi there", 3, true}, // 0 or 00 is not a legal offset.
   337  	{"GMT-5 hi there", 5, true},
   338  	{"GMT-51 hi there", 3, true},
   339  	{"ChST hi there", 4, true},
   340  	{"MeST hi there", 4, true},
   341  	{"MSDx", 3, true},
   342  	{"MSDY", 0, false}, // four letters must end in T.
   343  	{"ESAST hi", 5, true},
   344  	{"ESASTT hi", 0, false}, // run of upper-case letters too long.
   345  	{"ESATY hi", 0, false},  // five letters must end in T.
   346  }
   347  
   348  func TestParseTimeZone(t *testing.T) {
   349  	for _, test := range parseTimeZoneTests {
   350  		length, ok := ParseTimeZone(test.value)
   351  		if ok != test.ok {
   352  			t.Errorf("expected %t for %q got %t", test.ok, test.value, ok)
   353  		} else if length != test.length {
   354  			t.Errorf("expected %d for %q got %d", test.length, test.value, length)
   355  		}
   356  	}
   357  }
   358  
   359  type ParseErrorTest struct {
   360  	format string
   361  	value  string
   362  	expect string // must appear within the error
   363  }
   364  
   365  var parseErrorTests = []ParseErrorTest{
   366  	{ANSIC, "Feb  4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon
   367  	{ANSIC, "Thu Feb  4 21:00:57 @2010", "cannot parse"},
   368  	{ANSIC, "Thu Feb  4 21:00:60 2010", "second out of range"},
   369  	{ANSIC, "Thu Feb  4 21:61:57 2010", "minute out of range"},
   370  	{ANSIC, "Thu Feb  4 24:00:60 2010", "hour out of range"},
   371  	{"Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 23:00:59x01 2010", "cannot parse"},
   372  	{"Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 23:00:59.xxx 2010", "cannot parse"},
   373  	{"Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 23:00:59.-123 2010", "fractional second out of range"},
   374  	// issue 4502. StampNano requires exactly 9 digits of precision.
   375  	{StampNano, "Dec  7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`},
   376  	{StampNano, "Dec  7 11:22:01.0000000000", "extra text: 0"},
   377  	// issue 4493. Helpful errors.
   378  	{RFC3339, "2006-01-02T15:04:05Z07:00", `parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00`},
   379  	{RFC3339, "2006-01-02T15:04_abc", `parsing time "2006-01-02T15:04_abc" as "2006-01-02T15:04:05Z07:00": cannot parse "_abc" as ":"`},
   380  	{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"`},
   381  	{RFC3339, "2006-01-02T15:04:05Z_abc", `parsing time "2006-01-02T15:04:05Z_abc": extra text: _abc`},
   382  }
   383  
   384  func TestParseErrors(t *testing.T) {
   385  	for _, test := range parseErrorTests {
   386  		_, err := Parse(test.format, test.value)
   387  		if err == nil {
   388  			t.Errorf("expected error for %q %q", test.format, test.value)
   389  		} else if strings.Index(err.Error(), test.expect) < 0 {
   390  			t.Errorf("expected error with %q for %q %q; got %s", test.expect, test.format, test.value, err)
   391  		}
   392  	}
   393  }
   394  
   395  func TestNoonIs12PM(t *testing.T) {
   396  	noon := Date(0, January, 1, 12, 0, 0, 0, UTC)
   397  	const expect = "12:00PM"
   398  	got := noon.Format("3:04PM")
   399  	if got != expect {
   400  		t.Errorf("got %q; expect %q", got, expect)
   401  	}
   402  	got = noon.Format("03:04PM")
   403  	if got != expect {
   404  		t.Errorf("got %q; expect %q", got, expect)
   405  	}
   406  }
   407  
   408  func TestMidnightIs12AM(t *testing.T) {
   409  	midnight := Date(0, January, 1, 0, 0, 0, 0, UTC)
   410  	expect := "12:00AM"
   411  	got := midnight.Format("3:04PM")
   412  	if got != expect {
   413  		t.Errorf("got %q; expect %q", got, expect)
   414  	}
   415  	got = midnight.Format("03:04PM")
   416  	if got != expect {
   417  		t.Errorf("got %q; expect %q", got, expect)
   418  	}
   419  }
   420  
   421  func Test12PMIsNoon(t *testing.T) {
   422  	noon, err := Parse("3:04PM", "12:00PM")
   423  	if err != nil {
   424  		t.Fatal("error parsing date:", err)
   425  	}
   426  	if noon.Hour() != 12 {
   427  		t.Errorf("got %d; expect 12", noon.Hour())
   428  	}
   429  	noon, err = Parse("03:04PM", "12:00PM")
   430  	if err != nil {
   431  		t.Fatal("error parsing date:", err)
   432  	}
   433  	if noon.Hour() != 12 {
   434  		t.Errorf("got %d; expect 12", noon.Hour())
   435  	}
   436  }
   437  
   438  func Test12AMIsMidnight(t *testing.T) {
   439  	midnight, err := Parse("3:04PM", "12:00AM")
   440  	if err != nil {
   441  		t.Fatal("error parsing date:", err)
   442  	}
   443  	if midnight.Hour() != 0 {
   444  		t.Errorf("got %d; expect 0", midnight.Hour())
   445  	}
   446  	midnight, err = Parse("03:04PM", "12:00AM")
   447  	if err != nil {
   448  		t.Fatal("error parsing date:", err)
   449  	}
   450  	if midnight.Hour() != 0 {
   451  		t.Errorf("got %d; expect 0", midnight.Hour())
   452  	}
   453  }
   454  
   455  // Check that a time without a Zone still produces a (numeric) time zone
   456  // when formatted with MST as a requested zone.
   457  func TestMissingZone(t *testing.T) {
   458  	time, err := Parse(RubyDate, "Thu Feb 02 16:10:03 -0500 2006")
   459  	if err != nil {
   460  		t.Fatal("error parsing date:", err)
   461  	}
   462  	expect := "Thu Feb  2 16:10:03 -0500 2006" // -0500 not EST
   463  	str := time.Format(UnixDate)               // uses MST as its time zone
   464  	if str != expect {
   465  		t.Errorf("got %s; expect %s", str, expect)
   466  	}
   467  }
   468  
   469  func TestMinutesInTimeZone(t *testing.T) {
   470  	time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006")
   471  	if err != nil {
   472  		t.Fatal("error parsing date:", err)
   473  	}
   474  	expected := (1*60 + 23) * 60
   475  	_, offset := time.Zone()
   476  	if offset != expected {
   477  		t.Errorf("ZoneOffset = %d, want %d", offset, expected)
   478  	}
   479  }
   480  
   481  type SecondsTimeZoneOffsetTest struct {
   482  	format         string
   483  	value          string
   484  	expectedoffset int
   485  }
   486  
   487  var secondsTimeZoneOffsetTests = []SecondsTimeZoneOffsetTest{
   488  	{"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
   489  	{"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02-00:34:08", -(34*60 + 8)},
   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:05Z070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
   493  	{"2006-01-02T15:04:05Z07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
   494  }
   495  
   496  func TestParseSecondsInTimeZone(t *testing.T) {
   497  	// should accept timezone offsets with seconds like: Zone America/New_York   -4:56:02 -      LMT     1883 Nov 18 12:03:58
   498  	for _, test := range secondsTimeZoneOffsetTests {
   499  		time, err := Parse(test.format, test.value)
   500  		if err != nil {
   501  			t.Fatal("error parsing date:", err)
   502  		}
   503  		_, offset := time.Zone()
   504  		if offset != test.expectedoffset {
   505  			t.Errorf("ZoneOffset = %d, want %d", offset, test.expectedoffset)
   506  		}
   507  	}
   508  }
   509  
   510  func TestFormatSecondsInTimeZone(t *testing.T) {
   511  	for _, test := range secondsTimeZoneOffsetTests {
   512  		d := Date(1871, 1, 1, 5, 33, 2, 0, FixedZone("LMT", test.expectedoffset))
   513  		timestr := d.Format(test.format)
   514  		if timestr != test.value {
   515  			t.Errorf("Format = %s, want %s", timestr, test.value)
   516  		}
   517  	}
   518  }