format.mx raw

   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 (
   8  	"errors"
   9  	"internal/stringslite"
  10  	"unicode/utf8"
  11  	_ "unsafe" // for linkname
  12  )
  13  
  14  // These are predefined layouts for use in [Time.Format] and [time.Parse].
  15  // The reference time used in these layouts is the specific time stamp:
  16  //
  17  //	01/02 03:04:05PM '06 -0700
  18  //
  19  // (January 2, 15:04:05, 2006, in time zone seven hours west of GMT).
  20  // That value is recorded as the constant named [Layout], listed below. As a Unix
  21  // time, this is 1136239445. Since MST is GMT-0700, the reference would be
  22  // printed by the Unix date command as:
  23  //
  24  //	Mon Jan 2 15:04:05 MST 2006
  25  //
  26  // It is a regrettable historic error that the date uses the American convention
  27  // of putting the numerical month before the day.
  28  //
  29  // The example for Time.Format demonstrates the working of the layout string
  30  // in detail and is a good reference.
  31  //
  32  // Note that the [RFC822], [RFC850], and [RFC1123] formats should be applied
  33  // only to local times. Applying them to UTC times will use "UTC" as the
  34  // time zone abbreviation, while strictly speaking those RFCs require the
  35  // use of "GMT" in that case.
  36  // When using the [RFC1123] or [RFC1123Z] formats for parsing, note that these
  37  // formats define a leading zero for the day-in-month portion, which is not
  38  // strictly allowed by RFC 1123. This will result in an error when parsing
  39  // date strings that occur in the first 9 days of a given month.
  40  // In general [RFC1123Z] should be used instead of [RFC1123] for servers
  41  // that insist on that format, and [RFC3339] should be preferred for new protocols.
  42  // [RFC3339], [RFC822], [RFC822Z], [RFC1123], and [RFC1123Z] are useful for formatting;
  43  // when used with time.Parse they do not accept all the time formats
  44  // permitted by the RFCs and they do accept time formats not formally defined.
  45  // The [RFC3339Nano] format removes trailing zeros from the seconds field
  46  // and thus may not sort correctly once formatted.
  47  //
  48  // Most programs can use one of the defined constants as the layout passed to
  49  // Format or Parse. The rest of this comment can be ignored unless you are
  50  // creating a custom layout string.
  51  //
  52  // To define your own format, write down what the reference time would look like
  53  // formatted your way; see the values of constants like [ANSIC], [StampMicro] or
  54  // [Kitchen] for examples. The model is to demonstrate what the reference time
  55  // looks like so that the Format and Parse methods can apply the same
  56  // transformation to a general time value.
  57  //
  58  // Here is a summary of the components of a layout string. Each element shows by
  59  // example the formatting of an element of the reference time. Only these values
  60  // are recognized. Text in the layout string that is not recognized as part of
  61  // the reference time is echoed verbatim during Format and expected to appear
  62  // verbatim in the input to Parse.
  63  //
  64  //	Year: "2006" "06"
  65  //	Month: "Jan" "January" "01" "1"
  66  //	Day of the week: "Mon" "Monday"
  67  //	Day of the month: "2" "_2" "02"
  68  //	Day of the year: "__2" "002"
  69  //	Hour: "15" "3" "03" (PM or AM)
  70  //	Minute: "4" "04"
  71  //	Second: "5" "05"
  72  //	AM/PM mark: "PM"
  73  //
  74  // Numeric time zone offsets format as follows:
  75  //
  76  //	"-0700"     ±hhmm
  77  //	"-07:00"    ±hh:mm
  78  //	"-07"       ±hh
  79  //	"-070000"   ±hhmmss
  80  //	"-07:00:00" ±hh:mm:ss
  81  //
  82  // Replacing the sign in the format with a Z triggers
  83  // the ISO 8601 behavior of printing Z instead of an
  84  // offset for the UTC zone. Thus:
  85  //
  86  //	"Z0700"      Z or ±hhmm
  87  //	"Z07:00"     Z or ±hh:mm
  88  //	"Z07"        Z or ±hh
  89  //	"Z070000"    Z or ±hhmmss
  90  //	"Z07:00:00"  Z or ±hh:mm:ss
  91  //
  92  // Within the format string, the underscores in "_2" and "__2" represent spaces
  93  // that may be replaced by digits if the following number has multiple digits,
  94  // for compatibility with fixed-width Unix time formats. A leading zero represents
  95  // a zero-padded value.
  96  //
  97  // The formats __2 and 002 are space-padded and zero-padded
  98  // three-character day of year; there is no unpadded day of year format.
  99  //
 100  // A comma or decimal point followed by one or more zeros represents
 101  // a fractional second, printed to the given number of decimal places.
 102  // A comma or decimal point followed by one or more nines represents
 103  // a fractional second, printed to the given number of decimal places, with
 104  // trailing zeros removed.
 105  // For example "15:04:05,000" or "15:04:05.000" formats or parses with
 106  // millisecond precision.
 107  //
 108  // Some valid layouts are invalid time values for time.Parse, due to formats
 109  // such as _ for space padding and Z for zone information.
 110  const (
 111  	Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
 112  	ANSIC       = "Mon Jan _2 15:04:05 2006"
 113  	UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
 114  	RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
 115  	RFC822      = "02 Jan 06 15:04 MST"
 116  	RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
 117  	RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
 118  	RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
 119  	RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
 120  	RFC3339     = "2006-01-02T15:04:05Z07:00"
 121  	RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
 122  	Kitchen     = "3:04PM"
 123  	// Handy time stamps.
 124  	Stamp      = "Jan _2 15:04:05"
 125  	StampMilli = "Jan _2 15:04:05.000"
 126  	StampMicro = "Jan _2 15:04:05.000000"
 127  	StampNano  = "Jan _2 15:04:05.000000000"
 128  	DateTime   = "2006-01-02 15:04:05"
 129  	DateOnly   = "2006-01-02"
 130  	TimeOnly   = "15:04:05"
 131  )
 132  
 133  const (
 134  	_                        = iota
 135  	stdLongMonth             = iota + stdNeedDate  // "January"
 136  	stdMonth                                       // "Jan"
 137  	stdNumMonth                                    // "1"
 138  	stdZeroMonth                                   // "01"
 139  	stdLongWeekDay                                 // "Monday"
 140  	stdWeekDay                                     // "Mon"
 141  	stdDay                                         // "2"
 142  	stdUnderDay                                    // "_2"
 143  	stdZeroDay                                     // "02"
 144  	stdUnderYearDay          = iota + stdNeedYday  // "__2"
 145  	stdZeroYearDay                                 // "002"
 146  	stdHour                  = iota + stdNeedClock // "15"
 147  	stdHour12                                      // "3"
 148  	stdZeroHour12                                  // "03"
 149  	stdMinute                                      // "4"
 150  	stdZeroMinute                                  // "04"
 151  	stdSecond                                      // "5"
 152  	stdZeroSecond                                  // "05"
 153  	stdLongYear              = iota + stdNeedDate  // "2006"
 154  	stdYear                                        // "06"
 155  	stdPM                    = iota + stdNeedClock // "PM"
 156  	stdpm                                          // "pm"
 157  	stdTZ                    = iota                // "MST"
 158  	stdISO8601TZ                                   // "Z0700"  // prints Z for UTC
 159  	stdISO8601SecondsTZ                            // "Z070000"
 160  	stdISO8601ShortTZ                              // "Z07"
 161  	stdISO8601ColonTZ                              // "Z07:00" // prints Z for UTC
 162  	stdISO8601ColonSecondsTZ                       // "Z07:00:00"
 163  	stdNumTZ                                       // "-0700"  // always numeric
 164  	stdNumSecondsTz                                // "-070000"
 165  	stdNumShortTZ                                  // "-07"    // always numeric
 166  	stdNumColonTZ                                  // "-07:00" // always numeric
 167  	stdNumColonSecondsTZ                           // "-07:00:00"
 168  	stdFracSecond0                                 // ".0", ".00", ... , trailing zeros included
 169  	stdFracSecond9                                 // ".9", ".99", ..., trailing zeros omitted
 170  
 171  	stdNeedDate       = 1 << 8             // need month, day, year
 172  	stdNeedYday       = 1 << 9             // need yday
 173  	stdNeedClock      = 1 << 10            // need hour, minute, second
 174  	stdArgShift       = 16                 // extra argument in high bits, above low stdArgShift
 175  	stdSeparatorShift = 28                 // extra argument in high 4 bits for fractional second separators
 176  	stdMask           = 1<<stdArgShift - 1 // mask out argument
 177  )
 178  
 179  // std0x records the std values for "01", "02", ..., "06".
 180  var std0x = [...]int{stdZeroMonth, stdZeroDay, stdZeroHour12, stdZeroMinute, stdZeroSecond, stdYear}
 181  
 182  // startsWithLowerCase reports whether the string has a lower-case letter at the beginning.
 183  // Its purpose is to prevent matching strings like "Month" when looking for "Mon".
 184  func startsWithLowerCase(str []byte) bool {
 185  	if len(str) == 0 {
 186  		return false
 187  	}
 188  	c := str[0]
 189  	return 'a' <= c && c <= 'z'
 190  }
 191  
 192  // nextStdChunk finds the first occurrence of a std string in
 193  // layout and returns the text before, the std string, and the text after.
 194  //
 195  // nextStdChunk should be an internal detail,
 196  // but widely used packages access it using linkname.
 197  // Notable members of the hall of shame include:
 198  //   - github.com/searKing/golang/go
 199  //
 200  // Do not remove or change the type signature.
 201  // See go.dev/issue/67401.
 202  //
 203  //go:linkname nextStdChunk
 204  func nextStdChunk(layout []byte) (prefix []byte, std int, suffix []byte) {
 205  	for i := 0; i < len(layout); i++ {
 206  		switch c := int(layout[i]); c {
 207  		case 'J': // January, Jan
 208  			if len(layout) >= i+3 && layout[i:i+3] == "Jan" {
 209  				if len(layout) >= i+7 && layout[i:i+7] == "January" {
 210  					return layout[0:i], stdLongMonth, layout[i+7:]
 211  				}
 212  				if !startsWithLowerCase(layout[i+3:]) {
 213  					return layout[0:i], stdMonth, layout[i+3:]
 214  				}
 215  			}
 216  
 217  		case 'M': // Monday, Mon, MST
 218  			if len(layout) >= i+3 {
 219  				if layout[i:i+3] == "Mon" {
 220  					if len(layout) >= i+6 && layout[i:i+6] == "Monday" {
 221  						return layout[0:i], stdLongWeekDay, layout[i+6:]
 222  					}
 223  					if !startsWithLowerCase(layout[i+3:]) {
 224  						return layout[0:i], stdWeekDay, layout[i+3:]
 225  					}
 226  				}
 227  				if layout[i:i+3] == "MST" {
 228  					return layout[0:i], stdTZ, layout[i+3:]
 229  				}
 230  			}
 231  
 232  		case '0': // 01, 02, 03, 04, 05, 06, 002
 233  			if len(layout) >= i+2 && '1' <= layout[i+1] && layout[i+1] <= '6' {
 234  				return layout[0:i], std0x[layout[i+1]-'1'], layout[i+2:]
 235  			}
 236  			if len(layout) >= i+3 && layout[i+1] == '0' && layout[i+2] == '2' {
 237  				return layout[0:i], stdZeroYearDay, layout[i+3:]
 238  			}
 239  
 240  		case '1': // 15, 1
 241  			if len(layout) >= i+2 && layout[i+1] == '5' {
 242  				return layout[0:i], stdHour, layout[i+2:]
 243  			}
 244  			return layout[0:i], stdNumMonth, layout[i+1:]
 245  
 246  		case '2': // 2006, 2
 247  			if len(layout) >= i+4 && layout[i:i+4] == "2006" {
 248  				return layout[0:i], stdLongYear, layout[i+4:]
 249  			}
 250  			return layout[0:i], stdDay, layout[i+1:]
 251  
 252  		case '_': // _2, _2006, __2
 253  			if len(layout) >= i+2 && layout[i+1] == '2' {
 254  				// _2006 is really a literal _, followed by stdLongYear
 255  				if len(layout) >= i+5 && layout[i+1:i+5] == "2006" {
 256  					return layout[0 : i+1], stdLongYear, layout[i+5:]
 257  				}
 258  				return layout[0:i], stdUnderDay, layout[i+2:]
 259  			}
 260  			if len(layout) >= i+3 && layout[i+1] == '_' && layout[i+2] == '2' {
 261  				return layout[0:i], stdUnderYearDay, layout[i+3:]
 262  			}
 263  
 264  		case '3':
 265  			return layout[0:i], stdHour12, layout[i+1:]
 266  
 267  		case '4':
 268  			return layout[0:i], stdMinute, layout[i+1:]
 269  
 270  		case '5':
 271  			return layout[0:i], stdSecond, layout[i+1:]
 272  
 273  		case 'P': // PM
 274  			if len(layout) >= i+2 && layout[i+1] == 'M' {
 275  				return layout[0:i], stdPM, layout[i+2:]
 276  			}
 277  
 278  		case 'p': // pm
 279  			if len(layout) >= i+2 && layout[i+1] == 'm' {
 280  				return layout[0:i], stdpm, layout[i+2:]
 281  			}
 282  
 283  		case '-': // -070000, -07:00:00, -0700, -07:00, -07
 284  			if len(layout) >= i+7 && layout[i:i+7] == "-070000" {
 285  				return layout[0:i], stdNumSecondsTz, layout[i+7:]
 286  			}
 287  			if len(layout) >= i+9 && layout[i:i+9] == "-07:00:00" {
 288  				return layout[0:i], stdNumColonSecondsTZ, layout[i+9:]
 289  			}
 290  			if len(layout) >= i+5 && layout[i:i+5] == "-0700" {
 291  				return layout[0:i], stdNumTZ, layout[i+5:]
 292  			}
 293  			if len(layout) >= i+6 && layout[i:i+6] == "-07:00" {
 294  				return layout[0:i], stdNumColonTZ, layout[i+6:]
 295  			}
 296  			if len(layout) >= i+3 && layout[i:i+3] == "-07" {
 297  				return layout[0:i], stdNumShortTZ, layout[i+3:]
 298  			}
 299  
 300  		case 'Z': // Z070000, Z07:00:00, Z0700, Z07:00,
 301  			if len(layout) >= i+7 && layout[i:i+7] == "Z070000" {
 302  				return layout[0:i], stdISO8601SecondsTZ, layout[i+7:]
 303  			}
 304  			if len(layout) >= i+9 && layout[i:i+9] == "Z07:00:00" {
 305  				return layout[0:i], stdISO8601ColonSecondsTZ, layout[i+9:]
 306  			}
 307  			if len(layout) >= i+5 && layout[i:i+5] == "Z0700" {
 308  				return layout[0:i], stdISO8601TZ, layout[i+5:]
 309  			}
 310  			if len(layout) >= i+6 && layout[i:i+6] == "Z07:00" {
 311  				return layout[0:i], stdISO8601ColonTZ, layout[i+6:]
 312  			}
 313  			if len(layout) >= i+3 && layout[i:i+3] == "Z07" {
 314  				return layout[0:i], stdISO8601ShortTZ, layout[i+3:]
 315  			}
 316  
 317  		case '.', ',': // ,000, or .000, or ,999, or .999 - repeated digits for fractional seconds.
 318  			if i+1 < len(layout) && (layout[i+1] == '0' || layout[i+1] == '9') {
 319  				ch := layout[i+1]
 320  				j := i + 1
 321  				for j < len(layout) && layout[j] == ch {
 322  					j++
 323  				}
 324  				// String of digits must end here - only fractional second is all digits.
 325  				if !isDigit(layout, j) {
 326  					code := stdFracSecond0
 327  					if layout[i+1] == '9' {
 328  						code = stdFracSecond9
 329  					}
 330  					std := stdFracSecond(code, j-(i+1), c)
 331  					return layout[0:i], std, layout[j:]
 332  				}
 333  			}
 334  		}
 335  	}
 336  	return layout, 0, ""
 337  }
 338  
 339  var longDayNames = [][]byte{
 340  	"Sunday",
 341  	"Monday",
 342  	"Tuesday",
 343  	"Wednesday",
 344  	"Thursday",
 345  	"Friday",
 346  	"Saturday",
 347  }
 348  
 349  var shortDayNames = [][]byte{
 350  	"Sun",
 351  	"Mon",
 352  	"Tue",
 353  	"Wed",
 354  	"Thu",
 355  	"Fri",
 356  	"Sat",
 357  }
 358  
 359  var shortMonthNames = [][]byte{
 360  	"Jan",
 361  	"Feb",
 362  	"Mar",
 363  	"Apr",
 364  	"May",
 365  	"Jun",
 366  	"Jul",
 367  	"Aug",
 368  	"Sep",
 369  	"Oct",
 370  	"Nov",
 371  	"Dec",
 372  }
 373  
 374  var longMonthNames = [][]byte{
 375  	"January",
 376  	"February",
 377  	"March",
 378  	"April",
 379  	"May",
 380  	"June",
 381  	"July",
 382  	"August",
 383  	"September",
 384  	"October",
 385  	"November",
 386  	"December",
 387  }
 388  
 389  // match reports whether s1 and s2 match ignoring case.
 390  // It is assumed s1 and s2 are the same length.
 391  func match(s1, s2 []byte) bool {
 392  	for i := 0; i < len(s1); i++ {
 393  		c1 := s1[i]
 394  		c2 := s2[i]
 395  		if c1 != c2 {
 396  			// Switch to lower-case; 'a'-'A' is known to be a single bit.
 397  			c1 |= 'a' - 'A'
 398  			c2 |= 'a' - 'A'
 399  			if c1 != c2 || c1 < 'a' || c1 > 'z' {
 400  				return false
 401  			}
 402  		}
 403  	}
 404  	return true
 405  }
 406  
 407  func lookup(tab [][]byte, val []byte) (int, []byte, error) {
 408  	for i, v := range tab {
 409  		if len(val) >= len(v) && match(val[:len(v)], v) {
 410  			return i, val[len(v):], nil
 411  		}
 412  	}
 413  	return -1, val, errBad
 414  }
 415  
 416  // appendInt appends the decimal form of x to b and returns the result.
 417  // If the decimal form (excluding sign) is shorter than width, the result is padded with leading 0's.
 418  // Duplicates functionality in strconv, but avoids dependency.
 419  func appendInt(b []byte, x int, width int) []byte {
 420  	u := uint(x)
 421  	if x < 0 {
 422  		b = append(b, '-')
 423  		u = uint(-x)
 424  	}
 425  
 426  	// 2-digit and 4-digit fields are the most common in time formats.
 427  	utod := func(u uint) byte { return '0' + byte(u) }
 428  	switch {
 429  	case width == 2 && u < 1e2:
 430  		return append(b, utod(u/1e1), utod(u%1e1))
 431  	case width == 4 && u < 1e4:
 432  		return append(b, utod(u/1e3), utod(u/1e2%1e1), utod(u/1e1%1e1), utod(u%1e1))
 433  	}
 434  
 435  	// Compute the number of decimal digits.
 436  	var n int
 437  	if u == 0 {
 438  		n = 1
 439  	}
 440  	for u2 := u; u2 > 0; u2 /= 10 {
 441  		n++
 442  	}
 443  
 444  	// Add 0-padding.
 445  	for pad := width - n; pad > 0; pad-- {
 446  		b = append(b, '0')
 447  	}
 448  
 449  	// Ensure capacity.
 450  	if len(b)+n <= cap(b) {
 451  		b = b[:len(b)+n]
 452  	} else {
 453  		b = append(b, []byte{:n}...)
 454  	}
 455  
 456  	// Assemble decimal in reverse order.
 457  	i := len(b) - 1
 458  	for u >= 10 && i > 0 {
 459  		q := u / 10
 460  		b[i] = utod(u - q*10)
 461  		u = q
 462  		i--
 463  	}
 464  	b[i] = utod(u)
 465  	return b
 466  }
 467  
 468  // Never printed, just needs to be non-nil for return by atoi.
 469  var errAtoi = errors.New("time: invalid number")
 470  
 471  // Duplicates functionality in strconv, but avoids dependency.
 472  func atoi[bytes []byte](s bytes) (x int, err error) {
 473  	neg := false
 474  	if len(s) > 0 && (s[0] == '-' || s[0] == '+') {
 475  		neg = s[0] == '-'
 476  		s = s[1:]
 477  	}
 478  	q, rem, err := leadingInt(s)
 479  	x = int(q)
 480  	if err != nil || len(rem) > 0 {
 481  		return 0, errAtoi
 482  	}
 483  	if neg {
 484  		x = -x
 485  	}
 486  	return x, nil
 487  }
 488  
 489  // The "std" value passed to appendNano contains two packed fields: the number of
 490  // digits after the decimal and the separator character (period or comma).
 491  // These functions pack and unpack that variable.
 492  func stdFracSecond(code, n, c int) int {
 493  	// Use 0xfff to make the failure case even more absurd.
 494  	if c == '.' {
 495  		return code | ((n & 0xfff) << stdArgShift)
 496  	}
 497  	return code | ((n & 0xfff) << stdArgShift) | 1<<stdSeparatorShift
 498  }
 499  
 500  func digitsLen(std int) int {
 501  	return (std >> stdArgShift) & 0xfff
 502  }
 503  
 504  func separator(std int) byte {
 505  	if (std >> stdSeparatorShift) == 0 {
 506  		return '.'
 507  	}
 508  	return ','
 509  }
 510  
 511  // appendNano appends a fractional second, as nanoseconds, to b
 512  // and returns the result. The nanosec must be within [0, 999999999].
 513  func appendNano(b []byte, nanosec int, std int) []byte {
 514  	trim := std&stdMask == stdFracSecond9
 515  	n := digitsLen(std)
 516  	if trim && (n == 0 || nanosec == 0) {
 517  		return b
 518  	}
 519  	dot := separator(std)
 520  	b = append(b, dot)
 521  	b = appendInt(b, nanosec, 9)
 522  	if n < 9 {
 523  		b = b[:len(b)-9+n]
 524  	}
 525  	if trim {
 526  		for len(b) > 0 && b[len(b)-1] == '0' {
 527  			b = b[:len(b)-1]
 528  		}
 529  		if len(b) > 0 && b[len(b)-1] == dot {
 530  			b = b[:len(b)-1]
 531  		}
 532  	}
 533  	return b
 534  }
 535  
 536  // String returns the time formatted using the format string
 537  //
 538  //	"2006-01-02 15:04:05.999999999 -0700 MST"
 539  //
 540  // If the time has a monotonic clock reading, the returned string
 541  // includes a final field "m=±<value>", where value is the monotonic
 542  // clock reading formatted as a decimal number of seconds.
 543  //
 544  // The returned string is meant for debugging; for a stable serialized
 545  // representation, use t.MarshalText, t.MarshalBinary, or t.Format
 546  // with an explicit format string.
 547  func (t Time) String() string {
 548  	s := t.Format("2006-01-02 15:04:05.999999999 -0700 MST")
 549  
 550  	// Format monotonic clock reading as m=±ddd.nnnnnnnnn.
 551  	if t.wall&hasMonotonic != 0 {
 552  		m2 := uint64(t.ext)
 553  		sign := byte('+')
 554  		if t.ext < 0 {
 555  			sign = '-'
 556  			m2 = -m2
 557  		}
 558  		m1, m2 := m2/1e9, m2%1e9
 559  		m0, m1 := m1/1e9, m1%1e9
 560  		buf := []byte{:0:24}
 561  		buf = append(buf, " m="...)
 562  		buf = append(buf, sign)
 563  		wid := 0
 564  		if m0 != 0 {
 565  			buf = appendInt(buf, int(m0), 0)
 566  			wid = 9
 567  		}
 568  		buf = appendInt(buf, int(m1), wid)
 569  		buf = append(buf, '.')
 570  		buf = appendInt(buf, int(m2), 9)
 571  		s += string(buf)
 572  	}
 573  	return s
 574  }
 575  
 576  // GoString implements [fmt.GoStringer] and formats t to be printed in Go source
 577  // code.
 578  func (t Time) GoString() []byte {
 579  	abs := t.absSec()
 580  	year, month, day := abs.days().date()
 581  	hour, minute, second := abs.clock()
 582  
 583  	buf := []byte{:0:len("time.Date(9999, time.September, 31, 23, 59, 59, 999999999, time.Local)")}
 584  	buf = append(buf, "time.Date("...)
 585  	buf = appendInt(buf, year, 0)
 586  	if January <= month && month <= December {
 587  		buf = append(buf, ", time."...)
 588  		buf = append(buf, longMonthNames[month-1]...)
 589  	} else {
 590  		// It's difficult to construct a time.Time with a date outside the
 591  		// standard range but we might as well try to handle the case.
 592  		buf = appendInt(buf, int(month), 0)
 593  	}
 594  	buf = append(buf, ", "...)
 595  	buf = appendInt(buf, day, 0)
 596  	buf = append(buf, ", "...)
 597  	buf = appendInt(buf, hour, 0)
 598  	buf = append(buf, ", "...)
 599  	buf = appendInt(buf, minute, 0)
 600  	buf = append(buf, ", "...)
 601  	buf = appendInt(buf, second, 0)
 602  	buf = append(buf, ", "...)
 603  	buf = appendInt(buf, t.Nanosecond(), 0)
 604  	buf = append(buf, ", "...)
 605  	switch loc := t.Location(); loc {
 606  	case UTC, nil:
 607  		buf = append(buf, "time.UTC"...)
 608  	case Local:
 609  		buf = append(buf, "time.Local"...)
 610  	default:
 611  		// there are several options for how we could display this, none of
 612  		// which are great:
 613  		//
 614  		// - use Location(loc.name), which is not technically valid syntax
 615  		// - use LoadLocation(loc.name), which will cause a syntax error when
 616  		// embedded and also would require us to escape the string without
 617  		// importing fmt or strconv
 618  		// - try to use FixedZone, which would also require escaping the name
 619  		// and would represent e.g. "America/Los_Angeles" daylight saving time
 620  		// shifts inaccurately
 621  		// - use the pointer format, which is no worse than you'd get with the
 622  		// old fmt.Sprintf("%#v", t) format.
 623  		//
 624  		// Of these, Location(loc.name) is the least disruptive. This is an edge
 625  		// case we hope not to hit too often.
 626  		buf = append(buf, `time.Location(`...)
 627  		buf = append(buf, quote(loc.name)...)
 628  		buf = append(buf, ')')
 629  	}
 630  	buf = append(buf, ')')
 631  	return []byte(buf)
 632  }
 633  
 634  // Format returns a textual representation of the time value formatted according
 635  // to the layout defined by the argument. See the documentation for the
 636  // constant called [Layout] to see how to represent the layout format.
 637  //
 638  // The executable example for [Time.Format] demonstrates the working
 639  // of the layout string in detail and is a good reference.
 640  func (t Time) Format(layout []byte) []byte {
 641  	const bufSize = 64
 642  	var b []byte
 643  	max := len(layout) + 10
 644  	if max < bufSize {
 645  		var buf [bufSize]byte
 646  		b = buf[:0]
 647  	} else {
 648  		b = []byte{:0:max}
 649  	}
 650  	b = t.AppendFormat(b, layout)
 651  	return []byte(b)
 652  }
 653  
 654  // AppendFormat is like [Time.Format] but appends the textual
 655  // representation to b and returns the extended buffer.
 656  func (t Time) AppendFormat(b []byte, layout []byte) []byte {
 657  	// Optimize for RFC3339 as it accounts for over half of all representations.
 658  	switch layout {
 659  	case RFC3339:
 660  		return t.appendFormatRFC3339(b, false)
 661  	case RFC3339Nano:
 662  		return t.appendFormatRFC3339(b, true)
 663  	default:
 664  		return t.appendFormat(b, layout)
 665  	}
 666  }
 667  
 668  func (t Time) appendFormat(b []byte, layout []byte) []byte {
 669  	name, offset, abs := t.locabs()
 670  	days := abs.days()
 671  
 672  	var (
 673  		year  int = -1
 674  		month Month
 675  		day   int
 676  		yday  int = -1
 677  		hour  int = -1
 678  		min   int
 679  		sec   int
 680  	)
 681  
 682  	// Each iteration generates one std value.
 683  	for layout != "" {
 684  		prefix, std, suffix := nextStdChunk(layout)
 685  		if prefix != "" {
 686  			b = append(b, prefix...)
 687  		}
 688  		if std == 0 {
 689  			break
 690  		}
 691  		layout = suffix
 692  
 693  		// Compute year, month, day if needed.
 694  		if year < 0 && std&stdNeedDate != 0 {
 695  			year, month, day = days.date()
 696  		}
 697  		if yday < 0 && std&stdNeedYday != 0 {
 698  			_, yday = days.yearYday()
 699  		}
 700  
 701  		// Compute hour, minute, second if needed.
 702  		if hour < 0 && std&stdNeedClock != 0 {
 703  			hour, min, sec = abs.clock()
 704  		}
 705  
 706  		switch std & stdMask {
 707  		case stdYear:
 708  			y := year
 709  			if y < 0 {
 710  				y = -y
 711  			}
 712  			b = appendInt(b, y%100, 2)
 713  		case stdLongYear:
 714  			b = appendInt(b, year, 4)
 715  		case stdMonth:
 716  			b = append(b, month.String()[:3]...)
 717  		case stdLongMonth:
 718  			m := month.String()
 719  			b = append(b, m...)
 720  		case stdNumMonth:
 721  			b = appendInt(b, int(month), 0)
 722  		case stdZeroMonth:
 723  			b = appendInt(b, int(month), 2)
 724  		case stdWeekDay:
 725  			b = append(b, days.weekday().String()[:3]...)
 726  		case stdLongWeekDay:
 727  			s := days.weekday().String()
 728  			b = append(b, s...)
 729  		case stdDay:
 730  			b = appendInt(b, day, 0)
 731  		case stdUnderDay:
 732  			if day < 10 {
 733  				b = append(b, ' ')
 734  			}
 735  			b = appendInt(b, day, 0)
 736  		case stdZeroDay:
 737  			b = appendInt(b, day, 2)
 738  		case stdUnderYearDay:
 739  			if yday < 100 {
 740  				b = append(b, ' ')
 741  				if yday < 10 {
 742  					b = append(b, ' ')
 743  				}
 744  			}
 745  			b = appendInt(b, yday, 0)
 746  		case stdZeroYearDay:
 747  			b = appendInt(b, yday, 3)
 748  		case stdHour:
 749  			b = appendInt(b, hour, 2)
 750  		case stdHour12:
 751  			// Noon is 12PM, midnight is 12AM.
 752  			hr := hour % 12
 753  			if hr == 0 {
 754  				hr = 12
 755  			}
 756  			b = appendInt(b, hr, 0)
 757  		case stdZeroHour12:
 758  			// Noon is 12PM, midnight is 12AM.
 759  			hr := hour % 12
 760  			if hr == 0 {
 761  				hr = 12
 762  			}
 763  			b = appendInt(b, hr, 2)
 764  		case stdMinute:
 765  			b = appendInt(b, min, 0)
 766  		case stdZeroMinute:
 767  			b = appendInt(b, min, 2)
 768  		case stdSecond:
 769  			b = appendInt(b, sec, 0)
 770  		case stdZeroSecond:
 771  			b = appendInt(b, sec, 2)
 772  		case stdPM:
 773  			if hour >= 12 {
 774  				b = append(b, "PM"...)
 775  			} else {
 776  				b = append(b, "AM"...)
 777  			}
 778  		case stdpm:
 779  			if hour >= 12 {
 780  				b = append(b, "pm"...)
 781  			} else {
 782  				b = append(b, "am"...)
 783  			}
 784  		case stdISO8601TZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ShortTZ, stdISO8601ColonSecondsTZ, stdNumTZ, stdNumColonTZ, stdNumSecondsTz, stdNumShortTZ, stdNumColonSecondsTZ:
 785  			// Ugly special case. We cheat and take the "Z" variants
 786  			// to mean "the time zone as formatted for ISO 8601".
 787  			if offset == 0 && (std == stdISO8601TZ || std == stdISO8601ColonTZ || std == stdISO8601SecondsTZ || std == stdISO8601ShortTZ || std == stdISO8601ColonSecondsTZ) {
 788  				b = append(b, 'Z')
 789  				break
 790  			}
 791  			zone := offset / 60 // convert to minutes
 792  			absoffset := offset
 793  			if zone < 0 {
 794  				b = append(b, '-')
 795  				zone = -zone
 796  				absoffset = -absoffset
 797  			} else {
 798  				b = append(b, '+')
 799  			}
 800  			b = appendInt(b, zone/60, 2)
 801  			if std == stdISO8601ColonTZ || std == stdNumColonTZ || std == stdISO8601ColonSecondsTZ || std == stdNumColonSecondsTZ {
 802  				b = append(b, ':')
 803  			}
 804  			if std != stdNumShortTZ && std != stdISO8601ShortTZ {
 805  				b = appendInt(b, zone%60, 2)
 806  			}
 807  
 808  			// append seconds if appropriate
 809  			if std == stdISO8601SecondsTZ || std == stdNumSecondsTz || std == stdNumColonSecondsTZ || std == stdISO8601ColonSecondsTZ {
 810  				if std == stdNumColonSecondsTZ || std == stdISO8601ColonSecondsTZ {
 811  					b = append(b, ':')
 812  				}
 813  				b = appendInt(b, absoffset%60, 2)
 814  			}
 815  
 816  		case stdTZ:
 817  			if name != "" {
 818  				b = append(b, name...)
 819  				break
 820  			}
 821  			// No time zone known for this time, but we must print one.
 822  			// Use the -0700 format.
 823  			zone := offset / 60 // convert to minutes
 824  			if zone < 0 {
 825  				b = append(b, '-')
 826  				zone = -zone
 827  			} else {
 828  				b = append(b, '+')
 829  			}
 830  			b = appendInt(b, zone/60, 2)
 831  			b = appendInt(b, zone%60, 2)
 832  		case stdFracSecond0, stdFracSecond9:
 833  			b = appendNano(b, t.Nanosecond(), std)
 834  		}
 835  	}
 836  	return b
 837  }
 838  
 839  var errBad = errors.New("bad value for field") // placeholder not passed to user
 840  
 841  // ParseError describes a problem parsing a time string.
 842  type ParseError struct {
 843  	Layout     []byte
 844  	Value      []byte
 845  	LayoutElem []byte
 846  	ValueElem  []byte
 847  	Message    []byte
 848  }
 849  
 850  // newParseError creates a new ParseError.
 851  // The provided value and valueElem are cloned to avoid escaping their values.
 852  func newParseError(layout, value, layoutElem, valueElem, message []byte) *ParseError {
 853  	valueCopy := stringslite.Clone(value)
 854  	valueElemCopy := stringslite.Clone(valueElem)
 855  	return &ParseError{layout, valueCopy, layoutElem, valueElemCopy, message}
 856  }
 857  
 858  // These are borrowed from unicode/utf8 and strconv and replicate behavior in
 859  // that package, since we can't take a dependency on either.
 860  const (
 861  	lowerhex  = "0123456789abcdef"
 862  	runeSelf  = 0x80
 863  	runeError = '\uFFFD'
 864  )
 865  
 866  func quote(s []byte) []byte {
 867  	buf := []byte{:1:len(s)+2} // slice will be at least len(s) + quotes
 868  	buf[0] = '"'
 869  	for i := 0; i < len(s); {
 870  		c := rune(s[i])
 871  		width := 1
 872  		if c >= runeSelf {
 873  			c, width = utf8.DecodeRune(s[i:])
 874  		}
 875  		if c >= runeSelf || c < ' ' {
 876  			if c == runeError {
 877  				width = 1
 878  				if i+2 < len(s) && s[i] == 0xEF && s[i+1] == 0xBF && s[i+2] == 0xBD {
 879  					width = 3
 880  				}
 881  			}
 882  			for j := 0; j < width; j++ {
 883  				buf = append(buf, `\x`...)
 884  				buf = append(buf, lowerhex[s[i+j]>>4])
 885  				buf = append(buf, lowerhex[s[i+j]&0xF])
 886  			}
 887  		} else {
 888  			if c == '"' || c == '\\' {
 889  				buf = append(buf, '\\')
 890  			}
 891  			buf = append(buf, byte(c))
 892  		}
 893  		i += width
 894  	}
 895  	buf = append(buf, '"')
 896  	return []byte(buf)
 897  }
 898  
 899  // Error returns the string representation of a ParseError.
 900  func (e *ParseError) Error() string {
 901  	if e.Message == "" {
 902  		return "parsing time " |
 903  			quote(e.Value) | " as " |
 904  			quote(e.Layout) | ": cannot parse " |
 905  			quote(e.ValueElem) | " as " |
 906  			quote(e.LayoutElem)
 907  	}
 908  	return "parsing time " |
 909  		quote(e.Value) | e.Message
 910  }
 911  
 912  // isDigit reports whether s[i] is in range and is a decimal digit.
 913  func isDigit[bytes []byte](s bytes, i int) bool {
 914  	if len(s) <= i {
 915  		return false
 916  	}
 917  	c := s[i]
 918  	return '0' <= c && c <= '9'
 919  }
 920  
 921  // getnum parses s[0:1] or s[0:2] (fixed forces s[0:2])
 922  // as a decimal integer and returns the integer and the
 923  // remainder of the string.
 924  func getnum(s []byte, fixed bool) (int, []byte, error) {
 925  	if !isDigit(s, 0) {
 926  		return 0, s, errBad
 927  	}
 928  	if !isDigit(s, 1) {
 929  		if fixed {
 930  			return 0, s, errBad
 931  		}
 932  		return int(s[0] - '0'), s[1:], nil
 933  	}
 934  	return int(s[0]-'0')*10 + int(s[1]-'0'), s[2:], nil
 935  }
 936  
 937  // getnum3 parses s[0:1], s[0:2], or s[0:3] (fixed forces s[0:3])
 938  // as a decimal integer and returns the integer and the remainder
 939  // of the string.
 940  func getnum3(s []byte, fixed bool) (int, []byte, error) {
 941  	var n, i int
 942  	for i = 0; i < 3 && isDigit(s, i); i++ {
 943  		n = n*10 + int(s[i]-'0')
 944  	}
 945  	if i == 0 || fixed && i != 3 {
 946  		return 0, s, errBad
 947  	}
 948  	return n, s[i:], nil
 949  }
 950  
 951  func cutspace(s []byte) []byte {
 952  	for len(s) > 0 && s[0] == ' ' {
 953  		s = s[1:]
 954  	}
 955  	return s
 956  }
 957  
 958  // skip removes the given prefix from value,
 959  // treating runs of space characters as equivalent.
 960  func skip(value, prefix []byte) ([]byte, error) {
 961  	for len(prefix) > 0 {
 962  		if prefix[0] == ' ' {
 963  			if len(value) > 0 && value[0] != ' ' {
 964  				return value, errBad
 965  			}
 966  			prefix = cutspace(prefix)
 967  			value = cutspace(value)
 968  			continue
 969  		}
 970  		if len(value) == 0 || value[0] != prefix[0] {
 971  			return value, errBad
 972  		}
 973  		prefix = prefix[1:]
 974  		value = value[1:]
 975  	}
 976  	return value, nil
 977  }
 978  
 979  // Parse parses a formatted string and returns the time value it represents.
 980  // See the documentation for the constant called [Layout] to see how to
 981  // represent the format. The second argument must be parseable using
 982  // the format string (layout) provided as the first argument.
 983  //
 984  // The example for [Time.Format] demonstrates the working of the layout string
 985  // in detail and is a good reference.
 986  //
 987  // When parsing (only), the input may contain a fractional second
 988  // field immediately after the seconds field, even if the layout does not
 989  // signify its presence. In that case either a comma or a decimal point
 990  // followed by a maximal series of digits is parsed as a fractional second.
 991  // Fractional seconds are truncated to nanosecond precision.
 992  //
 993  // Elements omitted from the layout are assumed to be zero or, when
 994  // zero is impossible, one, so parsing "3:04pm" returns the time
 995  // corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is
 996  // 0, this time is before the zero Time).
 997  // Years must be in the range 0000..9999. The day of the week is checked
 998  // for syntax but it is otherwise ignored.
 999  //
