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