rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/time/format.go (about) 1 // Copyright 2010 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 6 7 import "errors" 8 9 // These are predefined layouts for use in Time.Format and Time.Parse. 10 // The reference time used in the layouts is the specific time: 11 // Mon Jan 2 15:04:05 MST 2006 12 // which is Unix time 1136239445. Since MST is GMT-0700, 13 // the reference time can be thought of as 14 // 01/02 03:04:05PM '06 -0700 15 // To define your own format, write down what the reference time would look 16 // like formatted your way; see the values of constants like ANSIC, 17 // StampMicro or Kitchen for examples. The model is to demonstrate what the 18 // reference time looks like so that the Format and Parse methods can apply 19 // the same transformation to a general time value. 20 // 21 // Within the format string, an underscore _ represents a space that may be 22 // replaced by a digit if the following number (a day) has two digits; for 23 // compatibility with fixed-width Unix time formats. 24 // 25 // A decimal point followed by one or more zeros represents a fractional 26 // second, printed to the given number of decimal places. A decimal point 27 // followed by one or more nines represents a fractional second, printed to 28 // the given number of decimal places, with trailing zeros removed. 29 // When parsing (only), the input may contain a fractional second 30 // field immediately after the seconds field, even if the layout does not 31 // signify its presence. In that case a decimal point followed by a maximal 32 // series of digits is parsed as a fractional second. 33 // 34 // Numeric time zone offsets format as follows: 35 // -0700 ±hhmm 36 // -07:00 ±hh:mm 37 // Replacing the sign in the format with a Z triggers 38 // the ISO 8601 behavior of printing Z instead of an 39 // offset for the UTC zone. Thus: 40 // Z0700 Z or ±hhmm 41 // Z07:00 Z or ±hh:mm 42 const ( 43 ANSIC = "Mon Jan _2 15:04:05 2006" 44 UnixDate = "Mon Jan _2 15:04:05 MST 2006" 45 RubyDate = "Mon Jan 02 15:04:05 -0700 2006" 46 RFC822 = "02 Jan 06 15:04 MST" 47 RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone 48 RFC850 = "Monday, 02-Jan-06 15:04:05 MST" 49 RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" 50 RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone 51 RFC3339 = "2006-01-02T15:04:05Z07:00" 52 RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" 53 Kitchen = "3:04PM" 54 // Handy time stamps. 55 Stamp = "Jan _2 15:04:05" 56 StampMilli = "Jan _2 15:04:05.000" 57 StampMicro = "Jan _2 15:04:05.000000" 58 StampNano = "Jan _2 15:04:05.000000000" 59 ) 60 61 const ( 62 _ = iota 63 stdLongMonth = iota + stdNeedDate // "January" 64 stdMonth // "Jan" 65 stdNumMonth // "1" 66 stdZeroMonth // "01" 67 stdLongWeekDay // "Monday" 68 stdWeekDay // "Mon" 69 stdDay // "2" 70 stdUnderDay // "_2" 71 stdZeroDay // "02" 72 stdHour = iota + stdNeedClock // "15" 73 stdHour12 // "3" 74 stdZeroHour12 // "03" 75 stdMinute // "4" 76 stdZeroMinute // "04" 77 stdSecond // "5" 78 stdZeroSecond // "05" 79 stdLongYear = iota + stdNeedDate // "2006" 80 stdYear // "06" 81 stdPM = iota + stdNeedClock // "PM" 82 stdpm // "pm" 83 stdTZ = iota // "MST" 84 stdISO8601TZ // "Z0700" // prints Z for UTC 85 stdISO8601SecondsTZ // "Z070000" 86 stdISO8601ColonTZ // "Z07:00" // prints Z for UTC 87 stdISO8601ColonSecondsTZ // "Z07:00:00" 88 stdNumTZ // "-0700" // always numeric 89 stdNumSecondsTz // "-070000" 90 stdNumShortTZ // "-07" // always numeric 91 stdNumColonTZ // "-07:00" // always numeric 92 stdNumColonSecondsTZ // "-07:00:00" 93 stdFracSecond0 // ".0", ".00", ... , trailing zeros included 94 stdFracSecond9 // ".9", ".99", ..., trailing zeros omitted 95 96 stdNeedDate = 1 << 8 // need month, day, year 97 stdNeedClock = 2 << 8 // need hour, minute, second 98 stdArgShift = 16 // extra argument in high bits, above low stdArgShift 99 stdMask = 1<<stdArgShift - 1 // mask out argument 100 ) 101 102 // std0x records the std values for "01", "02", ..., "06". 103 var std0x = [...]int{stdZeroMonth, stdZeroDay, stdZeroHour12, stdZeroMinute, stdZeroSecond, stdYear} 104 105 // startsWithLowerCase reports whether the string has a lower-case letter at the beginning. 106 // Its purpose is to prevent matching strings like "Month" when looking for "Mon". 107 func startsWithLowerCase(str string) bool { 108 if len(str) == 0 { 109 return false 110 } 111 c := str[0] 112 return 'a' <= c && c <= 'z' 113 } 114 115 // nextStdChunk finds the first occurrence of a std string in 116 // layout and returns the text before, the std string, and the text after. 117 func nextStdChunk(layout string) (prefix string, std int, suffix string) { 118 for i := 0; i < len(layout); i++ { 119 switch c := int(layout[i]); c { 120 case 'J': // January, Jan 121 if len(layout) >= i+3 && layout[i:i+3] == "Jan" { 122 if len(layout) >= i+7 && layout[i:i+7] == "January" { 123 return layout[0:i], stdLongMonth, layout[i+7:] 124 } 125 if !startsWithLowerCase(layout[i+3:]) { 126 return layout[0:i], stdMonth, layout[i+3:] 127 } 128 } 129 130 case 'M': // Monday, Mon, MST 131 if len(layout) >= i+3 { 132 if layout[i:i+3] == "Mon" { 133 if len(layout) >= i+6 && layout[i:i+6] == "Monday" { 134 return layout[0:i], stdLongWeekDay, layout[i+6:] 135 } 136 if !startsWithLowerCase(layout[i+3:]) { 137 return layout[0:i], stdWeekDay, layout[i+3:] 138 } 139 } 140 if layout[i:i+3] == "MST" { 141 return layout[0:i], stdTZ, layout[i+3:] 142 } 143 } 144 145 case '0': // 01, 02, 03, 04, 05, 06 146 if len(layout) >= i+2 && '1' <= layout[i+1] && layout[i+1] <= '6' { 147 return layout[0:i], std0x[layout[i+1]-'1'], layout[i+2:] 148 } 149 150 case '1': // 15, 1 151 if len(layout) >= i+2 && layout[i+1] == '5' { 152 return layout[0:i], stdHour, layout[i+2:] 153 } 154 return layout[0:i], stdNumMonth, layout[i+1:] 155 156 case '2': // 2006, 2 157 if len(layout) >= i+4 && layout[i:i+4] == "2006" { 158 return layout[0:i], stdLongYear, layout[i+4:] 159 } 160 return layout[0:i], stdDay, layout[i+1:] 161 162 case '_': // _2 163 if len(layout) >= i+2 && layout[i+1] == '2' { 164 return layout[0:i], stdUnderDay, layout[i+2:] 165 } 166 167 case '3': 168 return layout[0:i], stdHour12, layout[i+1:] 169 170 case '4': 171 return layout[0:i], stdMinute, layout[i+1:] 172 173 case '5': 174 return layout[0:i], stdSecond, layout[i+1:] 175 176 case 'P': // PM 177 if len(layout) >= i+2 && layout[i+1] == 'M' { 178 return layout[0:i], stdPM, layout[i+2:] 179 } 180 181 case 'p': // pm 182 if len(layout) >= i+2 && layout[i+1] == 'm' { 183 return layout[0:i], stdpm, layout[i+2:] 184 } 185 186 case '-': // -070000, -07:00:00, -0700, -07:00, -07 187 if len(layout) >= i+7 && layout[i:i+7] == "-070000" { 188 return layout[0:i], stdNumSecondsTz, layout[i+7:] 189 } 190 if len(layout) >= i+9 && layout[i:i+9] == "-07:00:00" { 191 return layout[0:i], stdNumColonSecondsTZ, layout[i+9:] 192 } 193 if len(layout) >= i+5 && layout[i:i+5] == "-0700" { 194 return layout[0:i], stdNumTZ, layout[i+5:] 195 } 196 if len(layout) >= i+6 && layout[i:i+6] == "-07:00" { 197 return layout[0:i], stdNumColonTZ, layout[i+6:] 198 } 199 if len(layout) >= i+3 && layout[i:i+3] == "-07" { 200 return layout[0:i], stdNumShortTZ, layout[i+3:] 201 } 202 203 case 'Z': // Z070000, Z07:00:00, Z0700, Z07:00, 204 if len(layout) >= i+7 && layout[i:i+7] == "Z070000" { 205 return layout[0:i], stdISO8601SecondsTZ, layout[i+7:] 206 } 207 if len(layout) >= i+9 && layout[i:i+9] == "Z07:00:00" { 208 return layout[0:i], stdISO8601ColonSecondsTZ, layout[i+9:] 209 } 210 if len(layout) >= i+5 && layout[i:i+5] == "Z0700" { 211 return layout[0:i], stdISO8601TZ, layout[i+5:] 212 } 213 if len(layout) >= i+6 && layout[i:i+6] == "Z07:00" { 214 return layout[0:i], stdISO8601ColonTZ, layout[i+6:] 215 } 216 217 case '.': // .000 or .999 - repeated digits for fractional seconds. 218 if i+1 < len(layout) && (layout[i+1] == '0' || layout[i+1] == '9') { 219 ch := layout[i+1] 220 j := i + 1 221 for j < len(layout) && layout[j] == ch { 222 j++ 223 } 224 // String of digits must end here - only fractional second is all digits. 225 if !isDigit(layout, j) { 226 std := stdFracSecond0 227 if layout[i+1] == '9' { 228 std = stdFracSecond9 229 } 230 std |= (j - (i + 1)) << stdArgShift 231 return layout[0:i], std, layout[j:] 232 } 233 } 234 } 235 } 236 return layout, 0, "" 237 } 238 239 var longDayNames = []string{ 240 "Sunday", 241 "Monday", 242 "Tuesday", 243 "Wednesday", 244 "Thursday", 245 "Friday", 246 "Saturday", 247 } 248 249 var shortDayNames = []string{ 250 "Sun", 251 "Mon", 252 "Tue", 253 "Wed", 254 "Thu", 255 "Fri", 256 "Sat", 257 } 258 259 var shortMonthNames = []string{ 260 "---", 261 "Jan", 262 "Feb", 263 "Mar", 264 "Apr", 265 "May", 266 "Jun", 267 "Jul", 268 "Aug", 269 "Sep", 270 "Oct", 271 "Nov", 272 "Dec", 273 } 274 275 var longMonthNames = []string{ 276 "---", 277 "January", 278 "February", 279 "March", 280 "April", 281 "May", 282 "June", 283 "July", 284 "August", 285 "September", 286 "October", 287 "November", 288 "December", 289 } 290 291 // match reports whether s1 and s2 match ignoring case. 292 // It is assumed s1 and s2 are the same length. 293 func match(s1, s2 string) bool { 294 for i := 0; i < len(s1); i++ { 295 c1 := s1[i] 296 c2 := s2[i] 297 if c1 != c2 { 298 // Switch to lower-case; 'a'-'A' is known to be a single bit. 299 c1 |= 'a' - 'A' 300 c2 |= 'a' - 'A' 301 if c1 != c2 || c1 < 'a' || c1 > 'z' { 302 return false 303 } 304 } 305 } 306 return true 307 } 308 309 func lookup(tab []string, val string) (int, string, error) { 310 for i, v := range tab { 311 if len(val) >= len(v) && match(val[0:len(v)], v) { 312 return i, val[len(v):], nil 313 } 314 } 315 return -1, val, errBad 316 } 317 318 // appendInt appends the decimal form of x to b and returns the result. 319 // If the decimal form (excluding sign) is shorter than width, the result is padded with leading 0's. 320 // Duplicates functionality in strconv, but avoids dependency. 321 func appendInt(b []byte, x int, width int) []byte { 322 u := uint(x) 323 if x < 0 { 324 b = append(b, '-') 325 u = uint(-x) 326 } 327 328 // Assemble decimal in reverse order. 329 var buf [20]byte 330 i := len(buf) 331 for u >= 10 { 332 i-- 333 q := u / 10 334 buf[i] = byte('0' + u - q*10) 335 u = q 336 } 337 i-- 338 buf[i] = byte('0' + u) 339 340 // Add 0-padding. 341 for w := len(buf) - i; w < width; w++ { 342 b = append(b, '0') 343 } 344 345 return append(b, buf[i:]...) 346 } 347 348 // Never printed, just needs to be non-nil for return by atoi. 349 var atoiError = errors.New("time: invalid number") 350 351 // Duplicates functionality in strconv, but avoids dependency. 352 func atoi(s string) (x int, err error) { 353 neg := false 354 if s != "" && (s[0] == '-' || s[0] == '+') { 355 neg = s[0] == '-' 356 s = s[1:] 357 } 358 q, rem, err := leadingInt(s) 359 x = int(q) 360 if err != nil || rem != "" { 361 return 0, atoiError 362 } 363 if neg { 364 x = -x 365 } 366 return x, nil 367 } 368 369 // formatNano appends a fractional second, as nanoseconds, to b 370 // and returns the result. 371 func formatNano(b []byte, nanosec uint, n int, trim bool) []byte { 372 u := nanosec 373 var buf [9]byte 374 for start := len(buf); start > 0; { 375 start-- 376 buf[start] = byte(u%10 + '0') 377 u /= 10 378 } 379 380 if n > 9 { 381 n = 9 382 } 383 if trim { 384 for n > 0 && buf[n-1] == '0' { 385 n-- 386 } 387 if n == 0 { 388 return b 389 } 390 } 391 b = append(b, '.') 392 return append(b, buf[:n]...) 393 } 394 395 // String returns the time formatted using the format string 396 // "2006-01-02 15:04:05.999999999 -0700 MST" 397 func (t Time) String() string { 398 return t.Format("2006-01-02 15:04:05.999999999 -0700 MST") 399 } 400 401 // Format returns a textual representation of the time value formatted 402 // according to layout, which defines the format by showing how the reference 403 // time, defined to be 404 // Mon Jan 2 15:04:05 -0700 MST 2006 405 // would be displayed if it were the value; it serves as an example of the 406 // desired output. The same display rules will then be applied to the time 407 // value. 408 // Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard 409 // and convenient representations of the reference time. For more information 410 // about the formats and the definition of the reference time, see the 411 // documentation for ANSIC and the other constants defined by this package. 412 func (t Time) Format(layout string) string { 413 const bufSize = 64 414 var b []byte 415 max := len(layout) + 10 416 if max < bufSize { 417 var buf [bufSize]byte 418 b = buf[:0] 419 } else { 420 b = make([]byte, 0, max) 421 } 422 b = t.AppendFormat(b, layout) 423 return string(b) 424 } 425 426 // AppendFormat is like Format but appends the textual 427 // representation to b and returns the extended buffer. 428 func (t Time) AppendFormat(b []byte, layout string) []byte { 429 var ( 430 name, offset, abs = t.locabs() 431 432 year int = -1 433 month Month 434 day int 435 hour int = -1 436 min int 437 sec int 438 ) 439 // Each iteration generates one std value. 440 for layout != "" { 441 prefix, std, suffix := nextStdChunk(layout) 442 if prefix != "" { 443 b = append(b, prefix...) 444 } 445 if std == 0 { 446 break 447 } 448 layout = suffix 449 450 // Compute year, month, day if needed. 451 if year < 0 && std&stdNeedDate != 0 { 452 year, month, day, _ = absDate(abs, true) 453 } 454 455 // Compute hour, minute, second if needed. 456 if hour < 0 && std&stdNeedClock != 0 { 457 hour, min, sec = absClock(abs) 458 } 459 460 switch std & stdMask { 461 case stdYear: 462 y := year 463 if y < 0 { 464 y = -y 465 } 466 b = appendInt(b, y%100, 2) 467 case stdLongYear: 468 b = appendInt(b, year, 4) 469 case stdMonth: 470 b = append(b, month.String()[:3]...) 471 case stdLongMonth: 472 m := month.String() 473 b = append(b, m...) 474 case stdNumMonth: 475 b = appendInt(b, int(month), 0) 476 case stdZeroMonth: 477 b = appendInt(b, int(month), 2) 478 case stdWeekDay: 479 b = append(b, absWeekday(abs).String()[:3]...) 480 case stdLongWeekDay: 481 s := absWeekday(abs).String() 482 b = append(b, s...) 483 case stdDay: 484 b = appendInt(b, day, 0) 485 case stdUnderDay: 486 if day < 10 { 487 b = append(b, ' ') 488 } 489 b = appendInt(b, day, 0) 490 case stdZeroDay: 491 b = appendInt(b, day, 2) 492 case stdHour: 493 b = appendInt(b, hour, 2) 494 case stdHour12: 495 // Noon is 12PM, midnight is 12AM. 496 hr := hour % 12 497 if hr == 0 { 498 hr = 12 499 } 500 b = appendInt(b, hr, 0) 501 case stdZeroHour12: 502 // Noon is 12PM, midnight is 12AM. 503 hr := hour % 12 504 if hr == 0 { 505 hr = 12 506 } 507 b = appendInt(b, hr, 2) 508 case stdMinute: 509 b = appendInt(b, min, 0) 510 case stdZeroMinute: 511 b = appendInt(b, min, 2) 512 case stdSecond: 513 b = appendInt(b, sec, 2) 514 case stdZeroSecond: 515 b = appendInt(b, sec, 2) 516 case stdPM: 517 if hour >= 12 { 518 b = append(b, "PM"...) 519 } else { 520 b = append(b, "AM"...) 521 } 522 case stdpm: 523 if hour >= 12 { 524 b = append(b, "pm"...) 525 } else { 526 b = append(b, "am"...) 527 } 528 case stdISO8601TZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ColonSecondsTZ, stdNumTZ, stdNumColonTZ, stdNumSecondsTz, stdNumColonSecondsTZ: 529 // Ugly special case. We cheat and take the "Z" variants 530 // to mean "the time zone as formatted for ISO 8601". 531 if offset == 0 && (std == stdISO8601TZ || std == stdISO8601ColonTZ || std == stdISO8601SecondsTZ || std == stdISO8601ColonSecondsTZ) { 532 b = append(b, 'Z') 533 break 534 } 535 zone := offset / 60 // convert to minutes 536 absoffset := offset 537 if zone < 0 { 538 b = append(b, '-') 539 zone = -zone 540 absoffset = -absoffset 541 } else { 542 b = append(b, '+') 543 } 544 b = appendInt(b, zone/60, 2) 545 if std == stdISO8601ColonTZ || std == stdNumColonTZ || std == stdISO8601ColonSecondsTZ || std == stdNumColonSecondsTZ { 546 b = append(b, ':') 547 } 548 b = appendInt(b, zone%60, 2) 549 550 // append seconds if appropriate 551 if std == stdISO8601SecondsTZ || std == stdNumSecondsTz || std == stdNumColonSecondsTZ || std == stdISO8601ColonSecondsTZ { 552 if std == stdNumColonSecondsTZ || std == stdISO8601ColonSecondsTZ { 553 b = append(b, ':') 554 } 555 b = appendInt(b, absoffset%60, 2) 556 } 557 558 case stdTZ: 559 if name != "" { 560 b = append(b, name...) 561 break 562 } 563 // No time zone known for this time, but we must print one. 564 // Use the -0700 format. 565 zone := offset / 60 // convert to minutes 566 if zone < 0 { 567 b = append(b, '-') 568 zone = -zone 569 } else { 570 b = append(b, '+') 571 } 572 b = appendInt(b, zone/60, 2) 573 b = appendInt(b, zone%60, 2) 574 case stdFracSecond0, stdFracSecond9: 575 b = formatNano(b, uint(t.Nanosecond()), std>>stdArgShift, std&stdMask == stdFracSecond9) 576 } 577 } 578 return b 579 } 580 581 var errBad = errors.New("bad value for field") // placeholder not passed to user 582 583 // ParseError describes a problem parsing a time string. 584 type ParseError struct { 585 Layout string 586 Value string 587 LayoutElem string 588 ValueElem string 589 Message string 590 } 591 592 func quote(s string) string { 593 return "\"" + s + "\"" 594 } 595 596 // Error returns the string representation of a ParseError. 597 func (e *ParseError) Error() string { 598 if e.Message == "" { 599 return "parsing time " + 600 quote(e.Value) + " as " + 601 quote(e.Layout) + ": cannot parse " + 602 quote(e.ValueElem) + " as " + 603 quote(e.LayoutElem) 604 } 605 return "parsing time " + 606 quote(e.Value) + e.Message 607 } 608 609 // isDigit reports whether s[i] is in range and is a decimal digit. 610 func isDigit(s string, i int) bool { 611 if len(s) <= i { 612 return false 613 } 614 c := s[i] 615 return '0' <= c && c <= '9' 616 } 617 618 // getnum parses s[0:1] or s[0:2] (fixed forces the latter) 619 // as a decimal integer and returns the integer and the 620 // remainder of the string. 621 func getnum(s string, fixed bool) (int, string, error) { 622 if !isDigit(s, 0) { 623 return 0, s, errBad 624 } 625 if !isDigit(s, 1) { 626 if fixed { 627 return 0, s, errBad 628 } 629 return int(s[0] - '0'), s[1:], nil 630 } 631 return int(s[0]-'0')*10 + int(s[1]-'0'), s[2:], nil 632 } 633 634 func cutspace(s string) string { 635 for len(s) > 0 && s[0] == ' ' { 636 s = s[1:] 637 } 638 return s 639 } 640 641 // skip removes the given prefix from value, 642 // treating runs of space characters as equivalent. 643 func skip(value, prefix string) (string, error) { 644 for len(prefix) > 0 { 645 if prefix[0] == ' ' { 646 if len(value) > 0 && value[0] != ' ' { 647 return value, errBad 648 } 649 prefix = cutspace(prefix) 650 value = cutspace(value) 651 continue 652 } 653 if len(value) == 0 || value[0] != prefix[0] { 654 return value, errBad 655 } 656 prefix = prefix[1:] 657 value = value[1:] 658 } 659 return value, nil 660 } 661 662 // Parse parses a formatted string and returns the time value it represents. 663 // The layout defines the format by showing how the reference time, 664 // defined to be 665 // Mon Jan 2 15:04:05 -0700 MST 2006 666 // would be interpreted if it were the value; it serves as an example of 667 // the input format. The same interpretation will then be made to the 668 // input string. 669 // 670 // Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard 671 // and convenient representations of the reference time. For more information 672 // about the formats and the definition of the reference time, see the 673 // documentation for ANSIC and the other constants defined by this package. 674 // Also, the executable Example for time.Format demonstrates the working 675 // of the layout string in detail and is a good reference. 676 // 677 // Elements omitted from the value are assumed to be zero or, when 678 // zero is impossible, one, so parsing "3:04pm" returns the time 679 // corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is 680 // 0, this time is before the zero Time). 681 // Years must be in the range 0000..9999. The day of the week is checked 682 // for syntax but it is otherwise ignored. 683 // 684 // In the absence of a time zone indicator, Parse returns a time in UTC. 685 // 686 // When parsing a time with a zone offset like -0700, if the offset corresponds 687 // to a time zone used by the current location (Local), then Parse uses that 688 // location and zone in the returned time. Otherwise it records the time as 689 // being in a fabricated location with time fixed at the given zone offset. 690 // 691 // When parsing a time with a zone abbreviation like MST, if the zone abbreviation 692 // has a defined offset in the current location, then that offset is used. 693 // The zone abbreviation "UTC" is recognized as UTC regardless of location. 694 // If the zone abbreviation is unknown, Parse records the time as being 695 // in a fabricated location with the given zone abbreviation and a zero offset. 696 // This choice means that such a time can be parsed and reformatted with the 697 // same layout losslessly, but the exact instant used in the representation will 698 // differ by the actual zone offset. To avoid such problems, prefer time layouts 699 // that use a numeric zone offset, or use ParseInLocation. 700 func Parse(layout, value string) (Time, error) { 701 return parse(layout, value, UTC, Local) 702 } 703 704 // ParseInLocation is like Parse but differs in two important ways. 705 // First, in the absence of time zone information, Parse interprets a time as UTC; 706 // ParseInLocation interprets the time as in the given location. 707 // Second, when given a zone offset or abbreviation, Parse tries to match it 708 // against the Local location; ParseInLocation uses the given location. 709 func ParseInLocation(layout, value string, loc *Location) (Time, error) { 710 return parse(layout, value, loc, loc) 711 } 712 713 func parse(layout, value string, defaultLocation, local *Location) (Time, error) { 714 alayout, avalue := layout, value 715 rangeErrString := "" // set if a value is out of range 716 amSet := false // do we need to subtract 12 from the hour for midnight? 717 pmSet := false // do we need to add 12 to the hour? 718 719 // Time being constructed. 720 var ( 721 year int 722 month int = 1 // January 723 day int = 1 724 hour int 725 min int 726 sec int 727 nsec int 728 z *Location 729 zoneOffset int = -1 730 zoneName string 731 ) 732 733 // Each iteration processes one std value. 734 for { 735 var err error 736 prefix, std, suffix := nextStdChunk(layout) 737 stdstr := layout[len(prefix) : len(layout)-len(suffix)] 738 value, err = skip(value, prefix) 739 if err != nil { 740 return Time{}, &ParseError{alayout, avalue, prefix, value, ""} 741 } 742 if std == 0 { 743 if len(value) != 0 { 744 return Time{}, &ParseError{alayout, avalue, "", value, ": extra text: " + value} 745 } 746 break 747 } 748 layout = suffix 749 var p string 750 switch std & stdMask { 751 case stdYear: 752 if len(value) < 2 { 753 err = errBad 754 break 755 } 756 p, value = value[0:2], value[2:] 757 year, err = atoi(p) 758 if year >= 69 { // Unix time starts Dec 31 1969 in some time zones 759 year += 1900 760 } else { 761 year += 2000 762 } 763 case stdLongYear: 764 if len(value) < 4 || !isDigit(value, 0) { 765 err = errBad 766 break 767 } 768 p, value = value[0:4], value[4:] 769 year, err = atoi(p) 770 case stdMonth: 771 month, value, err = lookup(shortMonthNames, value) 772 case stdLongMonth: 773 month, value, err = lookup(longMonthNames, value) 774 case stdNumMonth, stdZeroMonth: 775 month, value, err = getnum(value, std == stdZeroMonth) 776 if month <= 0 || 12 < month { 777 rangeErrString = "month" 778 } 779 case stdWeekDay: 780 // Ignore weekday except for error checking. 781 _, value, err = lookup(shortDayNames, value) 782 case stdLongWeekDay: 783 _, value, err = lookup(longDayNames, value) 784 case stdDay, stdUnderDay, stdZeroDay: 785 if std == stdUnderDay && len(value) > 0 && value[0] == ' ' { 786 value = value[1:] 787 } 788 day, value, err = getnum(value, std == stdZeroDay) 789 if day < 0 || 31 < day { 790 rangeErrString = "day" 791 } 792 case stdHour: 793 hour, value, err = getnum(value, false) 794 if hour < 0 || 24 <= hour { 795 rangeErrString = "hour" 796 } 797 case stdHour12, stdZeroHour12: 798 hour, value, err = getnum(value, std == stdZeroHour12) 799 if hour < 0 || 12 < hour { 800 rangeErrString = "hour" 801 } 802 case stdMinute, stdZeroMinute: 803 min, value, err = getnum(value, std == stdZeroMinute) 804 if min < 0 || 60 <= min { 805 rangeErrString = "minute" 806 } 807 case stdSecond, stdZeroSecond: 808 sec, value, err = getnum(value, std == stdZeroSecond) 809 if sec < 0 || 60 <= sec { 810 rangeErrString = "second" 811 } 812 // Special case: do we have a fractional second but no 813 // fractional second in the format? 814 if len(value) >= 2 && value[0] == '.' && isDigit(value, 1) { 815 _, std, _ = nextStdChunk(layout) 816 std &= stdMask 817 if std == stdFracSecond0 || std == stdFracSecond9 { 818 // Fractional second in the layout; proceed normally 819 break 820 } 821 // No fractional second in the layout but we have one in the input. 822 n := 2 823 for ; n < len(value) && isDigit(value, n); n++ { 824 } 825 nsec, rangeErrString, err = parseNanoseconds(value, n) 826 value = value[n:] 827 } 828 case stdPM: 829 if len(value) < 2 { 830 err = errBad 831 break 832 } 833 p, value = value[0:2], value[2:] 834 switch p { 835 case "PM": 836 pmSet = true 837 case "AM": 838 amSet = true 839 default: 840 err = errBad 841 } 842 case stdpm: 843 if len(value) < 2 { 844 err = errBad 845 break 846 } 847 p, value = value[0:2], value[2:] 848 switch p { 849 case "pm": 850 pmSet = true 851 case "am": 852 amSet = true 853 default: 854 err = errBad 855 } 856 case stdISO8601TZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ColonSecondsTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ, stdNumSecondsTz, stdNumColonSecondsTZ: 857 if (std == stdISO8601TZ || std == stdISO8601ColonTZ) && len(value) >= 1 && value[0] == 'Z' { 858 value = value[1:] 859 z = UTC 860 break 861 } 862 var sign, hour, min, seconds string 863 if std == stdISO8601ColonTZ || std == stdNumColonTZ { 864 if len(value) < 6 { 865 err = errBad 866 break 867 } 868 if value[3] != ':' { 869 err = errBad 870 break 871 } 872 sign, hour, min, seconds, value = value[0:1], value[1:3], value[4:6], "00", value[6:] 873 } else if std == stdNumShortTZ { 874 if len(value) < 3 { 875 err = errBad 876 break 877 } 878 sign, hour, min, seconds, value = value[0:1], value[1:3], "00", "00", value[3:] 879 } else if std == stdISO8601ColonSecondsTZ || std == stdNumColonSecondsTZ { 880 if len(value) < 9 { 881 err = errBad 882 break 883 } 884 if value[3] != ':' || value[6] != ':' { 885 err = errBad 886 break 887 } 888 sign, hour, min, seconds, value = value[0:1], value[1:3], value[4:6], value[7:9], value[9:] 889 } else if std == stdISO8601SecondsTZ || std == stdNumSecondsTz { 890 if len(value) < 7 { 891 err = errBad 892 break 893 } 894 sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], value[5:7], value[7:] 895 } else { 896 if len(value) < 5 { 897 err = errBad 898 break 899 } 900 sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], "00", value[5:] 901 } 902 var hr, mm, ss int 903 hr, err = atoi(hour) 904 if err == nil { 905 mm, err = atoi(min) 906 } 907 if err == nil { 908 ss, err = atoi(seconds) 909 } 910 zoneOffset = (hr*60+mm)*60 + ss // offset is in seconds 911 switch sign[0] { 912 case '+': 913 case '-': 914 zoneOffset = -zoneOffset 915 default: 916 err = errBad 917 } 918 case stdTZ: 919 // Does it look like a time zone? 920 if len(value) >= 3 && value[0:3] == "UTC" { 921 z = UTC 922 value = value[3:] 923 break 924 } 925 n, ok := parseTimeZone(value) 926 if !ok { 927 err = errBad 928 break 929 } 930 zoneName, value = value[:n], value[n:] 931 932 case stdFracSecond0: 933 // stdFracSecond0 requires the exact number of digits as specified in 934 // the layout. 935 ndigit := 1 + (std >> stdArgShift) 936 if len(value) < ndigit { 937 err = errBad 938 break 939 } 940 nsec, rangeErrString, err = parseNanoseconds(value, ndigit) 941 value = value[ndigit:] 942 943 case stdFracSecond9: 944 if len(value) < 2 || value[0] != '.' || value[1] < '0' || '9' < value[1] { 945 // Fractional second omitted. 946 break 947 } 948 // Take any number of digits, even more than asked for, 949 // because it is what the stdSecond case would do. 950 i := 0 951 for i < 9 && i+1 < len(value) && '0' <= value[i+1] && value[i+1] <= '9' { 952 i++ 953 } 954 nsec, rangeErrString, err = parseNanoseconds(value, 1+i) 955 value = value[1+i:] 956 } 957 if rangeErrString != "" { 958 return Time{}, &ParseError{alayout, avalue, stdstr, value, ": " + rangeErrString + " out of range"} 959 } 960 if err != nil { 961 return Time{}, &ParseError{alayout, avalue, stdstr, value, ""} 962 } 963 } 964 if pmSet && hour < 12 { 965 hour += 12 966 } else if amSet && hour == 12 { 967 hour = 0 968 } 969 970 if z != nil { 971 return Date(year, Month(month), day, hour, min, sec, nsec, z), nil 972 } 973 974 if zoneOffset != -1 { 975 t := Date(year, Month(month), day, hour, min, sec, nsec, UTC) 976 t.sec -= int64(zoneOffset) 977 978 // Look for local zone with the given offset. 979 // If that zone was in effect at the given time, use it. 980 name, offset, _, _, _ := local.lookup(t.sec + internalToUnix) 981 if offset == zoneOffset && (zoneName == "" || name == zoneName) { 982 t.loc = local 983 return t, nil 984 } 985 986 // Otherwise create fake zone to record offset. 987 t.loc = FixedZone(zoneName, zoneOffset) 988 return t, nil 989 } 990 991 if zoneName != "" { 992 t := Date(year, Month(month), day, hour, min, sec, nsec, UTC) 993 // Look for local zone with the given offset. 994 // If that zone was in effect at the given time, use it. 995 offset, _, ok := local.lookupName(zoneName, t.sec+internalToUnix) 996 if ok { 997 t.sec -= int64(offset) 998 t.loc = local 999 return t, nil 1000 } 1001 1002 // Otherwise, create fake zone with unknown offset. 1003 if len(zoneName) > 3 && zoneName[:3] == "GMT" { 1004 offset, _ = atoi(zoneName[3:]) // Guaranteed OK by parseGMT. 1005 offset *= 3600 1006 } 1007 t.loc = FixedZone(zoneName, offset) 1008 return t, nil 1009 } 1010 1011 // Otherwise, fall back to default. 1012 return Date(year, Month(month), day, hour, min, sec, nsec, defaultLocation), nil 1013 } 1014 1015 // parseTimeZone parses a time zone string and returns its length. Time zones 1016 // are human-generated and unpredictable. We can't do precise error checking. 1017 // On the other hand, for a correct parse there must be a time zone at the 1018 // beginning of the string, so it's almost always true that there's one 1019 // there. We look at the beginning of the string for a run of upper-case letters. 1020 // If there are more than 5, it's an error. 1021 // If there are 4 or 5 and the last is a T, it's a time zone. 1022 // If there are 3, it's a time zone. 1023 // Otherwise, other than special cases, it's not a time zone. 1024 // GMT is special because it can have an hour offset. 1025 func parseTimeZone(value string) (length int, ok bool) { 1026 if len(value) < 3 { 1027 return 0, false 1028 } 1029 // Special case 1: ChST and MeST are the only zones with a lower-case letter. 1030 if len(value) >= 4 && (value[:4] == "ChST" || value[:4] == "MeST") { 1031 return 4, true 1032 } 1033 // Special case 2: GMT may have an hour offset; treat it specially. 1034 if value[:3] == "GMT" { 1035 length = parseGMT(value) 1036 return length, true 1037 } 1038 // How many upper-case letters are there? Need at least three, at most five. 1039 var nUpper int 1040 for nUpper = 0; nUpper < 6; nUpper++ { 1041 if nUpper >= len(value) { 1042 break 1043 } 1044 if c := value[nUpper]; c < 'A' || 'Z' < c { 1045 break 1046 } 1047 } 1048 switch nUpper { 1049 case 0, 1, 2, 6: 1050 return 0, false 1051 case 5: // Must end in T to match. 1052 if value[4] == 'T' { 1053 return 5, true 1054 } 1055 case 4: // Must end in T to match. 1056 if value[3] == 'T' { 1057 return 4, true 1058 } 1059 case 3: 1060 return 3, true 1061 } 1062 return 0, false 1063 } 1064 1065 // parseGMT parses a GMT time zone. The input string is known to start "GMT". 1066 // The function checks whether that is followed by a sign and a number in the 1067 // range -14 through 12 excluding zero. 1068 func parseGMT(value string) int { 1069 value = value[3:] 1070 if len(value) == 0 { 1071 return 3 1072 } 1073 sign := value[0] 1074 if sign != '-' && sign != '+' { 1075 return 3 1076 } 1077 x, rem, err := leadingInt(value[1:]) 1078 if err != nil { 1079 return 3 1080 } 1081 if sign == '-' { 1082 x = -x 1083 } 1084 if x == 0 || x < -14 || 12 < x { 1085 return 3 1086 } 1087 return 3 + len(value) - len(rem) 1088 } 1089 1090 func parseNanoseconds(value string, nbytes int) (ns int, rangeErrString string, err error) { 1091 if value[0] != '.' { 1092 err = errBad 1093 return 1094 } 1095 if ns, err = atoi(value[1:nbytes]); err != nil { 1096 return 1097 } 1098 if ns < 0 || 1e9 <= ns { 1099 rangeErrString = "fractional second" 1100 return 1101 } 1102 // We need nanoseconds, which means scaling by the number 1103 // of missing digits in the format, maximum length 10. If it's 1104 // longer than 10, we won't scale. 1105 scaleDigits := 10 - nbytes 1106 for i := 0; i < scaleDigits; i++ { 1107 ns *= 10 1108 } 1109 return 1110 } 1111 1112 var errLeadingInt = errors.New("time: bad [0-9]*") // never printed 1113 1114 // leadingInt consumes the leading [0-9]* from s. 1115 func leadingInt(s string) (x int64, rem string, err error) { 1116 i := 0 1117 for ; i < len(s); i++ { 1118 c := s[i] 1119 if c < '0' || c > '9' { 1120 break 1121 } 1122 if x > (1<<63-1)/10 { 1123 // overflow 1124 return 0, "", errLeadingInt 1125 } 1126 x = x*10 + int64(c) - '0' 1127 if x < 0 { 1128 // overflow 1129 return 0, "", errLeadingInt 1130 } 1131 } 1132 return x, s[i:], nil 1133 } 1134 1135 var unitMap = map[string]int64{ 1136 "ns": int64(Nanosecond), 1137 "us": int64(Microsecond), 1138 "µs": int64(Microsecond), // U+00B5 = micro symbol 1139 "μs": int64(Microsecond), // U+03BC = Greek letter mu 1140 "ms": int64(Millisecond), 1141 "s": int64(Second), 1142 "m": int64(Minute), 1143 "h": int64(Hour), 1144 } 1145 1146 // ParseDuration parses a duration string. 1147 // A duration string is a possibly signed sequence of 1148 // decimal numbers, each with optional fraction and a unit suffix, 1149 // such as "300ms", "-1.5h" or "2h45m". 1150 // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". 1151 func ParseDuration(s string) (Duration, error) { 1152 // [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+ 1153 orig := s 1154 var d int64 1155 neg := false 1156 1157 // Consume [-+]? 1158 if s != "" { 1159 c := s[0] 1160 if c == '-' || c == '+' { 1161 neg = c == '-' 1162 s = s[1:] 1163 } 1164 } 1165 // Special case: if all that is left is "0", this is zero. 1166 if s == "0" { 1167 return 0, nil 1168 } 1169 if s == "" { 1170 return 0, errors.New("time: invalid duration " + orig) 1171 } 1172 for s != "" { 1173 var ( 1174 v, f int64 // integers before, after decimal point 1175 scale float64 = 1 // value = v + f/scale 1176 ) 1177 1178 var err error 1179 1180 // The next character must be [0-9.] 1181 if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') { 1182 return 0, errors.New("time: invalid duration " + orig) 1183 } 1184 // Consume [0-9]* 1185 pl := len(s) 1186 v, s, err = leadingInt(s) 1187 if err != nil { 1188 return 0, errors.New("time: invalid duration " + orig) 1189 } 1190 pre := pl != len(s) // whether we consumed anything before a period 1191 1192 // Consume (\.[0-9]*)? 1193 post := false 1194 if s != "" && s[0] == '.' { 1195 s = s[1:] 1196 pl := len(s) 1197 f, s, err = leadingInt(s) 1198 if err != nil { 1199 return 0, errors.New("time: invalid duration " + orig) 1200 } 1201 for n := pl - len(s); n > 0; n-- { 1202 scale *= 10 1203 } 1204 post = pl != len(s) 1205 } 1206 if !pre && !post { 1207 // no digits (e.g. ".s" or "-.s") 1208 return 0, errors.New("time: invalid duration " + orig) 1209 } 1210 1211 // Consume unit. 1212 i := 0 1213 for ; i < len(s); i++ { 1214 c := s[i] 1215 if c == '.' || '0' <= c && c <= '9' { 1216 break 1217 } 1218 } 1219 if i == 0 { 1220 return 0, errors.New("time: missing unit in duration " + orig) 1221 } 1222 u := s[:i] 1223 s = s[i:] 1224 unit, ok := unitMap[u] 1225 if !ok { 1226 return 0, errors.New("time: unknown unit " + u + " in duration " + orig) 1227 } 1228 if v > (1<<63-1)/unit { 1229 // overflow 1230 return 0, errors.New("time: invalid duration " + orig) 1231 } 1232 v *= unit 1233 if f > 0 { 1234 // float64 is needed to be nanosecond accurate for fractions of hours. 1235 // v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit) 1236 v += int64(float64(f) * (float64(unit) / scale)) 1237 if v < 0 { 1238 // overflow 1239 return 0, errors.New("time: invalid duration " + orig) 1240 } 1241 } 1242 d += v 1243 if d < 0 { 1244 // overflow 1245 return 0, errors.New("time: invalid duration " + orig) 1246 } 1247 } 1248 1249 if neg { 1250 d = -d 1251 } 1252 return Duration(d), nil 1253 }