github.com/embeddedgo/x@v0.0.6-0.20191217015414-d79a36f562e7/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 14 . "github.com/embeddedgo/x/time" 15 ) 16 17 var nextStdChunkTests = []string{ 18 "(2006)-(01)-(02)T(15):(04):(05)(Z07:00)", 19 "(2006)-(01)-(02) (002) (15):(04):(05)", 20 "(2006)-(01) (002) (15):(04):(05)", 21 "(2006)-(002) (15):(04):(05)", 22 "(2006)(002)(01) (15):(04):(05)", 23 "(2006)(002)(04) (15):(04):(05)", 24 } 25 26 func TestNextStdChunk(t *testing.T) { 27 // Most bugs in Parse or Format boil down to problems with 28 // the exact detection of format chunk boundaries in the 29 // helper function nextStdChunk (here called as NextStdChunk). 30 // This test checks nextStdChunk's behavior directly, 31 // instead of needing to test it only indirectly through Parse/Format. 32 33 // markChunks returns format with each detected 34 // 'format chunk' parenthesized. 35 // For example showChunks("2006-01-02") == "(2006)-(01)-(02)". 36 markChunks := func(format string) string { 37 // Note that NextStdChunk and StdChunkNames 38 // are not part of time's public API. 39 // They are exported in export_test for this test. 40 out := "" 41 for s := format; s != ""; { 42 prefix, std, suffix := NextStdChunk(s) 43 out += prefix 44 if std > 0 { 45 out += "(" + StdChunkNames[std] + ")" 46 } 47 s = suffix 48 } 49 return out 50 } 51 52 noParens := func(r rune) rune { 53 if r == '(' || r == ')' { 54 return -1 55 } 56 return r 57 } 58 59 for _, marked := range nextStdChunkTests { 60 // marked is an expected output from markChunks. 61 // If we delete the parens and pass it through markChunks, 62 // we should get the original back. 63 format := strings.Map(noParens, marked) 64 out := markChunks(format) 65 if out != marked { 66 t.Errorf("nextStdChunk parses %q as %q, want %q", format, out, marked) 67 } 68 } 69 } 70 71 type TimeFormatTest struct { 72 time Time 73 formattedValue string 74 } 75 76 var rfc3339Formats = []TimeFormatTest{ 77 {Date(2008, 9, 17, 20, 4, 26, 0, UTC), "2008-09-17T20:04:26Z"}, 78 {Date(1994, 9, 17, 20, 4, 26, 0, FixedZone("EST", -18000)), "1994-09-17T20:04:26-05:00"}, 79 {Date(2000, 12, 26, 1, 15, 6, 0, FixedZone("OTO", 15600)), "2000-12-26T01:15:06+04:20"}, 80 } 81 82 func TestRFC3339Conversion(t *testing.T) { 83 for _, f := range rfc3339Formats { 84 if f.time.Format(RFC3339) != f.formattedValue { 85 t.Error("RFC3339:") 86 t.Errorf(" want=%+v", f.formattedValue) 87 t.Errorf(" have=%+v", f.time.Format(RFC3339)) 88 } 89 } 90 } 91 92 type FormatTest struct { 93 name string 94 format string 95 result string 96 } 97 98 var formatTests = []FormatTest{ 99 {"ANSIC", ANSIC, "Wed Feb 4 21:00:57 2009"}, 100 {"UnixDate", UnixDate, "Wed Feb 4 21:00:57 PST 2009"}, 101 {"RubyDate", RubyDate, "Wed Feb 04 21:00:57 -0800 2009"}, 102 {"RFC822", RFC822, "04 Feb 09 21:00 PST"}, 103 {"RFC850", RFC850, "Wednesday, 04-Feb-09 21:00:57 PST"}, 104 {"RFC1123", RFC1123, "Wed, 04 Feb 2009 21:00:57 PST"}, 105 {"RFC1123Z", RFC1123Z, "Wed, 04 Feb 2009 21:00:57 -0800"}, 106 {"RFC3339", RFC3339, "2009-02-04T21:00:57-08:00"}, 107 {"RFC3339Nano", RFC3339Nano, "2009-02-04T21:00:57.0123456-08:00"}, 108 {"Kitchen", Kitchen, "9:00PM"}, 109 {"am/pm", "3pm", "9pm"}, 110 {"AM/PM", "3PM", "9PM"}, 111 {"two-digit year", "06 01 02", "09 02 04"}, 112 // Three-letter months and days must not be followed by lower-case letter. 113 {"Janet", "Hi Janet, the Month is January", "Hi Janet, the Month is February"}, 114 // Time stamps, Fractional seconds. 115 {"Stamp", Stamp, "Feb 4 21:00:57"}, 116 {"StampMilli", StampMilli, "Feb 4 21:00:57.012"}, 117 {"StampMicro", StampMicro, "Feb 4 21:00:57.012345"}, 118 {"StampNano", StampNano, "Feb 4 21:00:57.012345600"}, 119 {"YearDay", "Jan 2 002 __2 2", "Feb 4 035 35 4"}, 120 } 121 122 func TestFormat(t *testing.T) { 123 // The numeric time represents Thu Feb 4 21:00:57.012345600 PST 2010 124 time := Unix(0, 1233810057012345600) 125 for _, test := range formatTests { 126 result := time.Format(test.format) 127 if result != test.result { 128 t.Errorf("%s expected %q got %q", test.name, test.result, result) 129 } 130 } 131 } 132 133 // issue 12440. 134 func TestFormatSingleDigits(t *testing.T) { 135 time := Date(2001, 2, 3, 4, 5, 6, 700000000, UTC) 136 test := FormatTest{"single digit format", "3:4:5", "4:5:6"} 137 result := time.Format(test.format) 138 if result != test.result { 139 t.Errorf("%s expected %q got %q", test.name, test.result, result) 140 } 141 } 142 143 func TestFormatShortYear(t *testing.T) { 144 years := []int{ 145 -100001, -100000, -99999, 146 -10001, -10000, -9999, 147 -1001, -1000, -999, 148 -101, -100, -99, 149 -11, -10, -9, 150 -1, 0, 1, 151 9, 10, 11, 152 99, 100, 101, 153 999, 1000, 1001, 154 9999, 10000, 10001, 155 99999, 100000, 100001, 156 } 157 158 for _, y := range years { 159 time := Date(y, January, 1, 0, 0, 0, 0, UTC) 160 result := time.Format("2006.01.02") 161 var want string 162 if y < 0 { 163 // The 4 in %04d counts the - sign, so print -y instead 164 // and introduce our own - sign. 165 want = fmt.Sprintf("-%04d.%02d.%02d", -y, 1, 1) 166 } else { 167 want = fmt.Sprintf("%04d.%02d.%02d", y, 1, 1) 168 } 169 if result != want { 170 t.Errorf("(jan 1 %d).Format(\"2006.01.02\") = %q, want %q", y, result, want) 171 } 172 } 173 } 174 175 type ParseTest struct { 176 name string 177 format string 178 value string 179 hasTZ bool // contains a time zone 180 hasWD bool // contains a weekday 181 yearSign int // sign of year, -1 indicates the year is not present in the format 182 fracDigits int // number of digits of fractional second 183 } 184 185 var parseTests = []ParseTest{ 186 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0}, 187 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0}, 188 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0}, 189 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0}, 190 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0}, 191 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 22:00:57 PDT", true, true, 1, 0}, 192 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0}, 193 {"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00", true, false, 1, 0}, 194 {"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}, 195 // Optional fractional seconds. 196 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57.0 2010", false, true, 1, 1}, 197 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57.01 PST 2010", true, true, 1, 2}, 198 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57.012 -0800 2010", true, true, 1, 3}, 199 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57.0123 PST", true, true, 1, 4}, 200 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57.01234 PST", true, true, 1, 5}, 201 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57.01234 -0800", true, true, 1, 5}, 202 {"RFC3339", RFC3339, "2010-02-04T21:00:57.012345678-08:00", true, false, 1, 9}, 203 {"custom: \"2006-01-02 15:04:05\"", "2006-01-02 15:04:05", "2010-02-04 21:00:57.0", false, false, 1, 0}, 204 // Amount of white space should not matter. 205 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0}, 206 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0}, 207 // Case should not matter 208 {"ANSIC", ANSIC, "THU FEB 4 21:00:57 2010", false, true, 1, 0}, 209 {"ANSIC", ANSIC, "thu feb 4 21:00:57 2010", false, true, 1, 0}, 210 // Fractional seconds. 211 {"millisecond", "Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 21:00:57.012 2010", false, true, 1, 3}, 212 {"microsecond", "Mon Jan _2 15:04:05.000000 2006", "Thu Feb 4 21:00:57.012345 2010", false, true, 1, 6}, 213 {"nanosecond", "Mon Jan _2 15:04:05.000000000 2006", "Thu Feb 4 21:00:57.012345678 2010", false, true, 1, 9}, 214 // Leading zeros in other places should not be taken as fractional seconds. 215 {"zero1", "2006.01.02.15.04.05.0", "2010.02.04.21.00.57.0", false, false, 1, 1}, 216 {"zero2", "2006.01.02.15.04.05.00", "2010.02.04.21.00.57.01", false, false, 1, 2}, 217 // Month and day names only match when not followed by a lower-case letter. 218 {"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}, 219 220 // GMT with offset. 221 {"GMT-8", UnixDate, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0}, 222 223 // Accept any number of fractional second digits (including none) for .999... 224 // In Go 1, .999... was completely ignored in the format, meaning the first two 225 // cases would succeed, but the next four would not. Go 1.1 accepts all six. 226 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0}, 227 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57 -0800 PST", true, false, 1, 0}, 228 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4}, 229 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.0123 -0800 PST", true, false, 1, 4}, 230 {"", "2006-01-02 15:04:05.9999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9}, 231 {"", "2006-01-02 15:04:05.999999999 -0700 MST", "2010-02-04 21:00:57.012345678 -0800 PST", true, false, 1, 9}, 232 233 // issue 4502. 234 {"", StampNano, "Feb 4 21:00:57.012345678", false, false, -1, 9}, 235 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012300000", false, false, -1, 4}, 236 {"", "Jan _2 15:04:05.999", "Feb 4 21:00:57.012345678", false, false, -1, 9}, 237 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.0123", false, false, -1, 4}, 238 {"", "Jan _2 15:04:05.999999999", "Feb 4 21:00:57.012345678", false, false, -1, 9}, 239 240 // Day of year. 241 {"", "2006-01-02 002 15:04:05", "2010-02-04 035 21:00:57", false, false, 1, 0}, 242 {"", "2006-01 002 15:04:05", "2010-02 035 21:00:57", false, false, 1, 0}, 243 {"", "2006-002 15:04:05", "2010-035 21:00:57", false, false, 1, 0}, 244 {"", "200600201 15:04:05", "201003502 21:00:57", false, false, 1, 0}, 245 {"", "200600204 15:04:05", "201003504 21:00:57", false, false, 1, 0}, 246 } 247 248 func TestParse(t *testing.T) { 249 for _, test := range parseTests { 250 time, err := Parse(test.format, test.value) 251 if err != nil { 252 t.Errorf("%s error: %v", test.name, err) 253 } else { 254 checkTime(time, &test, t) 255 } 256 } 257 } 258 259 // All parsed with ANSIC. 260 var dayOutOfRangeTests = []struct { 261 date string 262 ok bool 263 }{ 264 {"Thu Jan 99 21:00:57 2010", false}, 265 {"Thu Jan 31 21:00:57 2010", true}, 266 {"Thu Jan 32 21:00:57 2010", false}, 267 {"Thu Feb 28 21:00:57 2012", true}, 268 {"Thu Feb 29 21:00:57 2012", true}, 269 {"Thu Feb 29 21:00:57 2010", false}, 270 {"Thu Mar 31 21:00:57 2010", true}, 271 {"Thu Mar 32 21:00:57 2010", false}, 272 {"Thu Apr 30 21:00:57 2010", true}, 273 {"Thu Apr 31 21:00:57 2010", false}, 274 {"Thu May 31 21:00:57 2010", true}, 275 {"Thu May 32 21:00:57 2010", false}, 276 {"Thu Jun 30 21:00:57 2010", true}, 277 {"Thu Jun 31 21:00:57 2010", false}, 278 {"Thu Jul 31 21:00:57 2010", true}, 279 {"Thu Jul 32 21:00:57 2010", false}, 280 {"Thu Aug 31 21:00:57 2010", true}, 281 {"Thu Aug 32 21:00:57 2010", false}, 282 {"Thu Sep 30 21:00:57 2010", true}, 283 {"Thu Sep 31 21:00:57 2010", false}, 284 {"Thu Oct 31 21:00:57 2010", true}, 285 {"Thu Oct 32 21:00:57 2010", false}, 286 {"Thu Nov 30 21:00:57 2010", true}, 287 {"Thu Nov 31 21:00:57 2010", false}, 288 {"Thu Dec 31 21:00:57 2010", true}, 289 {"Thu Dec 32 21:00:57 2010", false}, 290 {"Thu Dec 00 21:00:57 2010", false}, 291 } 292 293 func TestParseDayOutOfRange(t *testing.T) { 294 for _, test := range dayOutOfRangeTests { 295 _, err := Parse(ANSIC, test.date) 296 switch { 297 case test.ok && err == nil: 298 // OK 299 case !test.ok && err != nil: 300 if !strings.Contains(err.Error(), "day out of range") { 301 t.Errorf("%q: expected 'day' error, got %v", test.date, err) 302 } 303 case test.ok && err != nil: 304 t.Errorf("%q: unexpected error: %v", test.date, err) 305 case !test.ok && err == nil: 306 t.Errorf("%q: expected 'day' error, got none", test.date) 307 } 308 } 309 } 310 311 var rubyTests = []ParseTest{ 312 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0}, 313 // Ignore the time zone in the test. If it parses, it'll be OK. 314 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0000 2010", false, true, 1, 0}, 315 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +0000 2010", false, true, 1, 0}, 316 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 +1130 2010", false, true, 1, 0}, 317 } 318 319 // Problematic time zone format needs special tests. 320 func TestRubyParse(t *testing.T) { 321 for _, test := range rubyTests { 322 time, err := Parse(test.format, test.value) 323 if err != nil { 324 t.Errorf("%s error: %v", test.name, err) 325 } else { 326 checkTime(time, &test, t) 327 } 328 } 329 } 330 331 func checkTime(time Time, test *ParseTest, t *testing.T) { 332 // The time should be Thu Feb 4 21:00:57 PST 2010 333 if test.yearSign >= 0 && test.yearSign*time.Year() != 2010 { 334 t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010) 335 } 336 if time.Month() != February { 337 t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), February) 338 } 339 if time.Day() != 4 { 340 t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4) 341 } 342 if time.Hour() != 21 { 343 t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21) 344 } 345 if time.Minute() != 0 { 346 t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute(), 0) 347 } 348 if time.Second() != 57 { 349 t.Errorf("%s: bad second: %d not %d", test.name, time.Second(), 57) 350 } 351 // Nanoseconds must be checked against the precision of the input. 352 nanosec, err := strconv.ParseUint("012345678"[:test.fracDigits]+"000000000"[:9-test.fracDigits], 10, 0) 353 if err != nil { 354 panic(err) 355 } 356 if time.Nanosecond() != int(nanosec) { 357 t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanosecond(), nanosec) 358 } 359 name, offset := time.Zone() 360 if test.hasTZ && offset != -28800 { 361 t.Errorf("%s: bad tz offset: %s %d not %d", test.name, name, offset, -28800) 362 } 363 if test.hasWD && time.Weekday() != Thursday { 364 t.Errorf("%s: bad weekday: %s not %s", test.name, time.Weekday(), Thursday) 365 } 366 } 367 368 func TestFormatAndParse(t *testing.T) { 369 const fmt = "Mon MST " + RFC3339 // all fields 370 f := func(sec int64) bool { 371 t1 := Unix(sec/2, 0) 372 if t1.Year() < 1000 || t1.Year() > 9999 || t1.Unix() != sec { 373 // not required to work 374 return true 375 } 376 t2, err := Parse(fmt, t1.Format(fmt)) 377 if err != nil { 378 t.Errorf("error: %s", err) 379 return false 380 } 381 if t1.Unix() != t2.Unix() || t1.Nanosecond() != t2.Nanosecond() { 382 t.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec, t1, t1.Unix(), t2, t2.Unix()) 383 return false 384 } 385 return true 386 } 387 f32 := func(sec int32) bool { return f(int64(sec)) } 388 cfg := &quick.Config{MaxCount: 10000} 389 390 // Try a reasonable date first, then the huge ones. 391 if err := quick.Check(f32, cfg); err != nil { 392 t.Fatal(err) 393 } 394 if err := quick.Check(f, cfg); err != nil { 395 t.Fatal(err) 396 } 397 } 398 399 type ParseTimeZoneTest struct { 400 value string 401 length int 402 ok bool 403 } 404 405 var parseTimeZoneTests = []ParseTimeZoneTest{ 406 {"gmt hi there", 0, false}, 407 {"GMT hi there", 3, true}, 408 {"GMT+12 hi there", 6, true}, 409 {"GMT+00 hi there", 6, true}, 410 {"GMT+", 3, true}, 411 {"GMT+3", 5, true}, 412 {"GMT+a", 3, true}, 413 {"GMT+3a", 5, true}, 414 {"GMT-5 hi there", 5, true}, 415 {"GMT-51 hi there", 3, true}, 416 {"ChST hi there", 4, true}, 417 {"MeST hi there", 4, true}, 418 {"MSDx", 3, true}, 419 {"MSDY", 0, false}, // four letters must end in T. 420 {"ESAST hi", 5, true}, 421 {"ESASTT hi", 0, false}, // run of upper-case letters too long. 422 {"ESATY hi", 0, false}, // five letters must end in T. 423 {"WITA hi", 4, true}, // Issue #18251 424 // Issue #24071 425 {"+03 hi", 3, true}, 426 {"-04 hi", 3, true}, 427 // Issue #26032 428 {"+00", 3, true}, 429 {"-11", 3, true}, 430 {"-12", 3, true}, 431 {"-23", 3, true}, 432 {"-24", 0, false}, 433 {"+13", 3, true}, 434 {"+14", 3, true}, 435 {"+23", 3, true}, 436 {"+24", 0, false}, 437 } 438 439 func TestParseTimeZone(t *testing.T) { 440 for _, test := range parseTimeZoneTests { 441 length, ok := ParseTimeZone(test.value) 442 if ok != test.ok { 443 t.Errorf("expected %t for %q got %t", test.ok, test.value, ok) 444 } else if length != test.length { 445 t.Errorf("expected %d for %q got %d", test.length, test.value, length) 446 } 447 } 448 } 449 450 type ParseErrorTest struct { 451 format string 452 value string 453 expect string // must appear within the error 454 } 455 456 var parseErrorTests = []ParseErrorTest{ 457 {ANSIC, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon 458 {ANSIC, "Thu Feb 4 21:00:57 @2010", "cannot parse"}, 459 {ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"}, 460 {ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"}, 461 {ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"}, 462 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot parse"}, 463 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot parse"}, 464 {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fractional second out of range"}, 465 // issue 4502. StampNano requires exactly 9 digits of precision. 466 {StampNano, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`}, 467 {StampNano, "Dec 7 11:22:01.0000000000", "extra text: 0"}, 468 // issue 4493. Helpful errors. 469 {RFC3339, "2006-01-02T15:04:05Z07:00", `parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00`}, 470 {RFC3339, "2006-01-02T15:04_abc", `parsing time "2006-01-02T15:04_abc" as "2006-01-02T15:04:05Z07:00": cannot parse "_abc" as ":"`}, 471 {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"`}, 472 {RFC3339, "2006-01-02T15:04:05Z_abc", `parsing time "2006-01-02T15:04:05Z_abc": extra text: _abc`}, 473 // invalid second followed by optional fractional seconds 474 {RFC3339, "2010-02-04T21:00:67.012345678-08:00", "second out of range"}, 475 // issue 21113 476 {"_2 Jan 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"}, 477 {"_2 January 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"}, 478 479 // invalid or mismatched day-of-year 480 {"Jan _2 002 2006", "Feb 4 034 2006", "day-of-year does not match day"}, 481 {"Jan _2 002 2006", "Feb 4 004 2006", "day-of-year does not match month"}, 482 } 483 484 func TestParseErrors(t *testing.T) { 485 for _, test := range parseErrorTests { 486 _, err := Parse(test.format, test.value) 487 if err == nil { 488 t.Errorf("expected error for %q %q", test.format, test.value) 489 } else if !strings.Contains(err.Error(), test.expect) { 490 t.Errorf("expected error with %q for %q %q; got %s", test.expect, test.format, test.value, err) 491 } 492 } 493 } 494 495 func TestNoonIs12PM(t *testing.T) { 496 noon := Date(0, January, 1, 12, 0, 0, 0, UTC) 497 const expect = "12:00PM" 498 got := noon.Format("3:04PM") 499 if got != expect { 500 t.Errorf("got %q; expect %q", got, expect) 501 } 502 got = noon.Format("03:04PM") 503 if got != expect { 504 t.Errorf("got %q; expect %q", got, expect) 505 } 506 } 507 508 func TestMidnightIs12AM(t *testing.T) { 509 midnight := Date(0, January, 1, 0, 0, 0, 0, UTC) 510 expect := "12:00AM" 511 got := midnight.Format("3:04PM") 512 if got != expect { 513 t.Errorf("got %q; expect %q", got, expect) 514 } 515 got = midnight.Format("03:04PM") 516 if got != expect { 517 t.Errorf("got %q; expect %q", got, expect) 518 } 519 } 520 521 func Test12PMIsNoon(t *testing.T) { 522 noon, err := Parse("3:04PM", "12:00PM") 523 if err != nil { 524 t.Fatal("error parsing date:", err) 525 } 526 if noon.Hour() != 12 { 527 t.Errorf("got %d; expect 12", noon.Hour()) 528 } 529 noon, err = Parse("03:04PM", "12:00PM") 530 if err != nil { 531 t.Fatal("error parsing date:", err) 532 } 533 if noon.Hour() != 12 { 534 t.Errorf("got %d; expect 12", noon.Hour()) 535 } 536 } 537 538 func Test12AMIsMidnight(t *testing.T) { 539 midnight, err := Parse("3:04PM", "12:00AM") 540 if err != nil { 541 t.Fatal("error parsing date:", err) 542 } 543 if midnight.Hour() != 0 { 544 t.Errorf("got %d; expect 0", midnight.Hour()) 545 } 546 midnight, err = Parse("03:04PM", "12:00AM") 547 if err != nil { 548 t.Fatal("error parsing date:", err) 549 } 550 if midnight.Hour() != 0 { 551 t.Errorf("got %d; expect 0", midnight.Hour()) 552 } 553 } 554 555 // Check that a time without a Zone still produces a (numeric) time zone 556 // when formatted with MST as a requested zone. 557 func TestMissingZone(t *testing.T) { 558 time, err := Parse(RubyDate, "Thu Feb 02 16:10:03 -0500 2006") 559 if err != nil { 560 t.Fatal("error parsing date:", err) 561 } 562 expect := "Thu Feb 2 16:10:03 -0500 2006" // -0500 not EST 563 str := time.Format(UnixDate) // uses MST as its time zone 564 if str != expect { 565 t.Errorf("got %s; expect %s", str, expect) 566 } 567 } 568 569 func TestMinutesInTimeZone(t *testing.T) { 570 time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006") 571 if err != nil { 572 t.Fatal("error parsing date:", err) 573 } 574 expected := (1*60 + 23) * 60 575 _, offset := time.Zone() 576 if offset != expected { 577 t.Errorf("ZoneOffset = %d, want %d", offset, expected) 578 } 579 } 580 581 type SecondsTimeZoneOffsetTest struct { 582 format string 583 value string 584 expectedoffset int 585 } 586 587 var secondsTimeZoneOffsetTests = []SecondsTimeZoneOffsetTest{ 588 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)}, 589 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02-00:34:08", -(34*60 + 8)}, 590 {"2006-01-02T15:04:05-070000", "1871-01-01T05:33:02+003408", 34*60 + 8}, 591 {"2006-01-02T15:04:05-07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8}, 592 {"2006-01-02T15:04:05Z070000", "1871-01-01T05:33:02-003408", -(34*60 + 8)}, 593 {"2006-01-02T15:04:05Z07:00:00", "1871-01-01T05:33:02+00:34:08", 34*60 + 8}, 594 {"2006-01-02T15:04:05-07", "1871-01-01T05:33:02+01", 1 * 60 * 60}, 595 {"2006-01-02T15:04:05-07", "1871-01-01T05:33:02-02", -2 * 60 * 60}, 596 {"2006-01-02T15:04:05Z07", "1871-01-01T05:33:02-02", -2 * 60 * 60}, 597 } 598 599 func TestParseSecondsInTimeZone(t *testing.T) { 600 // should accept timezone offsets with seconds like: Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58 601 for _, test := range secondsTimeZoneOffsetTests { 602 time, err := Parse(test.format, test.value) 603 if err != nil { 604 t.Fatal("error parsing date:", err) 605 } 606 _, offset := time.Zone() 607 if offset != test.expectedoffset { 608 t.Errorf("ZoneOffset = %d, want %d", offset, test.expectedoffset) 609 } 610 } 611 } 612 613 func TestFormatSecondsInTimeZone(t *testing.T) { 614 for _, test := range secondsTimeZoneOffsetTests { 615 d := Date(1871, 1, 1, 5, 33, 2, 0, FixedZone("LMT", test.expectedoffset)) 616 timestr := d.Format(test.format) 617 if timestr != test.value { 618 t.Errorf("Format = %s, want %s", timestr, test.value) 619 } 620 } 621 } 622 623 // Issue 11334. 624 func TestUnderscoreTwoThousand(t *testing.T) { 625 format := "15:04_20060102" 626 input := "14:38_20150618" 627 time, err := Parse(format, input) 628 if err != nil { 629 t.Error(err) 630 } 631 if y, m, d := time.Date(); y != 2015 || m != 6 || d != 18 { 632 t.Errorf("Incorrect y/m/d, got %d/%d/%d", y, m, d) 633 } 634 if h := time.Hour(); h != 14 { 635 t.Errorf("Incorrect hour, got %d", h) 636 } 637 if m := time.Minute(); m != 38 { 638 t.Errorf("Incorrect minute, got %d", m) 639 } 640 } 641 642 // Issue 29918, 29916 643 func TestStd0xParseError(t *testing.T) { 644 tests := []struct { 645 format, value, valueElemPrefix string 646 }{ 647 {"01 MST", "0 MST", "0"}, 648 {"01 MST", "1 MST", "1"}, 649 {RFC850, "Thursday, 04-Feb-1 21:00:57 PST", "1"}, 650 } 651 for _, tt := range tests { 652 _, err := Parse(tt.format, tt.value) 653 if err == nil { 654 t.Errorf("Parse(%q, %q) did not fail as expected", tt.format, tt.value) 655 } else if perr, ok := err.(*ParseError); !ok { 656 t.Errorf("Parse(%q, %q) returned error type %T, expected ParseError", tt.format, tt.value, perr) 657 } else if !strings.Contains(perr.Error(), "cannot parse") || !strings.HasPrefix(perr.ValueElem, tt.valueElemPrefix) { 658 t.Errorf("Parse(%q, %q) returned wrong parsing error message: %v", tt.format, tt.value, perr) 659 } 660 } 661 } 662 663 var monthOutOfRangeTests = []struct { 664 value string 665 ok bool 666 }{ 667 {"00-01", false}, 668 {"13-01", false}, 669 {"01-01", true}, 670 } 671 672 func TestParseMonthOutOfRange(t *testing.T) { 673 for _, test := range monthOutOfRangeTests { 674 _, err := Parse("01-02", test.value) 675 switch { 676 case !test.ok && err != nil: 677 if !strings.Contains(err.Error(), "month out of range") { 678 t.Errorf("%q: expected 'month' error, got %v", test.value, err) 679 } 680 case test.ok && err != nil: 681 t.Errorf("%q: unexpected error: %v", test.value, err) 682 case !test.ok && err == nil: 683 t.Errorf("%q: expected 'month' error, got none", test.value) 684 } 685 } 686 }