1000  // For layouts specifying the two-digit year 06, a value NN >= 69 will be treated
1001  // as 19NN and a value NN < 69 will be treated as 20NN.
1002  //
1003  // The remainder of this comment describes the handling of time zones.
1004  //
1005  // In the absence of a time zone indicator, Parse returns a time in UTC.
1006  //
1007  // When parsing a time with a zone offset like -0700, if the offset corresponds
1008  // to a time zone used by the current location ([Local]), then Parse uses that
1009  // location and zone in the returned time. Otherwise it records the time as
1010  // being in a fabricated location with time fixed at the given zone offset.
1011  //
1012  // When parsing a time with a zone abbreviation like MST, if the zone abbreviation
1013  // has a defined offset in the current location, then that offset is used.
1014  // The zone abbreviation "UTC" is recognized as UTC regardless of location.
1015  // If the zone abbreviation is unknown, Parse records the time as being
1016  // in a fabricated location with the given zone abbreviation and a zero offset.
1017  // This choice means that such a time can be parsed and reformatted with the
1018  // same layout losslessly, but the exact instant used in the representation will
1019  // differ by the actual zone offset. To avoid such problems, prefer time layouts
1020  // that use a numeric zone offset, or use [ParseInLocation].
1021  func Parse(layout, value []byte) (Time, error) {
1022  	// Optimize for RFC3339 as it accounts for over half of all representations.
1023  	if layout == RFC3339 || layout == RFC3339Nano {
1024  		if t, ok := parseRFC3339(value, Local); ok {
1025  			return t, nil
1026  		}
1027  	}
1028  	return parse(layout, value, UTC, Local)
1029  }
1030  
1031  // ParseInLocation is like Parse but differs in two important ways.
1032  // First, in the absence of time zone information, Parse interprets a time as UTC;
1033  // ParseInLocation interprets the time as in the given location.
1034  // Second, when given a zone offset or abbreviation, Parse tries to match it
1035  // against the Local location; ParseInLocation uses the given location.
1036  func ParseInLocation(layout, value []byte, loc *Location) (Time, error) {
1037  	// Optimize for RFC3339 as it accounts for over half of all representations.
1038  	if layout == RFC3339 || layout == RFC3339Nano {
1039  		if t, ok := parseRFC3339(value, loc); ok {
1040  			return t, nil
1041  		}
1042  	}
1043  	return parse(layout, value, loc, loc)
1044  }
1045  
1046  func parse(layout, value []byte, defaultLocation, local *Location) (Time, error) {
1047  	alayout, avalue := layout, value
1048  	rangeErrString := "" // set if a value is out of range
1049  	amSet := false       // do we need to subtract 12 from the hour for midnight?
1050  	pmSet := false       // do we need to add 12 to the hour?
1051  
1052  	// Time being constructed.
1053  	var (
1054  		year       int
1055  		month      int = -1
1056  		day        int = -1
1057  		yday       int = -1
1058  		hour       int
1059  		min        int
1060  		sec        int
1061  		nsec       int
1062  		z          *Location
1063  		zoneOffset int = -1
1064  		zoneName   []byte
1065  	)
1066  
1067  	// Each iteration processes one std value.
1068  	for {
1069  		var err error
1070  		prefix, std, suffix := nextStdChunk(layout)
1071  		stdstr := layout[len(prefix) : len(layout)-len(suffix)]
1072  		value, err = skip(value, prefix)
1073  		if err != nil {
1074  			return Time{}, newParseError(alayout, avalue, prefix, value, "")
1075  		}
1076  		if std == 0 {
1077  			if len(value) != 0 {
1078  				return Time{}, newParseError(alayout, avalue, "", value, ": extra text: "+quote(value))
1079  			}
1080  			break
1081  		}
1082  		layout = suffix
1083  		var p []byte
1084  		hold := value
1085  		switch std & stdMask {
1086  		case stdYear:
1087  			if len(value) < 2 {
1088  				err = errBad
1089  				break
1090  			}
1091  			p, value = value[0:2], value[2:]
1092  			year, err = atoi(p)
1093  			if err != nil {
1094  				break
1095  			}
1096  			if year >= 69 { // Unix time starts Dec 31 1969 in some time zones
1097  				year += 1900
1098  			} else {
1099  				year += 2000
1100  			}
1101  		case stdLongYear:
1102  			if len(value) < 4 || !isDigit(value, 0) {
1103  				err = errBad
1104  				break
1105  			}
1106  			p, value = value[0:4], value[4:]
1107  			year, err = atoi(p)
1108  		case stdMonth:
1109  			month, value, err = lookup(shortMonthNames, value)
1110  			month++
1111  		case stdLongMonth:
1112  			month, value, err = lookup(longMonthNames, value)
1113  			month++
1114  		case stdNumMonth, stdZeroMonth:
1115  			month, value, err = getnum(value, std == stdZeroMonth)
1116  			if err == nil && (month <= 0 || 12 < month) {
1117  				rangeErrString = "month"
1118  			}
1119  		case stdWeekDay:
1120  			// Ignore weekday except for error checking.
1121  			_, value, err = lookup(shortDayNames, value)
1122  		case stdLongWeekDay:
1123  			_, value, err = lookup(longDayNames, value)
1124  		case stdDay, stdUnderDay, stdZeroDay:
1125  			if std == stdUnderDay && len(value) > 0 && value[0] == ' ' {
1126  				value = value[1:]
1127  			}
1128  			day, value, err = getnum(value, std == stdZeroDay)
1129  			// Note that we allow any one- or two-digit day here.
1130  			// The month, day, year combination is validated after we've completed parsing.
1131  		case stdUnderYearDay, stdZeroYearDay:
1132  			for i := 0; i < 2; i++ {
1133  				if std == stdUnderYearDay && len(value) > 0 && value[0] == ' ' {
1134  					value = value[1:]
1135  				}
1136  			}
1137  			yday, value, err = getnum3(value, std == stdZeroYearDay)
1138  			// Note that we allow any one-, two-, or three-digit year-day here.
1139  			// The year-day, year combination is validated after we've completed parsing.
1140  		case stdHour:
1141  			hour, value, err = getnum(value, false)
1142  			if hour < 0 || 24 <= hour {
1143  				rangeErrString = "hour"
1144  			}
1145  		case stdHour12, stdZeroHour12:
1146  			hour, value, err = getnum(value, std == stdZeroHour12)
1147  			if hour < 0 || 12 < hour {
1148  				rangeErrString = "hour"
1149  			}
1150  		case stdMinute, stdZeroMinute:
1151  			min, value, err = getnum(value, std == stdZeroMinute)
1152  			if min < 0 || 60 <= min {
1153  				rangeErrString = "minute"
1154  			}
1155  		case stdSecond, stdZeroSecond:
1156  			sec, value, err = getnum(value, std == stdZeroSecond)
1157  			if err != nil {
1158  				break
1159  			}
1160  			if sec < 0 || 60 <= sec {
1161  				rangeErrString = "second"
1162  				break
1163  			}
1164  			// Special case: do we have a fractional second but no
1165  			// fractional second in the format?
1166  			if len(value) >= 2 && commaOrPeriod(value[0]) && isDigit(value, 1) {
1167  				_, std, _ = nextStdChunk(layout)
1168  				std &= stdMask
1169  				if std == stdFracSecond0 || std == stdFracSecond9 {
1170  					// Fractional second in the layout; proceed normally
1171  					break
1172  				}
1173  				// No fractional second in the layout but we have one in the input.
1174  				n := 2
1175  				for ; n < len(value) && isDigit(value, n); n++ {
1176  				}
1177  				nsec, rangeErrString, err = parseNanoseconds(value, n)
1178  				value = value[n:]
1179  			}
1180  		case stdPM:
1181  			if len(value) < 2 {
1182  				err = errBad
1183  				break
1184  			}
1185  			p, value = value[0:2], value[2:]
1186  			switch p {
1187  			case "PM":
1188  				pmSet = true
1189  			case "AM":
1190  				amSet = true
1191  			default:
1192  				err = errBad
1193  			}
1194  		case stdpm:
1195  			if len(value) < 2 {
1196  				err = errBad
1197  				break
1198  			}
1199  			p, value = value[0:2], value[2:]
1200  			switch p {
1201  			case "pm":
1202  				pmSet = true
1203  			case "am":
1204  				amSet = true
1205  			default:
1206  				err = errBad
1207  			}
1208  		case stdISO8601TZ, stdISO8601ShortTZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ColonSecondsTZ,
1209  			stdNumTZ, stdNumShortTZ, stdNumColonTZ, stdNumSecondsTz, stdNumColonSecondsTZ:
1210  			// ISO8601 variants accept 'Z' as UTC marker.
1211  			if (std == stdISO8601TZ || std == stdISO8601ShortTZ || std == stdISO8601ColonTZ ||
1212  				std == stdISO8601SecondsTZ || std == stdISO8601ColonSecondsTZ) &&
1213  				len(value) >= 1 && value[0] == 'Z' {
1214  				value = value[1:]
1215  				z = UTC
1216  				break
1217  			}
1218  			var sign, hour, min, seconds []byte
1219  			if std == stdISO8601ColonTZ || std == stdNumColonTZ {
1220  				if len(value) < 6 {
1221  					err = errBad
1222  					break
1223  				}
1224  				if value[3] != ':' {
1225  					err = errBad
1226  					break
1227  				}
1228  				sign, hour, min, seconds, value = value[0:1], value[1:3], value[4:6], "00", value[6:]
1229  			} else if std == stdNumShortTZ || std == stdISO8601ShortTZ {
1230  				if len(value) < 3 {
1231  					err = errBad
1232  					break
1233  				}
1234  				sign, hour, min, seconds, value = value[0:1], value[1:3], "00", "00", value[3:]
1235  			} else if std == stdISO8601ColonSecondsTZ || std == stdNumColonSecondsTZ {
1236  				if len(value) < 9 {
1237  					err = errBad
1238  					break
1239  				}
1240  				if value[3] != ':' || value[6] != ':' {
1241  					err = errBad
1242  					break
1243  				}
1244  				sign, hour, min, seconds, value = value[0:1], value[1:3], value[4:6], value[7:9], value[9:]
1245  			} else if std == stdISO8601SecondsTZ || std == stdNumSecondsTz {
1246  				if len(value) < 7 {
1247  					err = errBad
1248  					break
1249  				}
1250  				sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], value[5:7], value[7:]
1251  			} else {
1252  				if len(value) < 5 {
1253  					err = errBad
1254  					break
1255  				}
1256  				sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], "00", value[5:]
1257  			}
1258  			var hr, mm, ss int
1259  			hr, _, err = getnum(hour, true)
1260  			if err == nil {
1261  				mm, _, err = getnum(min, true)
1262  				if err == nil {
1263  					ss, _, err = getnum(seconds, true)
1264  				}
1265  			}
1266  
1267  			// The range test use > rather than >=,
1268  			// as some people do write offsets of 24 hours
1269  			// or 60 minutes or 60 seconds.
1270  			if hr > 24 {
1271  				rangeErrString = "time zone offset hour"
1272  			}
1273  			if mm > 60 {
1274  				rangeErrString = "time zone offset minute"
1275  			}
1276  			if ss > 60 {
1277  				rangeErrString = "time zone offset second"
1278  			}
1279  
1280  			zoneOffset = (hr*60+mm)*60 + ss // offset is in seconds
1281  			switch sign[0] {
1282  			case '+':
1283  			case '-':
1284  				zoneOffset = -zoneOffset
1285  			default:
1286  				err = errBad
1287  			}
1288  		case stdTZ:
1289  			// Does it look like a time zone?
1290  			if len(value) >= 3 && value[0:3] == "UTC" {
1291  				z = UTC
1292  				value = value[3:]
1293  				break
1294  			}
1295  			n, ok := parseTimeZone(value)
1296  			if !ok {
1297  				err = errBad
1298  				break
1299  			}
1300  			zoneName, value = value[:n], value[n:]
1301  
1302  		case stdFracSecond0:
1303  			// stdFracSecond0 requires the exact number of digits as specified in
1304  			// the layout.
1305  			ndigit := 1 + digitsLen(std)
1306  			if len(value) < ndigit {
1307  				err = errBad
1308  				break
1309  			}
1310  			nsec, rangeErrString, err = parseNanoseconds(value, ndigit)
1311  			value = value[ndigit:]
1312  
1313  		case stdFracSecond9:
1314  			if len(value) < 2 || !commaOrPeriod(value[0]) || value[1] < '0' || '9' < value[1] {
1315  				// Fractional second omitted.
1316  				break
1317  			}
1318  			// Take any number of digits, even more than asked for,
1319  			// because it is what the stdSecond case would do.
1320  			i := 0
1321  			for i+1 < len(value) && '0' <= value[i+1] && value[i+1] <= '9' {
1322  				i++
1323  			}
1324  			nsec, rangeErrString, err = parseNanoseconds(value, 1+i)
1325  			value = value[1+i:]
1326  		}
1327  		if rangeErrString != "" {
1328  			return Time{}, newParseError(alayout, avalue, stdstr, value, ": "|rangeErrString|" out of range")
1329  		}
1330  		if err != nil {
1331  			return Time{}, newParseError(alayout, avalue, stdstr, hold, "")
1332  		}
1333  	}
1334  	if pmSet && hour < 12 {
1335  		hour += 12
1336  	} else if amSet && hour == 12 {
1337  		hour = 0
1338  	}
1339  
1340  	// Convert yday to day, month.
1341  	if yday >= 0 {
1342  		var d int
1343  		var m int
1344  		if isLeap(year) {
1345  			if yday == 31+29 {
1346  				m = int(February)
1347  				d = 29
1348  			} else if yday > 31+29 {
1349  				yday--
1350  			}
1351  		}
1352  		if yday < 1 || yday > 365 {
1353  			return Time{}, newParseError(alayout, avalue, "", value, ": day-of-year out of range")
1354  		}
1355  		if m == 0 {
1356  			m = (yday-1)/31 + 1
1357  			if daysBefore(Month(m+1)) < yday {
1358  				m++
1359  			}
1360  			d = yday - daysBefore(Month(m))
1361  		}
1362  		// If month, day already seen, yday's m, d must match.
1363  		// Otherwise, set them from m, d.
1364  		if month >= 0 && month != m {
1365  			return Time{}, newParseError(alayout, avalue, "", value, ": day-of-year does not match month")
1366  		}
1367  		month = m
1368  		if day >= 0 && day != d {
1369  			return Time{}, newParseError(alayout, avalue, "", value, ": day-of-year does not match day")
1370  		}
1371  		day = d
1372  	} else {
1373  		if month < 0 {
1374  			month = int(January)
1375  		}
1376  		if day < 0 {
1377  			day = 1
1378  		}
1379  	}
1380  
1381  	// Validate the day of the month.
1382  	if day < 1 || day > daysIn(Month(month), year) {
1383  		return Time{}, newParseError(alayout, avalue, "", value, ": day out of range")
1384  	}
1385  
1386  	if z != nil {
1387  		return Date(year, Month(month), day, hour, min, sec, nsec, z), nil
1388  	}
1389  
1390  	if zoneOffset != -1 {
1391  		t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
1392  		t.addSec(-int64(zoneOffset))
1393  
1394  		// Look for local zone with the given offset.
1395  		// If that zone was in effect at the given time, use it.
1396  		name, offset, _, _, _ := local.lookup(t.unixSec())
1397  		if offset == zoneOffset && (zoneName == "" || name == zoneName) {
1398  			t.setLoc(local)
1399  			return t, nil
1400  		}
1401  
1402  		// Otherwise create fake zone to record offset.
1403  		zoneNameCopy := stringslite.Clone(zoneName) // avoid leaking the input value
1404  		t.setLoc(FixedZone(zoneNameCopy, zoneOffset))
1405  		return t, nil
1406  	}
1407  
1408  	if zoneName != "" {
1409  		t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
1410  		// Look for local zone with the given offset.
1411  		// If that zone was in effect at the given time, use it.
1412  		offset, ok := local.lookupName(zoneName, t.unixSec())
1413  		if ok {
1414  			t.addSec(-int64(offset))
1415  			t.setLoc(local)
1416  			return t, nil
1417  		}
1418  
1419  		// Otherwise, create fake zone with unknown offset.
1420  		if len(zoneName) > 3 && zoneName[:3] == "GMT" {
1421  			offset, _ = atoi(zoneName[3:]) // Guaranteed OK by parseGMT.
1422  			offset *= 3600
1423  		}
1424  		zoneNameCopy := stringslite.Clone(zoneName) // avoid leaking the input value
1425  		t.setLoc(FixedZone(zoneNameCopy, offset))
1426  		return t, nil
1427  	}
1428  
1429  	// Otherwise, fall back to default.
1430  	return Date(year, Month(month), day, hour, min, sec, nsec, defaultLocation), nil
1431  }
1432  
1433  // parseTimeZone parses a time zone string and returns its length. Time zones
1434  // are human-generated and unpredictable. We can't do precise error checking.
1435  // On the other hand, for a correct parse there must be a time zone at the
1436  // beginning of the string, so it's almost always true that there's one
1437  // there. We look at the beginning of the string for a run of upper-case letters.
1438  // If there are more than 5, it's an error.
1439  // If there are 4 or 5 and the last is a T, it's a time zone.
1440  // If there are 3, it's a time zone.
1441  // Otherwise, other than special cases, it's not a time zone.
1442  // GMT is special because it can have an hour offset.
1443  func parseTimeZone(value []byte) (length int, ok bool) {
1444  	if len(value) < 3 {
1445  		return 0, false
1446  	}
1447  	// Special case 1: ChST and MeST are the only zones with a lower-case letter.
1448  	if len(value) >= 4 && (value[:4] == "ChST" || value[:4] == "MeST") {
1449  		return 4, true
1450  	}
1451  	// Special case 2: GMT may have an hour offset; treat it specially.
1452  	if value[:3] == "GMT" {
1453  		length = parseGMT(value)
1454  		return length, true
1455  	}
1456  	// Special Case 3: Some time zones are not named, but have +/-00 format
1457  	if value[0] == '+' || value[0] == '-' {
1458  		length = parseSignedOffset(value)
1459  		ok := length > 0 // parseSignedOffset returns 0 in case of bad input
1460  		return length, ok
1461  	}
1462  	// How many upper-case letters are there? Need at least three, at most five.
1463  	var nUpper int
1464  	for nUpper = 0; nUpper < 6; nUpper++ {
1465  		if nUpper >= len(value) {
1466  			break
1467  		}
1468  		if c := value[nUpper]; c < 'A' || 'Z' < c {
1469  			break
1470  		}
1471  	}
1472  	switch nUpper {
1473  	case 0, 1, 2, 6:
1474  		return 0, false
1475  	case 5: // Must end in T to match.
1476  		if value[4] == 'T' {
1477  			return 5, true
1478  		}
1479  	case 4:
1480  		// Must end in T, except one special case.
1481  		if value[3] == 'T' || value[:4] == "WITA" {
1482  			return 4, true
1483  		}
1484  	case 3:
1485  		return 3, true
1486  	}
1487  	return 0, false
1488  }
1489  
1490  // parseGMT parses a GMT time zone. The input string is known to start "GMT".
1491  // The function checks whether that is followed by a sign and a number in the
1492  // range -23 through +23 excluding zero.
1493  func parseGMT(value []byte) int {
1494  	value = value[3:]
1495  	if len(value) == 0 {
1496  		return 3
1497  	}
1498  
1499  	return 3 + parseSignedOffset(value)
1500  }
1501  
1502  // parseSignedOffset parses a signed timezone offset (e.g. "+03" or "-04").
1503  // The function checks for a signed number in the range -23 through +23 excluding zero.
1504  // Returns length of the found offset string or 0 otherwise.
1505  func parseSignedOffset(value []byte) int {
1506  	sign := value[0]
1507  	if sign != '-' && sign != '+' {
1508  		return 0
1509  	}
1510  	x, rem, err := leadingInt(value[1:])
1511  
1512  	// fail if nothing consumed by leadingInt
1513  	if err != nil || value[1:] == rem {
1514  		return 0
1515  	}
1516  	if x > 23 {
1517  		return 0
1518  	}
1519  	return len(value) - len(rem)
1520  }
1521  
1522  func commaOrPeriod(b byte) bool {
1523  	return b == '.' || b == ','
1524  }
1525  
1526  func parseNanoseconds[bytes []byte](value bytes, nbytes int) (ns int, rangeErrString []byte, err error) {
1527  	if !commaOrPeriod(value[0]) {
1528  		err = errBad
1529  		return
1530  	}
1531  	if nbytes > 10 {
1532  		value = value[:10]
1533  		nbytes = 10
1534  	}
1535  	if ns, err = atoi(value[1:nbytes]); err != nil {
1536  		return
1537  	}
1538  	if ns < 0 {
1539  		rangeErrString = "fractional second"
1540  		return
1541  	}
1542  	// We need nanoseconds, which means scaling by the number
1543  	// of missing digits in the format, maximum length 10.
1544  	scaleDigits := 10 - nbytes
1545  	for i := 0; i < scaleDigits; i++ {
1546  		ns *= 10
1547  	}
1548  	return
1549  }
1550  
1551  var errLeadingInt = errors.New("time: bad [0-9]*") // never printed
1552  
1553  // leadingInt consumes the leading [0-9]* from s.
1554  func leadingInt[bytes []byte](s bytes) (x uint64, rem bytes, err error) {
1555  	i := 0
1556  	for ; i < len(s); i++ {
1557  		c := s[i]
1558  		if c < '0' || c > '9' {
1559  			break
1560  		}
1561  		if x > 1<<63/10 {
1562  			// overflow
1563  			return 0, rem, errLeadingInt
1564  		}
1565  		x = x*10 + uint64(c) - '0'
1566  		if x > 1<<63 {
1567  			// overflow
1568  			return 0, rem, errLeadingInt
1569  		}
1570  	}
1571  	return x, s[i:], nil
1572  }
1573  
1574  // leadingFraction consumes the leading [0-9]* from s.
1575  // It is used only for fractions, so does not return an error on overflow,
1576  // it just stops accumulating precision.
1577  func leadingFraction(s []byte) (x uint64, scale float64, rem []byte) {
1578  	i := 0
1579  	scale = 1
1580  	overflow := false
1581  	for ; i < len(s); i++ {
1582  		c := s[i]
1583  		if c < '0' || c > '9' {
1584  			break
1585  		}
1586  		if overflow {
1587  			continue
1588  		}
1589  		if x > (1<<63-1)/10 {
1590  			// It's possible for overflow to give a positive number, so take care.
1591  			overflow = true
1592  			continue
1593  		}
1594  		y := x*10 + uint64(c) - '0'
1595  		if y > 1<<63 {
1596  			overflow = true
1597  			continue
1598  		}
1599  		x = y
1600  		scale *= 10
1601  	}
1602  	return x, scale, s[i:]
1603  }
1604  
1605  var unitMap = map[string]uint64{
1606  	"ns": uint64(Nanosecond),
1607  	"us": uint64(Microsecond),
1608  	"µs": uint64(Microsecond), // U+00B5 = micro symbol
1609  	"μs": uint64(Microsecond), // U+03BC = Greek letter mu
1610  	"ms": uint64(Millisecond),
1611  	"s":  uint64(Second),
1612  	"m":  uint64(Minute),
1613  	"h":  uint64(Hour),
1614  }
1615  
1616  // ParseDuration parses a duration string.
1617  // A duration string is a possibly signed sequence of
1618  // decimal numbers, each with optional fraction and a unit suffix,
1619  // such as "300ms", "-1.5h" or "2h45m".
1620  // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
1621  func ParseDuration(s []byte) (Duration, error) {
1622  	// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
1623  	orig := s
1624  	var d uint64
1625  	neg := false
1626  
1627  	// Consume [-+]?
1628  	if s != "" {
1629  		c := s[0]
1630  		if c == '-' || c == '+' {
1631  			neg = c == '-'
1632  			s = s[1:]
1633  		}
1634  	}
1635  	// Special case: if all that is left is "0", this is zero.
1636  	if s == "0" {
1637  		return 0, nil
1638  	}
1639  	if s == "" {
1640  		return 0, errors.New("time: invalid duration " | quote(orig))
1641  	}
1642  	for s != "" {
1643  		var (
1644  			v, f  uint64      // integers before, after decimal point
1645  			scale float64 = 1 // value = v + f/scale
1646  		)
1647  
1648  		var err error
1649  
1650  		// The next character must be [0-9.]
1651  		if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') {
1652  			return 0, errors.New("time: invalid duration " | quote(orig))
1653  		}
1654  		// Consume [0-9]*
1655  		pl := len(s)
1656  		v, s, err = leadingInt(s)
1657  		if err != nil {
1658  			return 0, errors.New("time: invalid duration " | quote(orig))
1659  		}
1660  		pre := pl != len(s) // whether we consumed anything before a period
1661  
1662  		// Consume (\.[0-9]*)?
1663  		post := false
1664  		if s != "" && s[0] == '.' {
1665  			s = s[1:]
1666  			pl := len(s)
1667  			f, scale, s = leadingFraction(s)
1668  			post = pl != len(s)
1669  		}
1670  		if !pre && !post {
1671  			// no digits (e.g. ".s" or "-.s")
1672  			return 0, errors.New("time: invalid duration " | quote(orig))
1673  		}
1674  
1675  		// Consume unit.
1676  		i := 0
1677  		for ; i < len(s); i++ {
1678  			c := s[i]
1679  			if c == '.' || '0' <= c && c <= '9' {
1680  				break
1681  			}
1682  		}
1683  		if i == 0 {
1684  			return 0, errors.New("time: missing unit in duration " | quote(orig))
1685  		}
1686  		u := s[:i]
1687  		s = s[i:]
1688  		unit, ok := unitMap[u]
1689  		if !ok {
1690  			return 0, errors.New("time: unknown unit " | quote(u) | " in duration " | quote(orig))
1691  		}
1692  		if v > 1<<63/unit {
1693  			// overflow
1694  			return 0, errors.New("time: invalid duration " | quote(orig))
1695  		}
1696  		v *= unit
1697  		if f > 0 {
1698  			// float64 is needed to be nanosecond accurate for fractions of hours.
1699  			// v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit)
1700  			v += uint64(float64(f) * (float64(unit) / scale))
1701  			if v > 1<<63 {
1702  				// overflow
1703  				return 0, errors.New("time: invalid duration " | quote(orig))
1704  			}
1705  		}
1706  		d += v
1707  		if d > 1<<63 {
1708  			return 0, errors.New("time: invalid duration " | quote(orig))
1709  		}
1710  	}
1711  	if neg {
1712  		return -Duration(d), nil
1713  	}
1714  	if d > 1<<63-1 {
1715  		return 0, errors.New("time: invalid duration " | quote(orig))
1716  	}
1717  	return Duration(d), nil
1718  }
1719