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