blob: d44347aed56a08a1f60212ed330eb9b747139a0c [file] [log] [blame]
Rob Pikef8cd2432014-01-16 14:30:01 -08001// 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
5package time_test
6
7import (
8 "fmt"
9 "strconv"
10 "strings"
11 "testing"
12 "testing/quick"
13 . "time"
14)
15
16type TimeFormatTest struct {
17 time Time
18 formattedValue string
19}
20
21var 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
27func 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
37type FormatTest struct {
38 name string
39 format string
40 result string
41}
42
43var 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
66func 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
Marvin Stengerc12e38a2015-09-03 00:39:53 +020077// issue 12440.
78func TestFormatSingleDigits(t *testing.T) {
79 time := Date(2001, 2, 3, 4, 5, 6, 700000000, UTC)
80 test := FormatTest{"single digit format", "3:4:5", "4:5:6"}
81 result := time.Format(test.format)
82 if result != test.result {
83 t.Errorf("%s expected %q got %q", test.name, test.result, result)
84 }
85}
86
Rob Pikef8cd2432014-01-16 14:30:01 -080087func TestFormatShortYear(t *testing.T) {
88 years := []int{
89 -100001, -100000, -99999,
90 -10001, -10000, -9999,
91 -1001, -1000, -999,
92 -101, -100, -99,
93 -11, -10, -9,
94 -1, 0, 1,
95 9, 10, 11,
96 99, 100, 101,
97 999, 1000, 1001,
98 9999, 10000, 10001,
99 99999, 100000, 100001,
100 }
101
102 for _, y := range years {
103 time := Date(y, January, 1, 0, 0, 0, 0, UTC)
104 result := time.Format("2006.01.02")
105 var want string
106 if y < 0 {
107 // The 4 in %04d counts the - sign, so print -y instead
108 // and introduce our own - sign.
109 want = fmt.Sprintf("-%04d.%02d.%02d", -y, 1, 1)
110 } else {
111 want = fmt.Sprintf("%04d.%02d.%02d", y, 1, 1)
112 }
113 if result != want {
114 t.Errorf("(jan 1 %d).Format(\"2006.01.02\") = %q, want %q", y, result, want)
115 }
116 }
117}
118
119type ParseTest struct {
120 name string
121 format string
122 value string
123 hasTZ bool // contains a time zone
124 hasWD bool // contains a weekday
125 yearSign int // sign of year, -1 indicates the year is not present in the format
126 fracDigits int // number of digits of fractional second
127}
128
129var parseTests = []ParseTest{
130 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
131 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0},
132 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
133 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0},
134 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0},
135 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 22:00:57 PDT", true, true, 1, 0},
136 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0},
137 {"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00", true, false, 1, 0},
138 {"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},
139 // Optional fractional seconds.
140 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57.0 2010", false, true, 1, 1},
141 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57.01 PST 2010", true, true, 1, 2},
142 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57.012 -0800 2010", true, true, 1, 3},
143 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57.0123 PST", true, true, 1, 4},
144 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57.01234 PST", true, true, 1, 5},
145 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57.01234 -0800", true, true, 1, 5},
146 {"RFC3339", RFC3339, "2010-02-04T21:00:57.012345678-08:00", true, false, 1, 9},
147 {"custom: \"2006-01-02 15:04:05\"", "2006-01-02 15:04:05", "2010-02-04 21:00:57.0", false, false, 1, 0},
148 // Amount of white space should not matter.
149 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
150 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
151 // Case should not matter
152 {"ANSIC", ANSIC, "THU FEB 4 21:00:57 2010", false, true, 1, 0},
153 {"ANSIC", ANSIC, "thu feb 4 21:00:57 2010", false, true, 1, 0},
154 // Fractional seconds.
155 {"millisecond", "Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 21:00:57.012 2010", false, true, 1, 3},
156 {"microsecond", "Mon Jan _2 15:04:05.000000 2006", "Thu Feb 4 21:00:57.012345 2010", false, true, 1, 6},
157 {"nanosecond", "Mon Jan _2 15:04:05.000000000 2006", "Thu Feb 4 21:00:57.012345678 2010", false, true, 1, 9},
158 // Leading zeros in other places should not be taken as fractional seconds.
159 {"zero1", "2006.01.02.15.04.05.0", "2010.02.04.21.00.57.0", false, false, 1, 1},
160 {"zero2", "2006.01.02.15.04.05.00", "2010.02.04.21.00.57.01", false, false, 1, 2},
161 // Month and day names only match when not followed by a lower-case letter.
162 {"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},
163
164 // GMT with offset.
165 {"GMT-8", UnixDate, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0},
166
167 // Accept any number of fractional second digits (including none) for .999...
168 // In Go 1, .999... was completely ignored in the format, meaning the first two
169 // cases would succeed, but the next four would not. Go 1.1 accepts all six.
170 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
171 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0},
172 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
173 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4},
174 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
175 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9},
176
177 // issue 4502.
178 {"", StampNano, "Feb 4 21:00:57.012345678", false, false, -1, 9},
179 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012300000", false, false, -1, 4},
180 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
181 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.0123", false, false, -1, 4},
182 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.012345678", false, false, -1, 9},
183}
184
185func TestParse(t *testing.T) {
186 for _, test := range parseTests {
187 time, err := Parse(test.format, test.value)
188 if err != nil {
189 t.Errorf("%s error: %v", test.name, err)
190 } else {
191 checkTime(time, &test, t)
192 }
193 }
194}
195
Alberto Donizetti5e701402014-08-21 10:35:43 -0700196func TestParseInLocation(t *testing.T) {
197 // Check that Parse (and ParseInLocation) understand that
198 // Feb 01 AST (Arabia Standard Time) and Feb 01 AST (Atlantic Standard Time)
199 // are in different time zones even though both are called AST
200
201 baghdad, err := LoadLocation("Asia/Baghdad")
Rob Pikef8cd2432014-01-16 14:30:01 -0800202 if err != nil {
203 t.Fatal(err)
204 }
205
Alberto Donizetti5e701402014-08-21 10:35:43 -0700206 t1, err := ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", baghdad)
Rob Pikef8cd2432014-01-16 14:30:01 -0800207 if err != nil {
208 t.Fatal(err)
209 }
Alberto Donizetti5e701402014-08-21 10:35:43 -0700210 t2 := Date(2013, February, 1, 00, 00, 00, 0, baghdad)
Rob Pikef8cd2432014-01-16 14:30:01 -0800211 if t1 != t2 {
Alberto Donizetti5e701402014-08-21 10:35:43 -0700212 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad) = %v, want %v", t1, t2)
Rob Pikef8cd2432014-01-16 14:30:01 -0800213 }
214 _, offset := t1.Zone()
Alberto Donizetti5e701402014-08-21 10:35:43 -0700215 if offset != 3*60*60 {
216 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Baghdad).Zone = _, %d, want _, %d", offset, 3*60*60)
Rob Pikef8cd2432014-01-16 14:30:01 -0800217 }
218
Alberto Donizetti5e701402014-08-21 10:35:43 -0700219 blancSablon, err := LoadLocation("America/Blanc-Sablon")
Rob Pikef8cd2432014-01-16 14:30:01 -0800220 if err != nil {
221 t.Fatal(err)
222 }
Alberto Donizetti5e701402014-08-21 10:35:43 -0700223
224 t1, err = ParseInLocation("Jan 02 2006 MST", "Feb 01 2013 AST", blancSablon)
225 if err != nil {
226 t.Fatal(err)
227 }
228 t2 = Date(2013, February, 1, 00, 00, 00, 0, blancSablon)
Rob Pikef8cd2432014-01-16 14:30:01 -0800229 if t1 != t2 {
Alberto Donizetti5e701402014-08-21 10:35:43 -0700230 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon) = %v, want %v", t1, t2)
Rob Pikef8cd2432014-01-16 14:30:01 -0800231 }
232 _, offset = t1.Zone()
Alberto Donizetti5e701402014-08-21 10:35:43 -0700233 if offset != -4*60*60 {
234 t.Fatalf("ParseInLocation(Feb 01 2013 AST, Blanc-Sablon).Zone = _, %d, want _, %d", offset, -4*60*60)
Rob Pikef8cd2432014-01-16 14:30:01 -0800235 }
236}
237
238func TestLoadLocationZipFile(t *testing.T) {
239 ForceZipFileForTesting(true)
240 defer ForceZipFileForTesting(false)
241
242 _, err := LoadLocation("Australia/Sydney")
243 if err != nil {
244 t.Fatal(err)
245 }
246}
247
248var rubyTests = []ParseTest{
249 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
250 // Ignore the time zone in the test. If it parses, it'll be OK.
251 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0000 2010", false, true, 1, 0},
252 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +0000 2010", false, true, 1, 0},
253 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +1130 2010", false, true, 1, 0},
254}
255
256// Problematic time zone format needs special tests.
257func TestRubyParse(t *testing.T) {
258 for _, test := range rubyTests {
259 time, err := Parse(test.format, test.value)
260 if err != nil {
261 t.Errorf("%s error: %v", test.name, err)
262 } else {
263 checkTime(time, &test, t)
264 }
265 }
266}
267
268func checkTime(time Time, test *ParseTest, t *testing.T) {
269 // The time should be Thu Feb 4 21:00:57 PST 2010
270 if test.yearSign >= 0 && test.yearSign*time.Year() != 2010 {
271 t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010)
272 }
273 if time.Month() != February {
274 t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), February)
275 }
276 if time.Day() != 4 {
277 t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4)
278 }
279 if time.Hour() != 21 {
280 t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21)
281 }
282 if time.Minute() != 0 {
283 t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute(), 0)
284 }
285 if time.Second() != 57 {
286 t.Errorf("%s: bad second: %d not %d", test.name, time.Second(), 57)
287 }
288 // Nanoseconds must be checked against the precision of the input.
289 nanosec, err := strconv.ParseUint("012345678"[:test.fracDigits]+"000000000"[:9-test.fracDigits], 10, 0)
290 if err != nil {
291 panic(err)
292 }
293 if time.Nanosecond() != int(nanosec) {
294 t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanosecond(), nanosec)
295 }
296 name, offset := time.Zone()
297 if test.hasTZ && offset != -28800 {
298 t.Errorf("%s: bad tz offset: %s %d not %d", test.name, name, offset, -28800)
299 }
300 if test.hasWD && time.Weekday() != Thursday {
301 t.Errorf("%s: bad weekday: %s not %s", test.name, time.Weekday(), Thursday)
302 }
303}
304
305func TestFormatAndParse(t *testing.T) {
306 const fmt = "Mon MST " + RFC3339 // all fields
307 f := func(sec int64) bool {
308 t1 := Unix(sec, 0)
309 if t1.Year() < 1000 || t1.Year() > 9999 {
310 // not required to work
311 return true
312 }
313 t2, err := Parse(fmt, t1.Format(fmt))
314 if err != nil {
315 t.Errorf("error: %s", err)
316 return false
317 }
318 if t1.Unix() != t2.Unix() || t1.Nanosecond() != t2.Nanosecond() {
319 t.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec, t1, t1.Unix(), t2, t2.Unix())
320 return false
321 }
322 return true
323 }
324 f32 := func(sec int32) bool { return f(int64(sec)) }
325 cfg := &quick.Config{MaxCount: 10000}
326
327 // Try a reasonable date first, then the huge ones.
328 if err := quick.Check(f32, cfg); err != nil {
329 t.Fatal(err)
330 }
331 if err := quick.Check(f, cfg); err != nil {
332 t.Fatal(err)
333 }
334}
335
336type ParseTimeZoneTest struct {
337 value string
338 length int
339 ok bool
340}
341
342var parseTimeZoneTests = []ParseTimeZoneTest{
343 {"gmt hi there", 0, false},
344 {"GMT hi there", 3, true},
345 {"GMT+12 hi there", 6, true},
346 {"GMT+00 hi there", 3, true}, // 0 or 00 is not a legal offset.
347 {"GMT-5 hi there", 5, true},
348 {"GMT-51 hi there", 3, true},
349 {"ChST hi there", 4, true},
Rui Ueyama70f6c432014-04-30 11:30:36 -0400350 {"MeST hi there", 4, true},
Rob Pikef8cd2432014-01-16 14:30:01 -0800351 {"MSDx", 3, true},
352 {"MSDY", 0, false}, // four letters must end in T.
353 {"ESAST hi", 5, true},
354 {"ESASTT hi", 0, false}, // run of upper-case letters too long.
355 {"ESATY hi", 0, false}, // five letters must end in T.
356}
357
358func TestParseTimeZone(t *testing.T) {
359 for _, test := range parseTimeZoneTests {
360 length, ok := ParseTimeZone(test.value)
361 if ok != test.ok {
362 t.Errorf("expected %t for %q got %t", test.ok, test.value, ok)
363 } else if length != test.length {
364 t.Errorf("expected %d for %q got %d", test.length, test.value, length)
365 }
366 }
367}
368
369type ParseErrorTest struct {
370 format string
371 value string
372 expect string // must appear within the error
373}
374
375var parseErrorTests = []ParseErrorTest{
376 {ANSIC, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon
377 {ANSIC, "Thu Feb 4 21:00:57 @2010", "cannot parse"},
378 {ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"},
379 {ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"},
380 {ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"},
381 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot parse"},
382 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot parse"},
383 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fractional second out of range"},
384 // issue 4502. StampNano requires exactly 9 digits of precision.
385 {StampNano, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`},
386 {StampNano, "Dec 7 11:22:01.0000000000", "extra text: 0"},
387 // issue 4493. Helpful errors.
388 {RFC3339, "2006-01-02T15:04:05Z07:00", `parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00`},
389 {RFC3339, "2006-01-02T15:04_abc", `parsing time "2006-01-02T15:04_abc" as "2006-01-02T15:04:05Z07:00": cannot parse "_abc" as ":"`},
390 {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"`},
391 {RFC3339, "2006-01-02T15:04:05Z_abc", `parsing time "2006-01-02T15:04:05Z_abc": extra text: _abc`},
392}
393
394func TestParseErrors(t *testing.T) {
395 for _, test := range parseErrorTests {
396 _, err := Parse(test.format, test.value)
397 if err == nil {
398 t.Errorf("expected error for %q %q", test.format, test.value)
399 } else if strings.Index(err.Error(), test.expect) < 0 {
400 t.Errorf("expected error with %q for %q %q; got %s", test.expect, test.format, test.value, err)
401 }
402 }
403}
404
405func TestNoonIs12PM(t *testing.T) {
406 noon := Date(0, January, 1, 12, 0, 0, 0, UTC)
407 const expect = "12:00PM"
408 got := noon.Format("3:04PM")
409 if got != expect {
410 t.Errorf("got %q; expect %q", got, expect)
411 }
412 got = noon.Format("03:04PM")
413 if got != expect {
414 t.Errorf("got %q; expect %q", got, expect)
415 }
416}
417
418func TestMidnightIs12AM(t *testing.T) {
419 midnight := Date(0, January, 1, 0, 0, 0, 0, UTC)
420 expect := "12:00AM"
421 got := midnight.Format("3:04PM")
422 if got != expect {
423 t.Errorf("got %q; expect %q", got, expect)
424 }
425 got = midnight.Format("03:04PM")
426 if got != expect {
427 t.Errorf("got %q; expect %q", got, expect)
428 }
429}
430
431func Test12PMIsNoon(t *testing.T) {
432 noon, err := Parse("3:04PM", "12:00PM")
433 if err != nil {
434 t.Fatal("error parsing date:", err)
435 }
436 if noon.Hour() != 12 {
437 t.Errorf("got %d; expect 12", noon.Hour())
438 }
439 noon, err = Parse("03:04PM", "12:00PM")
440 if err != nil {
441 t.Fatal("error parsing date:", err)
442 }
443 if noon.Hour() != 12 {
444 t.Errorf("got %d; expect 12", noon.Hour())
445 }
446}
447
448func Test12AMIsMidnight(t *testing.T) {
449 midnight, err := Parse("3:04PM", "12:00AM")
450 if err != nil {
451 t.Fatal("error parsing date:", err)
452 }
453 if midnight.Hour() != 0 {
454 t.Errorf("got %d; expect 0", midnight.Hour())
455 }
456 midnight, err = Parse("03:04PM", "12:00AM")
457 if err != nil {
458 t.Fatal("error parsing date:", err)
459 }
460 if midnight.Hour() != 0 {
461 t.Errorf("got %d; expect 0", midnight.Hour())
462 }
463}
464
465// Check that a time without a Zone still produces a (numeric) time zone
466// when formatted with MST as a requested zone.
467func TestMissingZone(t *testing.T) {
468 time, err := Parse(RubyDate, "Thu Feb 02 16:10:03 -0500 2006")
469 if err != nil {
470 t.Fatal("error parsing date:", err)
471 }
472 expect := "Thu Feb 2 16:10:03 -0500 2006" // -0500 not EST
473 str := time.Format(UnixDate) // uses MST as its time zone
474 if str != expect {
475 t.Errorf("got %s; expect %s", str, expect)
476 }
477}
478
479func TestMinutesInTimeZone(t *testing.T) {
480 time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006")
481 if err != nil {
482 t.Fatal("error parsing date:", err)
483 }
484 expected := (1*60 + 23) * 60
485 _, offset := time.Zone()
486 if offset != expected {
487 t.Errorf("ZoneOffset = %d, want %d", offset, expected)
488 }
489}
490
491type SecondsTimeZoneOffsetTest struct {
492 format string
493 value string
494 expectedoffset int
495}
496
497var secondsTimeZoneOffsetTests = []SecondsTimeZoneOffsetTest{
498 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
499 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02-00:34:08", -(34*60 + 8)},
500 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02+003408", 34*60 + 8},
501 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
502 {"2006-01-02T15:04:05Z070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)},
503 {"2006-01-02T15:04:05Z07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8},
504}
505
506func TestParseSecondsInTimeZone(t *testing.T) {
507 // should accept timezone offsets with seconds like: Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
508 for _, test := range secondsTimeZoneOffsetTests {
509 time, err := Parse(test.format, test.value)
510 if err != nil {
511 t.Fatal("error parsing date:", err)
512 }
513 _, offset := time.Zone()
514 if offset != test.expectedoffset {
515 t.Errorf("ZoneOffset = %d, want %d", offset, test.expectedoffset)
516 }
517 }
518}
519
520func TestFormatSecondsInTimeZone(t *testing.T) {
Joel Stemmer298c6232014-08-08 12:42:20 -0700521 for _, test := range secondsTimeZoneOffsetTests {
522 d := Date(1871, 1, 1, 5, 33, 2, 0, FixedZone("LMT", test.expectedoffset))
523 timestr := d.Format(test.format)
524 if timestr != test.value {
525 t.Errorf("Format = %s, want %s", timestr, test.value)
526 }
Rob Pikef8cd2432014-01-16 14:30:01 -0800527 }
528}