2012-01-19 11:14:56 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2010-01-06 16:32:48 -07:00
|
|
|
package time
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
import "errors"
|
2010-01-06 16:32:48 -07:00
|
|
|
|
2017-10-28 16:03:15 -06:00
|
|
|
// These are predefined layouts for use in Time.Format and time.Parse.
|
2014-07-28 18:24:39 -06:00
|
|
|
// The reference time used in the layouts is the specific time:
|
2012-02-09 19:52:19 -07:00
|
|
|
// Mon Jan 2 15:04:05 MST 2006
|
2013-01-22 12:44:49 -07:00
|
|
|
// which is Unix time 1136239445. Since MST is GMT-0700,
|
2013-04-22 12:36:17 -06:00
|
|
|
// the reference time can be thought of as
|
2012-02-09 19:52:19 -07:00
|
|
|
// 01/02 03:04:05PM '06 -0700
|
2013-04-22 12:36:17 -06:00
|
|
|
// To define your own format, write down what the reference time would look
|
2012-02-09 19:52:19 -07:00
|
|
|
// like formatted your way; see the values of constants like ANSIC,
|
2013-04-22 12:36:17 -06:00
|
|
|
// StampMicro or Kitchen for examples. The model is to demonstrate what the
|
|
|
|
// reference time looks like so that the Format and Parse methods can apply
|
|
|
|
// the same transformation to a general time value.
|
2010-12-16 14:25:41 -07:00
|
|
|
//
|
2017-10-31 17:38:17 -06:00
|
|
|
// Some valid layouts are invalid time values for time.Parse, due to formats
|
2017-11-02 06:35:25 -06:00
|
|
|
// such as _ for space padding and Z for zone information.
|
2017-10-28 16:03:15 -06:00
|
|
|
//
|
2010-12-16 14:25:41 -07:00
|
|
|
// Within the format string, an underscore _ represents a space that may be
|
|
|
|
// replaced by a digit if the following number (a day) has two digits; for
|
|
|
|
// compatibility with fixed-width Unix time formats.
|
2010-04-27 01:05:24 -06:00
|
|
|
//
|
2011-08-10 07:24:45 -06:00
|
|
|
// A decimal point followed by one or more zeros represents a fractional
|
2016-03-01 16:21:55 -07:00
|
|
|
// second, printed to the given number of decimal places. A decimal point
|
2012-02-07 21:37:25 -07:00
|
|
|
// followed by one or more nines represents a fractional second, printed to
|
|
|
|
// the given number of decimal places, with trailing zeros removed.
|
|
|
|
// When parsing (only), the input may contain a fractional second
|
2011-08-10 07:24:45 -06:00
|
|
|
// field immediately after the seconds field, even if the layout does not
|
|
|
|
// signify its presence. In that case a decimal point followed by a maximal
|
|
|
|
// series of digits is parsed as a fractional second.
|
2011-08-07 15:50:17 -06:00
|
|
|
//
|
2010-04-27 01:05:24 -06:00
|
|
|
// Numeric time zone offsets format as follows:
|
|
|
|
// -0700 ±hhmm
|
|
|
|
// -07:00 ±hh:mm
|
2015-11-30 10:21:34 -07:00
|
|
|
// -07 ±hh
|
2010-04-27 01:05:24 -06:00
|
|
|
// Replacing the sign in the format with a Z triggers
|
|
|
|
// the ISO 8601 behavior of printing Z instead of an
|
2016-03-01 16:21:55 -07:00
|
|
|
// offset for the UTC zone. Thus:
|
2010-04-27 01:05:24 -06:00
|
|
|
// Z0700 Z or ±hhmm
|
|
|
|
// Z07:00 Z or ±hh:mm
|
2015-11-30 10:21:34 -07:00
|
|
|
// Z07 Z or ±hh
|
2015-07-14 23:02:38 -06:00
|
|
|
//
|
2016-11-02 15:19:42 -06:00
|
|
|
// The recognized day of week formats are "Mon" and "Monday".
|
|
|
|
// The recognized month formats are "Jan" and "January".
|
|
|
|
//
|
2018-07-09 20:52:22 -06:00
|
|
|
// The formats 2, _2, and 02 are unpadded, space-padded, and zero-padded
|
|
|
|
// day of month. The formats __2 and 002 are space-padded and zero-padded
|
|
|
|
// three-character day of year; there is no unpadded day of year format.
|
|
|
|
//
|
2016-11-02 15:19:42 -06:00
|
|
|
// Text in the format string that is not recognized as part of the reference
|
|
|
|
// time is echoed verbatim during Format and expected to appear verbatim
|
|
|
|
// in the input to Parse.
|
|
|
|
//
|
2017-10-28 16:03:15 -06:00
|
|
|
// The executable example for Time.Format demonstrates the working
|
2015-07-14 23:02:38 -06:00
|
|
|
// of the layout string in detail and is a good reference.
|
2016-01-06 09:23:03 -07:00
|
|
|
//
|
|
|
|
// Note that the RFC822, RFC850, and RFC1123 formats should be applied
|
|
|
|
// only to local times. Applying them to UTC times will use "UTC" as the
|
|
|
|
// time zone abbreviation, while strictly speaking those RFCs require the
|
|
|
|
// use of "GMT" in that case.
|
|
|
|
// In general RFC1123Z should be used instead of RFC1123 for servers
|
|
|
|
// that insist on that format, and RFC3339 should be preferred for new protocols.
|
2017-10-28 16:03:15 -06:00
|
|
|
// RFC3339, RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting;
|
2016-06-08 22:15:26 -06:00
|
|
|
// when used with time.Parse they do not accept all the time formats
|
|
|
|
// permitted by the RFCs.
|
2017-06-28 16:07:06 -06:00
|
|
|
// The RFC3339Nano format removes trailing zeros from the seconds field
|
|
|
|
// and thus may not sort correctly once formatted.
|
2010-01-06 16:32:48 -07:00
|
|
|
const (
|
2012-02-07 21:37:25 -07:00
|
|
|
ANSIC = "Mon Jan _2 15:04:05 2006"
|
|
|
|
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
|
|
|
|
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
|
2012-04-03 14:54:39 -06:00
|
|
|
RFC822 = "02 Jan 06 15:04 MST"
|
|
|
|
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
|
2012-02-07 21:37:25 -07:00
|
|
|
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
|
|
|
|
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
|
|
|
|
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
|
|
|
|
RFC3339 = "2006-01-02T15:04:05Z07:00"
|
|
|
|
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
|
|
|
|
Kitchen = "3:04PM"
|
2011-08-07 15:50:17 -06:00
|
|
|
// Handy time stamps.
|
|
|
|
Stamp = "Jan _2 15:04:05"
|
|
|
|
StampMilli = "Jan _2 15:04:05.000"
|
|
|
|
StampMicro = "Jan _2 15:04:05.000000"
|
|
|
|
StampNano = "Jan _2 15:04:05.000000000"
|
2010-01-06 16:32:48 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-07-31 00:11:02 -06:00
|
|
|
_ = iota
|
|
|
|
stdLongMonth = iota + stdNeedDate // "January"
|
|
|
|
stdMonth // "Jan"
|
|
|
|
stdNumMonth // "1"
|
|
|
|
stdZeroMonth // "01"
|
|
|
|
stdLongWeekDay // "Monday"
|
|
|
|
stdWeekDay // "Mon"
|
|
|
|
stdDay // "2"
|
|
|
|
stdUnderDay // "_2"
|
|
|
|
stdZeroDay // "02"
|
2018-07-09 20:52:22 -06:00
|
|
|
stdUnderYearDay // "__2"
|
|
|
|
stdZeroYearDay // "002"
|
2013-07-31 00:11:02 -06:00
|
|
|
stdHour = iota + stdNeedClock // "15"
|
|
|
|
stdHour12 // "3"
|
|
|
|
stdZeroHour12 // "03"
|
|
|
|
stdMinute // "4"
|
|
|
|
stdZeroMinute // "04"
|
|
|
|
stdSecond // "5"
|
|
|
|
stdZeroSecond // "05"
|
|
|
|
stdLongYear = iota + stdNeedDate // "2006"
|
|
|
|
stdYear // "06"
|
|
|
|
stdPM = iota + stdNeedClock // "PM"
|
|
|
|
stdpm // "pm"
|
|
|
|
stdTZ = iota // "MST"
|
|
|
|
stdISO8601TZ // "Z0700" // prints Z for UTC
|
|
|
|
stdISO8601SecondsTZ // "Z070000"
|
2015-11-30 10:21:34 -07:00
|
|
|
stdISO8601ShortTZ // "Z07"
|
2013-07-31 00:11:02 -06:00
|
|
|
stdISO8601ColonTZ // "Z07:00" // prints Z for UTC
|
|
|
|
stdISO8601ColonSecondsTZ // "Z07:00:00"
|
|
|
|
stdNumTZ // "-0700" // always numeric
|
|
|
|
stdNumSecondsTz // "-070000"
|
|
|
|
stdNumShortTZ // "-07" // always numeric
|
|
|
|
stdNumColonTZ // "-07:00" // always numeric
|
|
|
|
stdNumColonSecondsTZ // "-07:00:00"
|
|
|
|
stdFracSecond0 // ".0", ".00", ... , trailing zeros included
|
|
|
|
stdFracSecond9 // ".9", ".99", ..., trailing zeros omitted
|
2012-06-03 09:08:17 -06:00
|
|
|
|
|
|
|
stdNeedDate = 1 << 8 // need month, day, year
|
|
|
|
stdNeedClock = 2 << 8 // need hour, minute, second
|
|
|
|
stdArgShift = 16 // extra argument in high bits, above low stdArgShift
|
|
|
|
stdMask = 1<<stdArgShift - 1 // mask out argument
|
2010-01-06 16:32:48 -07:00
|
|
|
)
|
|
|
|
|
2012-06-03 09:08:17 -06:00
|
|
|
// std0x records the std values for "01", "02", ..., "06".
|
|
|
|
var std0x = [...]int{stdZeroMonth, stdZeroDay, stdZeroHour12, stdZeroMinute, stdZeroSecond, stdYear}
|
|
|
|
|
2014-04-16 17:57:25 -06:00
|
|
|
// startsWithLowerCase reports whether the string has a lower-case letter at the beginning.
|
2013-08-04 18:53:46 -06:00
|
|
|
// Its purpose is to prevent matching strings like "Month" when looking for "Mon".
|
|
|
|
func startsWithLowerCase(str string) bool {
|
|
|
|
if len(str) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c := str[0]
|
|
|
|
return 'a' <= c && c <= 'z'
|
|
|
|
}
|
|
|
|
|
2010-03-30 15:54:32 -06:00
|
|
|
// nextStdChunk finds the first occurrence of a std string in
|
|
|
|
// layout and returns the text before, the std string, and the text after.
|
2012-06-03 09:08:17 -06:00
|
|
|
func nextStdChunk(layout string) (prefix string, std int, suffix string) {
|
2010-03-30 15:54:32 -06:00
|
|
|
for i := 0; i < len(layout); i++ {
|
2012-06-03 09:08:17 -06:00
|
|
|
switch c := int(layout[i]); c {
|
2010-03-30 15:54:32 -06:00
|
|
|
case 'J': // January, Jan
|
2012-06-03 09:08:17 -06:00
|
|
|
if len(layout) >= i+3 && layout[i:i+3] == "Jan" {
|
|
|
|
if len(layout) >= i+7 && layout[i:i+7] == "January" {
|
|
|
|
return layout[0:i], stdLongMonth, layout[i+7:]
|
|
|
|
}
|
2013-08-04 18:53:46 -06:00
|
|
|
if !startsWithLowerCase(layout[i+3:]) {
|
|
|
|
return layout[0:i], stdMonth, layout[i+3:]
|
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'M': // Monday, Mon, MST
|
|
|
|
if len(layout) >= i+3 {
|
2012-06-03 09:08:17 -06:00
|
|
|
if layout[i:i+3] == "Mon" {
|
|
|
|
if len(layout) >= i+6 && layout[i:i+6] == "Monday" {
|
|
|
|
return layout[0:i], stdLongWeekDay, layout[i+6:]
|
|
|
|
}
|
2013-08-04 18:53:46 -06:00
|
|
|
if !startsWithLowerCase(layout[i+3:]) {
|
|
|
|
return layout[0:i], stdWeekDay, layout[i+3:]
|
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
if layout[i:i+3] == "MST" {
|
2010-03-30 15:54:32 -06:00
|
|
|
return layout[0:i], stdTZ, layout[i+3:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:52:22 -06:00
|
|
|
case '0': // 01, 02, 03, 04, 05, 06, 002
|
2010-03-30 15:54:32 -06:00
|
|
|
if len(layout) >= i+2 && '1' <= layout[i+1] && layout[i+1] <= '6' {
|
2012-06-03 09:08:17 -06:00
|
|
|
return layout[0:i], std0x[layout[i+1]-'1'], layout[i+2:]
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
2018-07-09 20:52:22 -06:00
|
|
|
if len(layout) >= i+3 && layout[i+1] == '0' && layout[i+2] == '2' {
|
|
|
|
return layout[0:i], stdZeroYearDay, layout[i+3:]
|
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
|
|
|
|
case '1': // 15, 1
|
|
|
|
if len(layout) >= i+2 && layout[i+1] == '5' {
|
|
|
|
return layout[0:i], stdHour, layout[i+2:]
|
|
|
|
}
|
|
|
|
return layout[0:i], stdNumMonth, layout[i+1:]
|
|
|
|
|
|
|
|
case '2': // 2006, 2
|
2012-06-03 09:08:17 -06:00
|
|
|
if len(layout) >= i+4 && layout[i:i+4] == "2006" {
|
2010-03-30 15:54:32 -06:00
|
|
|
return layout[0:i], stdLongYear, layout[i+4:]
|
|
|
|
}
|
|
|
|
return layout[0:i], stdDay, layout[i+1:]
|
|
|
|
|
2018-07-09 20:52:22 -06:00
|
|
|
case '_': // _2, _2006, __2
|
2018-01-03 11:24:29 -07:00
|
|
|
if len(layout) >= i+2 && layout[i+1] == '2' {
|
|
|
|
//_2006 is really a literal _, followed by stdLongYear
|
|
|
|
if len(layout) >= i+5 && layout[i+1:i+5] == "2006" {
|
|
|
|
return layout[0 : i+1], stdLongYear, layout[i+5:]
|
2015-10-25 15:04:48 -06:00
|
|
|
}
|
2018-01-03 11:24:29 -07:00
|
|
|
return layout[0:i], stdUnderDay, layout[i+2:]
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
2018-07-09 20:52:22 -06:00
|
|
|
if len(layout) >= i+3 && layout[i+1] == '_' && layout[i+2] == '2' {
|
|
|
|
return layout[0:i], stdUnderYearDay, layout[i+3:]
|
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
|
2012-06-03 09:08:17 -06:00
|
|
|
case '3':
|
|
|
|
return layout[0:i], stdHour12, layout[i+1:]
|
|
|
|
|
|
|
|
case '4':
|
|
|
|
return layout[0:i], stdMinute, layout[i+1:]
|
|
|
|
|
|
|
|
case '5':
|
|
|
|
return layout[0:i], stdSecond, layout[i+1:]
|
2010-03-30 15:54:32 -06:00
|
|
|
|
|
|
|
case 'P': // PM
|
|
|
|
if len(layout) >= i+2 && layout[i+1] == 'M' {
|
2012-06-03 09:08:17 -06:00
|
|
|
return layout[0:i], stdPM, layout[i+2:]
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'p': // pm
|
|
|
|
if len(layout) >= i+2 && layout[i+1] == 'm' {
|
2012-06-03 09:08:17 -06:00
|
|
|
return layout[0:i], stdpm, layout[i+2:]
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
|
|
|
|
2013-07-31 00:11:02 -06:00
|
|
|
case '-': // -070000, -07:00:00, -0700, -07:00, -07
|
|
|
|
if len(layout) >= i+7 && layout[i:i+7] == "-070000" {
|
|
|
|
return layout[0:i], stdNumSecondsTz, layout[i+7:]
|
|
|
|
}
|
|
|
|
if len(layout) >= i+9 && layout[i:i+9] == "-07:00:00" {
|
|
|
|
return layout[0:i], stdNumColonSecondsTZ, layout[i+9:]
|
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
if len(layout) >= i+5 && layout[i:i+5] == "-0700" {
|
|
|
|
return layout[0:i], stdNumTZ, layout[i+5:]
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
if len(layout) >= i+6 && layout[i:i+6] == "-07:00" {
|
|
|
|
return layout[0:i], stdNumColonTZ, layout[i+6:]
|
2010-04-27 01:05:24 -06:00
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
if len(layout) >= i+3 && layout[i:i+3] == "-07" {
|
|
|
|
return layout[0:i], stdNumShortTZ, layout[i+3:]
|
2010-08-24 15:41:26 -06:00
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
|
|
|
|
case 'Z': // Z070000, Z07:00:00, Z0700, Z07:00,
|
|
|
|
if len(layout) >= i+7 && layout[i:i+7] == "Z070000" {
|
|
|
|
return layout[0:i], stdISO8601SecondsTZ, layout[i+7:]
|
|
|
|
}
|
|
|
|
if len(layout) >= i+9 && layout[i:i+9] == "Z07:00:00" {
|
|
|
|
return layout[0:i], stdISO8601ColonSecondsTZ, layout[i+9:]
|
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
if len(layout) >= i+5 && layout[i:i+5] == "Z0700" {
|
|
|
|
return layout[0:i], stdISO8601TZ, layout[i+5:]
|
2010-04-27 01:05:24 -06:00
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
if len(layout) >= i+6 && layout[i:i+6] == "Z07:00" {
|
|
|
|
return layout[0:i], stdISO8601ColonTZ, layout[i+6:]
|
2010-04-27 01:05:24 -06:00
|
|
|
}
|
2015-11-30 10:21:34 -07:00
|
|
|
if len(layout) >= i+3 && layout[i:i+3] == "Z07" {
|
|
|
|
return layout[0:i], stdISO8601ShortTZ, layout[i+3:]
|
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
|
2012-02-07 21:37:25 -07:00
|
|
|
case '.': // .000 or .999 - repeated digits for fractional seconds.
|
|
|
|
if i+1 < len(layout) && (layout[i+1] == '0' || layout[i+1] == '9') {
|
|
|
|
ch := layout[i+1]
|
|
|
|
j := i + 1
|
|
|
|
for j < len(layout) && layout[j] == ch {
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
// String of digits must end here - only fractional second is all digits.
|
|
|
|
if !isDigit(layout, j) {
|
2012-06-03 09:08:17 -06:00
|
|
|
std := stdFracSecond0
|
|
|
|
if layout[i+1] == '9' {
|
|
|
|
std = stdFracSecond9
|
|
|
|
}
|
|
|
|
std |= (j - (i + 1)) << stdArgShift
|
|
|
|
return layout[0:i], std, layout[j:]
|
2012-02-07 21:37:25 -07:00
|
|
|
}
|
2011-08-07 15:50:17 -06:00
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
return layout, 0, ""
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
|
|
|
|
2010-01-06 16:32:48 -07:00
|
|
|
var longDayNames = []string{
|
|
|
|
"Sunday",
|
|
|
|
"Monday",
|
|
|
|
"Tuesday",
|
|
|
|
"Wednesday",
|
|
|
|
"Thursday",
|
|
|
|
"Friday",
|
|
|
|
"Saturday",
|
|
|
|
}
|
|
|
|
|
|
|
|
var shortDayNames = []string{
|
|
|
|
"Sun",
|
|
|
|
"Mon",
|
|
|
|
"Tue",
|
|
|
|
"Wed",
|
|
|
|
"Thu",
|
|
|
|
"Fri",
|
|
|
|
"Sat",
|
|
|
|
}
|
|
|
|
|
|
|
|
var shortMonthNames = []string{
|
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec",
|
|
|
|
}
|
|
|
|
|
|
|
|
var longMonthNames = []string{
|
|
|
|
"January",
|
|
|
|
"February",
|
|
|
|
"March",
|
|
|
|
"April",
|
|
|
|
"May",
|
|
|
|
"June",
|
|
|
|
"July",
|
|
|
|
"August",
|
|
|
|
"September",
|
|
|
|
"October",
|
|
|
|
"November",
|
|
|
|
"December",
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// match reports whether s1 and s2 match ignoring case.
|
2011-10-04 13:52:30 -06:00
|
|
|
// It is assumed s1 and s2 are the same length.
|
|
|
|
func match(s1, s2 string) bool {
|
|
|
|
for i := 0; i < len(s1); i++ {
|
|
|
|
c1 := s1[i]
|
|
|
|
c2 := s2[i]
|
|
|
|
if c1 != c2 {
|
|
|
|
// Switch to lower-case; 'a'-'A' is known to be a single bit.
|
|
|
|
c1 |= 'a' - 'A'
|
|
|
|
c2 |= 'a' - 'A'
|
|
|
|
if c1 != c2 || c1 < 'a' || c1 > 'z' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func lookup(tab []string, val string) (int, string, error) {
|
2010-01-07 18:59:20 -07:00
|
|
|
for i, v := range tab {
|
2011-10-04 13:52:30 -06:00
|
|
|
if len(val) >= len(v) && match(val[0:len(v)], v) {
|
2010-03-30 15:54:32 -06:00
|
|
|
return i, val[len(v):], nil
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
return -1, val, errBad
|
2010-01-06 16:32:48 -07:00
|
|
|
}
|
|
|
|
|
2015-01-11 03:29:00 -07:00
|
|
|
// appendInt appends the decimal form of x to b and returns the result.
|
|
|
|
// If the decimal form (excluding sign) is shorter than width, the result is padded with leading 0's.
|
2011-11-30 09:59:44 -07:00
|
|
|
// Duplicates functionality in strconv, but avoids dependency.
|
2015-01-11 03:29:00 -07:00
|
|
|
func appendInt(b []byte, x int, width int) []byte {
|
|
|
|
u := uint(x)
|
|
|
|
if x < 0 {
|
|
|
|
b = append(b, '-')
|
|
|
|
u = uint(-x)
|
2012-06-03 09:08:17 -06:00
|
|
|
}
|
|
|
|
|
2015-01-11 03:29:00 -07:00
|
|
|
// Assemble decimal in reverse order.
|
|
|
|
var buf [20]byte
|
|
|
|
i := len(buf)
|
|
|
|
for u >= 10 {
|
|
|
|
i--
|
|
|
|
q := u / 10
|
|
|
|
buf[i] = byte('0' + u - q*10)
|
|
|
|
u = q
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
i--
|
|
|
|
buf[i] = byte('0' + u)
|
|
|
|
|
|
|
|
// Add 0-padding.
|
|
|
|
for w := len(buf) - i; w < width; w++ {
|
|
|
|
b = append(b, '0')
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
|
|
|
|
return append(b, buf[i:]...)
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Never printed, just needs to be non-nil for return by atoi.
|
|
|
|
var atoiError = errors.New("time: invalid number")
|
|
|
|
|
|
|
|
// Duplicates functionality in strconv, but avoids dependency.
|
|
|
|
func atoi(s string) (x int, err error) {
|
2011-12-22 22:28:56 -07:00
|
|
|
neg := false
|
2013-08-14 18:10:49 -06:00
|
|
|
if s != "" && (s[0] == '-' || s[0] == '+') {
|
|
|
|
neg = s[0] == '-'
|
2011-12-22 22:28:56 -07:00
|
|
|
s = s[1:]
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
2012-10-14 14:50:13 -06:00
|
|
|
q, rem, err := leadingInt(s)
|
|
|
|
x = int(q)
|
2011-12-22 22:28:56 -07:00
|
|
|
if err != nil || rem != "" {
|
2011-11-30 09:59:44 -07:00
|
|
|
return 0, atoiError
|
|
|
|
}
|
2011-12-22 22:28:56 -07:00
|
|
|
if neg {
|
2011-11-30 09:59:44 -07:00
|
|
|
x = -x
|
|
|
|
}
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:08:17 -06:00
|
|
|
// formatNano appends a fractional second, as nanoseconds, to b
|
|
|
|
// and returns the result.
|
|
|
|
func formatNano(b []byte, nanosec uint, n int, trim bool) []byte {
|
|
|
|
u := nanosec
|
|
|
|
var buf [9]byte
|
|
|
|
for start := len(buf); start > 0; {
|
|
|
|
start--
|
|
|
|
buf[start] = byte(u%10 + '0')
|
|
|
|
u /= 10
|
2010-01-06 16:32:48 -07:00
|
|
|
}
|
2010-01-12 20:39:30 -07:00
|
|
|
|
2011-08-07 15:50:17 -06:00
|
|
|
if n > 9 {
|
|
|
|
n = 9
|
|
|
|
}
|
2012-02-07 21:37:25 -07:00
|
|
|
if trim {
|
2012-06-03 09:08:17 -06:00
|
|
|
for n > 0 && buf[n-1] == '0' {
|
2012-02-07 21:37:25 -07:00
|
|
|
n--
|
|
|
|
}
|
|
|
|
if n == 0 {
|
2012-06-03 09:08:17 -06:00
|
|
|
return b
|
2012-02-07 21:37:25 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, '.')
|
|
|
|
return append(b, buf[:n]...)
|
2011-08-07 15:50:17 -06:00
|
|
|
}
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
// String returns the time formatted using the format string
|
2012-02-14 09:17:48 -07:00
|
|
|
// "2006-01-02 15:04:05.999999999 -0700 MST"
|
2017-02-05 13:06:34 -07:00
|
|
|
//
|
2017-02-02 14:20:58 -07:00
|
|
|
// If the time has a monotonic clock reading, the returned string
|
2017-02-05 13:06:34 -07:00
|
|
|
// includes a final field "m=±<value>", where value is the monotonic
|
2017-02-02 14:20:58 -07:00
|
|
|
// clock reading formatted as a decimal number of seconds.
|
2017-07-05 21:39:58 -06:00
|
|
|
//
|
|
|
|
// The returned string is meant for debugging; for a stable serialized
|
|
|
|
// representation, use t.MarshalText, t.MarshalBinary, or t.Format
|
|
|
|
// with an explicit format string.
|
2011-11-30 09:59:44 -07:00
|
|
|
func (t Time) String() string {
|
2017-02-02 14:20:58 -07:00
|
|
|
s := t.Format("2006-01-02 15:04:05.999999999 -0700 MST")
|
|
|
|
|
|
|
|
// Format monotonic clock reading as m=±ddd.nnnnnnnnn.
|
|
|
|
if t.wall&hasMonotonic != 0 {
|
2017-02-08 14:05:25 -07:00
|
|
|
m2 := uint64(t.ext)
|
2017-02-02 14:20:58 -07:00
|
|
|
sign := byte('+')
|
2017-02-08 14:05:25 -07:00
|
|
|
if t.ext < 0 {
|
2017-02-02 14:20:58 -07:00
|
|
|
sign = '-'
|
2017-02-08 14:05:25 -07:00
|
|
|
m2 = -m2
|
2017-02-02 14:20:58 -07:00
|
|
|
}
|
2017-02-08 14:05:25 -07:00
|
|
|
m1, m2 := m2/1e9, m2%1e9
|
2017-02-02 14:20:58 -07:00
|
|
|
m0, m1 := m1/1e9, m1%1e9
|
|
|
|
var buf []byte
|
|
|
|
buf = append(buf, " m="...)
|
|
|
|
buf = append(buf, sign)
|
|
|
|
wid := 0
|
|
|
|
if m0 != 0 {
|
|
|
|
buf = appendInt(buf, int(m0), 0)
|
|
|
|
wid = 9
|
|
|
|
}
|
|
|
|
buf = appendInt(buf, int(m1), wid)
|
|
|
|
buf = append(buf, '.')
|
|
|
|
buf = appendInt(buf, int(m2), 9)
|
|
|
|
s += string(buf)
|
|
|
|
}
|
|
|
|
return s
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
|
2010-01-06 16:32:48 -07:00
|
|
|
// Format returns a textual representation of the time value formatted
|
2013-04-22 12:36:17 -06:00
|
|
|
// according to layout, which defines the format by showing how the reference
|
2014-07-28 18:24:39 -06:00
|
|
|
// time, defined to be
|
2012-02-09 19:52:19 -07:00
|
|
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
2013-04-22 12:36:17 -06:00
|
|
|
// would be displayed if it were the value; it serves as an example of the
|
2013-04-22 23:09:11 -06:00
|
|
|
// desired output. The same display rules will then be applied to the time
|
2013-04-22 12:36:17 -06:00
|
|
|
// value.
|
2015-07-14 23:02:38 -06:00
|
|
|
//
|
|
|
|
// A fractional second is represented by adding a period and zeros
|
|
|
|
// to the end of the seconds section of layout string, as in "15:04:05.000"
|
|
|
|
// to format a time stamp with millisecond precision.
|
|
|
|
//
|
2013-04-22 12:36:17 -06:00
|
|
|
// Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard
|
|
|
|
// and convenient representations of the reference time. For more information
|
|
|
|
// about the formats and the definition of the reference time, see the
|
|
|
|
// documentation for ANSIC and the other constants defined by this package.
|
2011-11-30 09:59:44 -07:00
|
|
|
func (t Time) Format(layout string) string {
|
2014-12-17 21:18:06 -07:00
|
|
|
const bufSize = 64
|
|
|
|
var b []byte
|
|
|
|
max := len(layout) + 10
|
|
|
|
if max < bufSize {
|
|
|
|
var buf [bufSize]byte
|
|
|
|
b = buf[:0]
|
|
|
|
} else {
|
|
|
|
b = make([]byte, 0, max)
|
|
|
|
}
|
|
|
|
b = t.AppendFormat(b, layout)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendFormat is like Format but appends the textual
|
|
|
|
// representation to b and returns the extended buffer.
|
|
|
|
func (t Time) AppendFormat(b []byte, layout string) []byte {
|
2011-11-30 09:59:44 -07:00
|
|
|
var (
|
2012-06-03 09:08:17 -06:00
|
|
|
name, offset, abs = t.locabs()
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
year int = -1
|
|
|
|
month Month
|
|
|
|
day int
|
2018-07-09 20:52:22 -06:00
|
|
|
yday int
|
2011-11-30 09:59:44 -07:00
|
|
|
hour int = -1
|
|
|
|
min int
|
|
|
|
sec int
|
|
|
|
)
|
2010-03-30 15:54:32 -06:00
|
|
|
// Each iteration generates one std value.
|
2012-06-03 09:08:17 -06:00
|
|
|
for layout != "" {
|
2010-03-30 15:54:32 -06:00
|
|
|
prefix, std, suffix := nextStdChunk(layout)
|
2012-06-03 09:08:17 -06:00
|
|
|
if prefix != "" {
|
|
|
|
b = append(b, prefix...)
|
|
|
|
}
|
|
|
|
if std == 0 {
|
2010-03-30 15:54:32 -06:00
|
|
|
break
|
2010-01-06 20:36:54 -07:00
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
layout = suffix
|
2011-11-30 09:59:44 -07:00
|
|
|
|
|
|
|
// Compute year, month, day if needed.
|
2012-06-03 09:08:17 -06:00
|
|
|
if year < 0 && std&stdNeedDate != 0 {
|
2018-07-09 20:52:22 -06:00
|
|
|
year, month, day, yday = absDate(abs, true)
|
|
|
|
yday++
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute hour, minute, second if needed.
|
2012-06-03 09:08:17 -06:00
|
|
|
if hour < 0 && std&stdNeedClock != 0 {
|
|
|
|
hour, min, sec = absClock(abs)
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
|
2012-06-03 09:08:17 -06:00
|
|
|
switch std & stdMask {
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdYear:
|
2012-06-03 09:08:17 -06:00
|
|
|
y := year
|
|
|
|
if y < 0 {
|
|
|
|
y = -y
|
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, y%100, 2)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdLongYear:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, year, 4)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdMonth:
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, month.String()[:3]...)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdLongMonth:
|
2012-06-03 09:08:17 -06:00
|
|
|
m := month.String()
|
|
|
|
b = append(b, m...)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdNumMonth:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, int(month), 0)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdZeroMonth:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, int(month), 2)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdWeekDay:
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, absWeekday(abs).String()[:3]...)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdLongWeekDay:
|
2012-06-03 09:08:17 -06:00
|
|
|
s := absWeekday(abs).String()
|
|
|
|
b = append(b, s...)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdDay:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, day, 0)
|
2010-01-12 20:39:30 -07:00
|
|
|
case stdUnderDay:
|
2015-01-11 03:29:00 -07:00
|
|
|
if day < 10 {
|
|
|
|
b = append(b, ' ')
|
|
|
|
}
|
|
|
|
b = appendInt(b, day, 0)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdZeroDay:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, day, 2)
|
2018-07-09 20:52:22 -06:00
|
|
|
case stdUnderYearDay:
|
|
|
|
if yday < 100 {
|
|
|
|
b = append(b, ' ')
|
|
|
|
if yday < 10 {
|
|
|
|
b = append(b, ' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = appendInt(b, yday, 0)
|
|
|
|
case stdZeroYearDay:
|
|
|
|
b = appendInt(b, yday, 3)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdHour:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, hour, 2)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdHour12:
|
2011-05-27 15:06:53 -06:00
|
|
|
// Noon is 12PM, midnight is 12AM.
|
2011-11-30 09:59:44 -07:00
|
|
|
hr := hour % 12
|
2011-05-27 15:06:53 -06:00
|
|
|
if hr == 0 {
|
|
|
|
hr = 12
|
2011-05-27 07:24:39 -06:00
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, hr, 0)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdZeroHour12:
|
2011-05-27 15:06:53 -06:00
|
|
|
// Noon is 12PM, midnight is 12AM.
|
2011-11-30 09:59:44 -07:00
|
|
|
hr := hour % 12
|
2011-05-27 15:06:53 -06:00
|
|
|
if hr == 0 {
|
|
|
|
hr = 12
|
2011-05-27 07:24:39 -06:00
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, hr, 2)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdMinute:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, min, 0)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdZeroMinute:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, min, 2)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdSecond:
|
2015-09-02 16:39:53 -06:00
|
|
|
b = appendInt(b, sec, 0)
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdZeroSecond:
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, sec, 2)
|
2011-11-30 09:59:44 -07:00
|
|
|
case stdPM:
|
|
|
|
if hour >= 12 {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, "PM"...)
|
2011-11-30 09:59:44 -07:00
|
|
|
} else {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, "AM"...)
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
case stdpm:
|
|
|
|
if hour >= 12 {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, "pm"...)
|
2011-11-30 09:59:44 -07:00
|
|
|
} else {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, "am"...)
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
2015-11-30 10:21:34 -07:00
|
|
|
case stdISO8601TZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ShortTZ, stdISO8601ColonSecondsTZ, stdNumTZ, stdNumColonTZ, stdNumSecondsTz, stdNumShortTZ, stdNumColonSecondsTZ:
|
2016-03-01 16:21:55 -07:00
|
|
|
// Ugly special case. We cheat and take the "Z" variants
|
2010-04-27 01:05:24 -06:00
|
|
|
// to mean "the time zone as formatted for ISO 8601".
|
2015-11-30 10:21:34 -07:00
|
|
|
if offset == 0 && (std == stdISO8601TZ || std == stdISO8601ColonTZ || std == stdISO8601SecondsTZ || std == stdISO8601ShortTZ || std == stdISO8601ColonSecondsTZ) {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, 'Z')
|
2010-03-30 15:54:32 -06:00
|
|
|
break
|
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
zone := offset / 60 // convert to minutes
|
2013-07-31 00:11:02 -06:00
|
|
|
absoffset := offset
|
2010-03-30 15:54:32 -06:00
|
|
|
if zone < 0 {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, '-')
|
2010-03-30 15:54:32 -06:00
|
|
|
zone = -zone
|
2013-07-31 00:11:02 -06:00
|
|
|
absoffset = -absoffset
|
2010-01-06 16:32:48 -07:00
|
|
|
} else {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, '+')
|
2010-01-06 16:32:48 -07:00
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, zone/60, 2)
|
2014-08-08 13:42:20 -06:00
|
|
|
if std == stdISO8601ColonTZ || std == stdNumColonTZ || std == stdISO8601ColonSecondsTZ || std == stdNumColonSecondsTZ {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, ':')
|
2010-04-27 01:05:24 -06:00
|
|
|
}
|
2015-11-30 10:21:34 -07:00
|
|
|
if std != stdNumShortTZ && std != stdISO8601ShortTZ {
|
|
|
|
b = appendInt(b, zone%60, 2)
|
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
|
|
|
|
// append seconds if appropriate
|
|
|
|
if std == stdISO8601SecondsTZ || std == stdNumSecondsTz || std == stdNumColonSecondsTZ || std == stdISO8601ColonSecondsTZ {
|
|
|
|
if std == stdNumColonSecondsTZ || std == stdISO8601ColonSecondsTZ {
|
|
|
|
b = append(b, ':')
|
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, absoffset%60, 2)
|
2013-07-31 00:11:02 -06:00
|
|
|
}
|
|
|
|
|
2010-01-06 16:32:48 -07:00
|
|
|
case stdTZ:
|
2011-11-30 09:59:44 -07:00
|
|
|
if name != "" {
|
2012-06-03 09:08:17 -06:00
|
|
|
b = append(b, name...)
|
|
|
|
break
|
2010-02-03 21:39:27 -07:00
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
// No time zone known for this time, but we must print one.
|
|
|
|
// Use the -0700 format.
|
|
|
|
zone := offset / 60 // convert to minutes
|
|
|
|
if zone < 0 {
|
|
|
|
b = append(b, '-')
|
|
|
|
zone = -zone
|
|
|
|
} else {
|
|
|
|
b = append(b, '+')
|
2011-08-07 15:50:17 -06:00
|
|
|
}
|
2015-01-11 03:29:00 -07:00
|
|
|
b = appendInt(b, zone/60, 2)
|
|
|
|
b = appendInt(b, zone%60, 2)
|
2012-06-03 09:08:17 -06:00
|
|
|
case stdFracSecond0, stdFracSecond9:
|
|
|
|
b = formatNano(b, uint(t.Nanosecond()), std>>stdArgShift, std&stdMask == stdFracSecond9)
|
2010-01-06 16:32:48 -07:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 21:18:06 -07:00
|
|
|
return b
|
2010-01-06 16:32:48 -07:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
var errBad = errors.New("bad value for field") // placeholder not passed to user
|
2010-01-07 18:59:20 -07:00
|
|
|
|
|
|
|
// ParseError describes a problem parsing a time string.
|
|
|
|
type ParseError struct {
|
|
|
|
Layout string
|
|
|
|
Value string
|
|
|
|
LayoutElem string
|
|
|
|
ValueElem string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
func quote(s string) string {
|
|
|
|
return "\"" + s + "\""
|
|
|
|
}
|
|
|
|
|
2011-12-01 12:44:17 -07:00
|
|
|
// Error returns the string representation of a ParseError.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (e *ParseError) Error() string {
|
2010-01-07 18:59:20 -07:00
|
|
|
if e.Message == "" {
|
2010-09-11 22:06:45 -06:00
|
|
|
return "parsing time " +
|
2011-11-30 09:59:44 -07:00
|
|
|
quote(e.Value) + " as " +
|
|
|
|
quote(e.Layout) + ": cannot parse " +
|
|
|
|
quote(e.ValueElem) + " as " +
|
|
|
|
quote(e.LayoutElem)
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
|
|
|
return "parsing time " +
|
2011-11-30 09:59:44 -07:00
|
|
|
quote(e.Value) + e.Message
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// isDigit reports whether s[i] is in range and is a decimal digit.
|
2011-08-10 07:24:45 -06:00
|
|
|
func isDigit(s string, i int) bool {
|
|
|
|
if len(s) <= i {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c := s[i]
|
|
|
|
return '0' <= c && c <= '9'
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:52:22 -06:00
|
|
|
// getnum parses s[0:1] or s[0:2] (fixed forces s[0:2])
|
2010-03-30 15:54:32 -06:00
|
|
|
// as a decimal integer and returns the integer and the
|
|
|
|
// remainder of the string.
|
2011-11-01 20:05:34 -06:00
|
|
|
func getnum(s string, fixed bool) (int, string, error) {
|
2011-08-10 07:24:45 -06:00
|
|
|
if !isDigit(s, 0) {
|
2010-03-30 15:54:32 -06:00
|
|
|
return 0, s, errBad
|
2010-01-12 20:39:30 -07:00
|
|
|
}
|
2011-08-10 07:24:45 -06:00
|
|
|
if !isDigit(s, 1) {
|
2010-03-30 15:54:32 -06:00
|
|
|
if fixed {
|
|
|
|
return 0, s, errBad
|
2010-01-12 20:39:30 -07:00
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
return int(s[0] - '0'), s[1:], nil
|
2010-01-12 20:39:30 -07:00
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
return int(s[0]-'0')*10 + int(s[1]-'0'), s[2:], nil
|
2010-01-12 20:39:30 -07:00
|
|
|
}
|
|
|
|
|
2018-07-09 20:52:22 -06:00
|
|
|
// getnum3 parses s[0:1], s[0:2], or s[0:3] (fixed forces s[0:3])
|
|
|
|
// as a decimal integer and returns the integer and the remainder
|
|
|
|
// of the string.
|
|
|
|
func getnum3(s string, fixed bool) (int, string, error) {
|
|
|
|
var n, i int
|
|
|
|
for i = 0; i < 3 && isDigit(s, i); i++ {
|
|
|
|
n = n*10 + int(s[i]-'0')
|
|
|
|
}
|
|
|
|
if i == 0 || fixed && i != 3 {
|
|
|
|
return 0, s, errBad
|
|
|
|
}
|
|
|
|
return n, s[i:], nil
|
|
|
|
}
|
|
|
|
|
2010-03-30 15:54:32 -06:00
|
|
|
func cutspace(s string) string {
|
|
|
|
for len(s) > 0 && s[0] == ' ' {
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip removes the given prefix from value,
|
|
|
|
// treating runs of space characters as equivalent.
|
2011-11-01 20:05:34 -06:00
|
|
|
func skip(value, prefix string) (string, error) {
|
2010-03-30 15:54:32 -06:00
|
|
|
for len(prefix) > 0 {
|
|
|
|
if prefix[0] == ' ' {
|
|
|
|
if len(value) > 0 && value[0] != ' ' {
|
2013-02-03 22:00:36 -07:00
|
|
|
return value, errBad
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
|
|
|
prefix = cutspace(prefix)
|
|
|
|
value = cutspace(value)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(value) == 0 || value[0] != prefix[0] {
|
2013-02-03 22:00:36 -07:00
|
|
|
return value, errBad
|
2010-03-30 15:54:32 -06:00
|
|
|
}
|
|
|
|
prefix = prefix[1:]
|
|
|
|
value = value[1:]
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
2010-01-12 20:39:30 -07:00
|
|
|
|
2010-01-07 18:59:20 -07:00
|
|
|
// Parse parses a formatted string and returns the time value it represents.
|
2017-08-10 00:00:39 -06:00
|
|
|
// The layout defines the format by showing how the reference time,
|
2014-07-28 18:24:39 -06:00
|
|
|
// defined to be
|
2012-02-09 19:52:19 -07:00
|
|
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
2013-04-22 16:41:06 -06:00
|
|
|
// would be interpreted if it were the value; it serves as an example of
|
2013-04-22 12:36:17 -06:00
|
|
|
// the input format. The same interpretation will then be made to the
|
|
|
|
// input string.
|
2015-03-20 15:01:51 -06:00
|
|
|
//
|
2013-04-22 12:36:17 -06:00
|
|
|
// Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard
|
|
|
|
// and convenient representations of the reference time. For more information
|
|
|
|
// about the formats and the definition of the reference time, see the
|
|
|
|
// documentation for ANSIC and the other constants defined by this package.
|
2017-10-28 16:03:15 -06:00
|
|
|
// Also, the executable example for Time.Format demonstrates the working
|
2015-03-20 15:01:51 -06:00
|
|
|
// of the layout string in detail and is a good reference.
|
2010-01-07 18:59:20 -07:00
|
|
|
//
|
2012-02-09 19:52:19 -07:00
|
|
|
// Elements omitted from the value are assumed to be zero or, when
|
2011-11-30 09:59:44 -07:00
|
|
|
// zero is impossible, one, so parsing "3:04pm" returns the time
|
2013-01-18 13:57:31 -07:00
|
|
|
// corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is
|
|
|
|
// 0, this time is before the zero Time).
|
2011-09-12 12:47:55 -06:00
|
|
|
// Years must be in the range 0000..9999. The day of the week is checked
|
|
|
|
// for syntax but it is otherwise ignored.
|
2013-02-03 21:02:12 -07:00
|
|
|
//
|
|
|
|
// In the absence of a time zone indicator, Parse returns a time in UTC.
|
|
|
|
//
|
|
|
|
// When parsing a time with a zone offset like -0700, if the offset corresponds
|
|
|
|
// to a time zone used by the current location (Local), then Parse uses that
|
|
|
|
// location and zone in the returned time. Otherwise it records the time as
|
|
|
|
// being in a fabricated location with time fixed at the given zone offset.
|
|
|
|
//
|
|
|
|
// When parsing a time with a zone abbreviation like MST, if the zone abbreviation
|
|
|
|
// has a defined offset in the current location, then that offset is used.
|
|
|
|
// The zone abbreviation "UTC" is recognized as UTC regardless of location.
|
|
|
|
// If the zone abbreviation is unknown, Parse records the time as being
|
|
|
|
// in a fabricated location with the given zone abbreviation and a zero offset.
|
2014-07-24 16:18:41 -06:00
|
|
|
// This choice means that such a time can be parsed and reformatted with the
|
2013-02-03 21:02:12 -07:00
|
|
|
// same layout losslessly, but the exact instant used in the representation will
|
|
|
|
// differ by the actual zone offset. To avoid such problems, prefer time layouts
|
|
|
|
// that use a numeric zone offset, or use ParseInLocation.
|
2011-11-30 09:59:44 -07:00
|
|
|
func Parse(layout, value string) (Time, error) {
|
2013-02-03 21:02:12 -07:00
|
|
|
return parse(layout, value, UTC, Local)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseInLocation is like Parse but differs in two important ways.
|
|
|
|
// First, in the absence of time zone information, Parse interprets a time as UTC;
|
|
|
|
// ParseInLocation interprets the time as in the given location.
|
|
|
|
// Second, when given a zone offset or abbreviation, Parse tries to match it
|
|
|
|
// against the Local location; ParseInLocation uses the given location.
|
|
|
|
func ParseInLocation(layout, value string, loc *Location) (Time, error) {
|
|
|
|
return parse(layout, value, loc, loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(layout, value string, defaultLocation, local *Location) (Time, error) {
|
2011-11-30 09:59:44 -07:00
|
|
|
alayout, avalue := layout, value
|
2010-01-07 18:59:20 -07:00
|
|
|
rangeErrString := "" // set if a value is out of range
|
2011-05-27 15:06:53 -06:00
|
|
|
amSet := false // do we need to subtract 12 from the hour for midnight?
|
2010-01-07 18:59:20 -07:00
|
|
|
pmSet := false // do we need to add 12 to the hour?
|
2011-11-30 09:59:44 -07:00
|
|
|
|
|
|
|
// Time being constructed.
|
|
|
|
var (
|
|
|
|
year int
|
2018-07-09 20:52:22 -06:00
|
|
|
month int = -1
|
|
|
|
day int = -1
|
|
|
|
yday int = -1
|
2011-11-30 09:59:44 -07:00
|
|
|
hour int
|
|
|
|
min int
|
|
|
|
sec int
|
|
|
|
nsec int
|
|
|
|
z *Location
|
|
|
|
zoneOffset int = -1
|
|
|
|
zoneName string
|
|
|
|
)
|
|
|
|
|
2010-03-30 15:54:32 -06:00
|
|
|
// Each iteration processes one std value.
|
|
|
|
for {
|
2011-11-01 20:05:34 -06:00
|
|
|
var err error
|
2010-03-30 15:54:32 -06:00
|
|
|
prefix, std, suffix := nextStdChunk(layout)
|
2012-06-03 09:08:17 -06:00
|
|
|
stdstr := layout[len(prefix) : len(layout)-len(suffix)]
|
2010-03-30 15:54:32 -06:00
|
|
|
value, err = skip(value, prefix)
|
|
|
|
if err != nil {
|
2011-11-30 09:59:44 -07:00
|
|
|
return Time{}, &ParseError{alayout, avalue, prefix, value, ""}
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2012-06-03 09:08:17 -06:00
|
|
|
if std == 0 {
|
2010-03-30 15:54:32 -06:00
|
|
|
if len(value) != 0 {
|
2011-11-30 09:59:44 -07:00
|
|
|
return Time{}, &ParseError{alayout, avalue, "", value, ": extra text: " + value}
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
break
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
layout = suffix
|
|
|
|
var p string
|
2012-06-03 09:08:17 -06:00
|
|
|
switch std & stdMask {
|
2010-01-07 18:59:20 -07:00
|
|
|
case stdYear:
|
2010-03-30 15:54:32 -06:00
|
|
|
if len(value) < 2 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p, value = value[0:2], value[2:]
|
2011-11-30 09:59:44 -07:00
|
|
|
year, err = atoi(p)
|
|
|
|
if year >= 69 { // Unix time starts Dec 31 1969 in some time zones
|
|
|
|
year += 1900
|
2010-01-07 18:59:20 -07:00
|
|
|
} else {
|
2011-11-30 09:59:44 -07:00
|
|
|
year += 2000
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
|
|
|
case stdLongYear:
|
2011-08-10 07:24:45 -06:00
|
|
|
if len(value) < 4 || !isDigit(value, 0) {
|
2010-03-30 15:54:32 -06:00
|
|
|
err = errBad
|
|
|
|
break
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2010-03-30 15:54:32 -06:00
|
|
|
p, value = value[0:4], value[4:]
|
2011-11-30 09:59:44 -07:00
|
|
|
year, err = atoi(p)
|
2010-01-07 18:59:20 -07:00
|
|
|
case stdMonth:
|
2011-11-30 09:59:44 -07:00
|
|
|
month, value, err = lookup(shortMonthNames, value)
|
2017-07-25 00:04:50 -06:00
|
|
|
month++
|
2010-01-07 18:59:20 -07:00
|
|
|
case stdLongMonth:
|
2011-11-30 09:59:44 -07:00
|
|
|
month, value, err = lookup(longMonthNames, value)
|
2017-07-25 00:04:50 -06:00
|
|
|
month++
|
2018-01-03 11:24:29 -07:00
|
|
|
case stdNumMonth, stdZeroMonth:
|
2011-11-30 09:59:44 -07:00
|
|
|
month, value, err = getnum(value, std == stdZeroMonth)
|
|
|
|
if month <= 0 || 12 < month {
|
2010-01-07 18:59:20 -07:00
|
|
|
rangeErrString = "month"
|
|
|
|
}
|
|
|
|
case stdWeekDay:
|
2011-09-12 12:47:55 -06:00
|
|
|
// Ignore weekday except for error checking.
|
|
|
|
_, value, err = lookup(shortDayNames, value)
|
2010-01-07 18:59:20 -07:00
|
|
|
case stdLongWeekDay:
|
2011-09-12 12:47:55 -06:00
|
|
|
_, value, err = lookup(longDayNames, value)
|
2010-01-12 20:39:30 -07:00
|
|
|
case stdDay, stdUnderDay, stdZeroDay:
|
2010-03-30 15:54:32 -06:00
|
|
|
if std == stdUnderDay && len(value) > 0 && value[0] == ' ' {
|
|
|
|
value = value[1:]
|
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
day, value, err = getnum(value, std == stdZeroDay)
|
2018-07-09 20:52:22 -06:00
|
|
|
// Note that we allow any one- or two-digit day here.
|
|
|
|
// The month, day, year combination is validated after we've completed parsing.
|
|
|
|
case stdUnderYearDay, stdZeroYearDay:
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if std == stdUnderYearDay && len(value) > 0 && value[0] == ' ' {
|
|
|
|
value = value[1:]
|
|
|
|
}
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2018-07-09 20:52:22 -06:00
|
|
|
yday, value, err = getnum3(value, std == stdZeroYearDay)
|
|
|
|
// Note that we allow any one-, two-, or three-digit year-day here.
|
|
|
|
// The year-day, year combination is validated after we've completed parsing.
|
2010-01-07 18:59:20 -07:00
|
|
|
case stdHour:
|
2011-11-30 09:59:44 -07:00
|
|
|
hour, value, err = getnum(value, false)
|
|
|
|
if hour < 0 || 24 <= hour {
|
2010-01-07 18:59:20 -07:00
|
|
|
rangeErrString = "hour"
|
|
|
|
}
|
2018-01-03 11:24:29 -07:00
|
|
|
case stdHour12, stdZeroHour12:
|
2011-11-30 09:59:44 -07:00
|
|
|
hour, value, err = getnum(value, std == stdZeroHour12)
|
|
|
|
if hour < 0 || 12 < hour {
|
2010-01-07 18:59:20 -07:00
|
|
|
rangeErrString = "hour"
|
|
|
|
}
|
2018-01-03 11:24:29 -07:00
|
|
|
case stdMinute, stdZeroMinute:
|
2011-11-30 09:59:44 -07:00
|
|
|
min, value, err = getnum(value, std == stdZeroMinute)
|
|
|
|
if min < 0 || 60 <= min {
|
2010-01-07 18:59:20 -07:00
|
|
|
rangeErrString = "minute"
|
|
|
|
}
|
2018-01-03 11:24:29 -07:00
|
|
|
case stdSecond, stdZeroSecond:
|
2011-11-30 09:59:44 -07:00
|
|
|
sec, value, err = getnum(value, std == stdZeroSecond)
|
|
|
|
if sec < 0 || 60 <= sec {
|
2010-01-07 18:59:20 -07:00
|
|
|
rangeErrString = "second"
|
2016-08-23 06:31:26 -06:00
|
|
|
break
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2011-08-10 07:24:45 -06:00
|
|
|
// Special case: do we have a fractional second but no
|
|
|
|
// fractional second in the format?
|
2012-04-15 19:56:37 -06:00
|
|
|
if len(value) >= 2 && value[0] == '.' && isDigit(value, 1) {
|
2013-06-20 17:14:40 -06:00
|
|
|
_, std, _ = nextStdChunk(layout)
|
2012-06-03 09:08:17 -06:00
|
|
|
std &= stdMask
|
|
|
|
if std == stdFracSecond0 || std == stdFracSecond9 {
|
2011-08-10 07:24:45 -06:00
|
|
|
// Fractional second in the layout; proceed normally
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// No fractional second in the layout but we have one in the input.
|
|
|
|
n := 2
|
|
|
|
for ; n < len(value) && isDigit(value, n); n++ {
|
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
nsec, rangeErrString, err = parseNanoseconds(value, n)
|
2011-08-10 07:24:45 -06:00
|
|
|
value = value[n:]
|
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
case stdPM:
|
|
|
|
if len(value) < 2 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p, value = value[0:2], value[2:]
|
|
|
|
switch p {
|
|
|
|
case "PM":
|
|
|
|
pmSet = true
|
|
|
|
case "AM":
|
|
|
|
amSet = true
|
|
|
|
default:
|
|
|
|
err = errBad
|
|
|
|
}
|
|
|
|
case stdpm:
|
|
|
|
if len(value) < 2 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p, value = value[0:2], value[2:]
|
|
|
|
switch p {
|
|
|
|
case "pm":
|
|
|
|
pmSet = true
|
|
|
|
case "am":
|
|
|
|
amSet = true
|
|
|
|
default:
|
|
|
|
err = errBad
|
|
|
|
}
|
2015-11-30 10:21:34 -07:00
|
|
|
case stdISO8601TZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ShortTZ, stdISO8601ColonSecondsTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ, stdNumSecondsTz, stdNumColonSecondsTZ:
|
|
|
|
if (std == stdISO8601TZ || std == stdISO8601ShortTZ || std == stdISO8601ColonTZ) && len(value) >= 1 && value[0] == 'Z' {
|
2010-03-30 15:54:32 -06:00
|
|
|
value = value[1:]
|
2011-11-30 09:59:44 -07:00
|
|
|
z = UTC
|
2010-01-07 18:59:20 -07:00
|
|
|
break
|
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
var sign, hour, min, seconds string
|
2010-04-27 01:05:24 -06:00
|
|
|
if std == stdISO8601ColonTZ || std == stdNumColonTZ {
|
|
|
|
if len(value) < 6 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if value[3] != ':' {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
sign, hour, min, seconds, value = value[0:1], value[1:3], value[4:6], "00", value[6:]
|
2015-11-30 10:21:34 -07:00
|
|
|
} else if std == stdNumShortTZ || std == stdISO8601ShortTZ {
|
2010-08-24 15:41:26 -06:00
|
|
|
if len(value) < 3 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
sign, hour, min, seconds, value = value[0:1], value[1:3], "00", "00", value[3:]
|
|
|
|
} else if std == stdISO8601ColonSecondsTZ || std == stdNumColonSecondsTZ {
|
|
|
|
if len(value) < 9 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if value[3] != ':' || value[6] != ':' {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
sign, hour, min, seconds, value = value[0:1], value[1:3], value[4:6], value[7:9], value[9:]
|
|
|
|
} else if std == stdISO8601SecondsTZ || std == stdNumSecondsTz {
|
|
|
|
if len(value) < 7 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], value[5:7], value[7:]
|
2010-04-27 01:05:24 -06:00
|
|
|
} else {
|
|
|
|
if len(value) < 5 {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], "00", value[5:]
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
var hr, mm, ss int
|
2011-11-30 09:59:44 -07:00
|
|
|
hr, err = atoi(hour)
|
2010-07-19 09:08:04 -06:00
|
|
|
if err == nil {
|
2011-11-30 09:59:44 -07:00
|
|
|
mm, err = atoi(min)
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2013-07-31 00:11:02 -06:00
|
|
|
if err == nil {
|
|
|
|
ss, err = atoi(seconds)
|
|
|
|
}
|
|
|
|
zoneOffset = (hr*60+mm)*60 + ss // offset is in seconds
|
2010-01-13 17:57:38 -07:00
|
|
|
switch sign[0] {
|
2010-01-07 18:59:20 -07:00
|
|
|
case '+':
|
|
|
|
case '-':
|
2011-11-30 09:59:44 -07:00
|
|
|
zoneOffset = -zoneOffset
|
2011-05-27 15:06:53 -06:00
|
|
|
default:
|
2010-01-07 18:59:20 -07:00
|
|
|
err = errBad
|
|
|
|
}
|
|
|
|
case stdTZ:
|
|
|
|
// Does it look like a time zone?
|
2010-03-30 15:54:32 -06:00
|
|
|
if len(value) >= 3 && value[0:3] == "UTC" {
|
2011-11-30 09:59:44 -07:00
|
|
|
z = UTC
|
|
|
|
value = value[3:]
|
2010-01-07 18:59:20 -07:00
|
|
|
break
|
|
|
|
}
|
2013-08-14 18:10:49 -06:00
|
|
|
n, ok := parseTimeZone(value)
|
|
|
|
if !ok {
|
2010-01-07 18:59:20 -07:00
|
|
|
err = errBad
|
2010-03-30 15:54:32 -06:00
|
|
|
break
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2013-08-14 18:10:49 -06:00
|
|
|
zoneName, value = value[:n], value[n:]
|
2012-06-03 09:08:17 -06:00
|
|
|
|
2012-06-04 11:09:19 -06:00
|
|
|
case stdFracSecond0:
|
2012-12-17 13:52:23 -07:00
|
|
|
// stdFracSecond0 requires the exact number of digits as specified in
|
|
|
|
// the layout.
|
|
|
|
ndigit := 1 + (std >> stdArgShift)
|
|
|
|
if len(value) < ndigit {
|
|
|
|
err = errBad
|
|
|
|
break
|
|
|
|
}
|
|
|
|
nsec, rangeErrString, err = parseNanoseconds(value, ndigit)
|
|
|
|
value = value[ndigit:]
|
2012-06-04 11:09:19 -06:00
|
|
|
|
|
|
|
case stdFracSecond9:
|
|
|
|
if len(value) < 2 || value[0] != '.' || value[1] < '0' || '9' < value[1] {
|
|
|
|
// Fractional second omitted.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Take any number of digits, even more than asked for,
|
|
|
|
// because it is what the stdSecond case would do.
|
|
|
|
i := 0
|
|
|
|
for i < 9 && i+1 < len(value) && '0' <= value[i+1] && value[i+1] <= '9' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
nsec, rangeErrString, err = parseNanoseconds(value, 1+i)
|
|
|
|
value = value[1+i:]
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
|
|
|
if rangeErrString != "" {
|
2012-06-03 09:08:17 -06:00
|
|
|
return Time{}, &ParseError{alayout, avalue, stdstr, value, ": " + rangeErrString + " out of range"}
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
2012-06-03 09:08:17 -06:00
|
|
|
return Time{}, &ParseError{alayout, avalue, stdstr, value, ""}
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if pmSet && hour < 12 {
|
|
|
|
hour += 12
|
|
|
|
} else if amSet && hour == 12 {
|
|
|
|
hour = 0
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:52:22 -06:00
|
|
|
// Convert yday to day, month.
|
|
|
|
if yday >= 0 {
|
|
|
|
var d int
|
|
|
|
var m int
|
|
|
|
if isLeap(year) {
|
|
|
|
if yday == 31+29 {
|
|
|
|
m = int(February)
|
|
|
|
d = 29
|
|
|
|
} else if yday > 31+29 {
|
|
|
|
yday--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if yday < 1 || yday > 365 {
|
|
|
|
return Time{}, &ParseError{alayout, avalue, "", value, ": day-of-year out of range"}
|
|
|
|
}
|
|
|
|
if m == 0 {
|
|
|
|
m = yday/31 + 1
|
|
|
|
if int(daysBefore[m]) < yday {
|
|
|
|
m++
|
|
|
|
}
|
|
|
|
d = yday - int(daysBefore[m-1])
|
|
|
|
}
|
|
|
|
// If month, day already seen, yday's m, d must match.
|
|
|
|
// Otherwise, set them from m, d.
|
|
|
|
if month >= 0 && month != m {
|
|
|
|
return Time{}, &ParseError{alayout, avalue, "", value, ": day-of-year does not match month"}
|
|
|
|
}
|
|
|
|
month = m
|
|
|
|
if day >= 0 && day != d {
|
|
|
|
return Time{}, &ParseError{alayout, avalue, "", value, ": day-of-year does not match day"}
|
|
|
|
}
|
|
|
|
day = d
|
|
|
|
} else {
|
|
|
|
if month < 0 {
|
|
|
|
month = int(January)
|
|
|
|
}
|
|
|
|
if day < 0 {
|
|
|
|
day = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 11:41:08 -07:00
|
|
|
// Validate the day of the month.
|
2016-11-21 18:40:07 -07:00
|
|
|
if day < 1 || day > daysIn(Month(month), year) {
|
2016-01-07 13:36:54 -07:00
|
|
|
return Time{}, &ParseError{alayout, avalue, "", value, ": day out of range"}
|
2015-12-10 11:41:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
if z != nil {
|
|
|
|
return Date(year, Month(month), day, hour, min, sec, nsec, z), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if zoneOffset != -1 {
|
2013-02-03 21:02:12 -07:00
|
|
|
t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
|
2017-02-02 14:20:58 -07:00
|
|
|
t.addSec(-int64(zoneOffset))
|
2011-11-30 09:59:44 -07:00
|
|
|
|
|
|
|
// Look for local zone with the given offset.
|
|
|
|
// If that zone was in effect at the given time, use it.
|
2018-03-24 09:49:43 -06:00
|
|
|
name, offset, _, _ := local.lookup(t.unixSec())
|
2011-11-30 09:59:44 -07:00
|
|
|
if offset == zoneOffset && (zoneName == "" || name == zoneName) {
|
2016-10-17 13:47:18 -06:00
|
|
|
t.setLoc(local)
|
2011-11-30 09:59:44 -07:00
|
|
|
return t, nil
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
|
|
|
|
// Otherwise create fake zone to record offset.
|
2016-10-17 13:47:18 -06:00
|
|
|
t.setLoc(FixedZone(zoneName, zoneOffset))
|
2011-11-30 09:59:44 -07:00
|
|
|
return t, nil
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
|
|
|
|
if zoneName != "" {
|
2013-02-03 21:02:12 -07:00
|
|
|
t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
|
2011-11-30 09:59:44 -07:00
|
|
|
// Look for local zone with the given offset.
|
|
|
|
// If that zone was in effect at the given time, use it.
|
2017-08-13 19:49:27 -06:00
|
|
|
offset, ok := local.lookupName(zoneName, t.unixSec())
|
2011-11-30 09:59:44 -07:00
|
|
|
if ok {
|
2017-02-02 14:20:58 -07:00
|
|
|
t.addSec(-int64(offset))
|
2016-10-17 13:47:18 -06:00
|
|
|
t.setLoc(local)
|
2013-02-03 21:02:12 -07:00
|
|
|
return t, nil
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create fake zone with unknown offset.
|
2013-08-14 18:10:49 -06:00
|
|
|
if len(zoneName) > 3 && zoneName[:3] == "GMT" {
|
|
|
|
offset, _ = atoi(zoneName[3:]) // Guaranteed OK by parseGMT.
|
|
|
|
offset *= 3600
|
|
|
|
}
|
2016-10-17 13:47:18 -06:00
|
|
|
t.setLoc(FixedZone(zoneName, offset))
|
2011-11-30 09:59:44 -07:00
|
|
|
return t, nil
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
|
2013-02-03 21:02:12 -07:00
|
|
|
// Otherwise, fall back to default.
|
|
|
|
return Date(year, Month(month), day, hour, min, sec, nsec, defaultLocation), nil
|
2010-01-07 18:59:20 -07:00
|
|
|
}
|
2011-08-10 07:24:45 -06:00
|
|
|
|
2013-08-15 00:42:54 -06:00
|
|
|
// parseTimeZone parses a time zone string and returns its length. Time zones
|
|
|
|
// are human-generated and unpredictable. We can't do precise error checking.
|
|
|
|
// On the other hand, for a correct parse there must be a time zone at the
|
|
|
|
// beginning of the string, so it's almost always true that there's one
|
2013-08-15 22:57:49 -06:00
|
|
|
// there. We look at the beginning of the string for a run of upper-case letters.
|
|
|
|
// If there are more than 5, it's an error.
|
|
|
|
// If there are 4 or 5 and the last is a T, it's a time zone.
|
|
|
|
// If there are 3, it's a time zone.
|
|
|
|
// Otherwise, other than special cases, it's not a time zone.
|
2013-08-15 00:42:54 -06:00
|
|
|
// GMT is special because it can have an hour offset.
|
2013-08-14 18:10:49 -06:00
|
|
|
func parseTimeZone(value string) (length int, ok bool) {
|
|
|
|
if len(value) < 3 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2014-04-30 09:30:36 -06:00
|
|
|
// Special case 1: ChST and MeST are the only zones with a lower-case letter.
|
|
|
|
if len(value) >= 4 && (value[:4] == "ChST" || value[:4] == "MeST") {
|
2013-08-15 00:42:54 -06:00
|
|
|
return 4, true
|
|
|
|
}
|
|
|
|
// Special case 2: GMT may have an hour offset; treat it specially.
|
|
|
|
if value[:3] == "GMT" {
|
2013-08-14 18:10:49 -06:00
|
|
|
length = parseGMT(value)
|
|
|
|
return length, true
|
|
|
|
}
|
2018-03-02 11:03:21 -07:00
|
|
|
// Special Case 3: Some time zones are not named, but have +/-00 format
|
|
|
|
if value[0] == '+' || value[0] == '-' {
|
|
|
|
length = parseSignedOffset(value)
|
2018-06-24 05:42:59 -06:00
|
|
|
ok := length > 0 // parseSignedOffset returns 0 in case of bad input
|
|
|
|
return length, ok
|
2018-03-02 11:03:21 -07:00
|
|
|
}
|
2013-08-15 22:57:49 -06:00
|
|
|
// How many upper-case letters are there? Need at least three, at most five.
|
|
|
|
var nUpper int
|
|
|
|
for nUpper = 0; nUpper < 6; nUpper++ {
|
|
|
|
if nUpper >= len(value) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if c := value[nUpper]; c < 'A' || 'Z' < c {
|
|
|
|
break
|
2013-08-14 18:10:49 -06:00
|
|
|
}
|
|
|
|
}
|
2013-08-15 22:57:49 -06:00
|
|
|
switch nUpper {
|
|
|
|
case 0, 1, 2, 6:
|
|
|
|
return 0, false
|
|
|
|
case 5: // Must end in T to match.
|
|
|
|
if value[4] == 'T' {
|
|
|
|
return 5, true
|
|
|
|
}
|
2016-12-11 03:54:22 -07:00
|
|
|
case 4:
|
|
|
|
// Must end in T, except one special case.
|
|
|
|
if value[3] == 'T' || value[:4] == "WITA" {
|
2013-08-15 22:57:49 -06:00
|
|
|
return 4, true
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
return 3, true
|
2013-08-15 00:42:54 -06:00
|
|
|
}
|
2013-08-15 22:57:49 -06:00
|
|
|
return 0, false
|
2013-08-14 18:10:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseGMT parses a GMT time zone. The input string is known to start "GMT".
|
|
|
|
// The function checks whether that is followed by a sign and a number in the
|
2018-06-24 05:42:59 -06:00
|
|
|
// range -23 through +23 excluding zero.
|
2013-08-14 18:10:49 -06:00
|
|
|
func parseGMT(value string) int {
|
|
|
|
value = value[3:]
|
|
|
|
if len(value) == 0 {
|
|
|
|
return 3
|
|
|
|
}
|
2018-03-02 11:03:21 -07:00
|
|
|
|
|
|
|
return 3 + parseSignedOffset(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseSignedOffset parses a signed timezone offset (e.g. "+03" or "-04").
|
2018-06-24 05:42:59 -06:00
|
|
|
// The function checks for a signed number in the range -23 through +23 excluding zero.
|
2018-03-02 11:03:21 -07:00
|
|
|
// Returns length of the found offset string or 0 otherwise
|
|
|
|
func parseSignedOffset(value string) int {
|
2013-08-14 18:10:49 -06:00
|
|
|
sign := value[0]
|
|
|
|
if sign != '-' && sign != '+' {
|
2018-03-02 11:03:21 -07:00
|
|
|
return 0
|
2013-08-14 18:10:49 -06:00
|
|
|
}
|
|
|
|
x, rem, err := leadingInt(value[1:])
|
2018-08-22 02:58:24 -06:00
|
|
|
|
|
|
|
// fail if nothing consumed by leadingInt
|
|
|
|
if err != nil || value[1:] == rem {
|
2018-03-02 11:03:21 -07:00
|
|
|
return 0
|
2013-08-14 18:10:49 -06:00
|
|
|
}
|
|
|
|
if sign == '-' {
|
|
|
|
x = -x
|
|
|
|
}
|
2018-08-22 02:58:24 -06:00
|
|
|
if x < -23 || 23 < x {
|
2018-03-02 11:03:21 -07:00
|
|
|
return 0
|
2013-08-14 18:10:49 -06:00
|
|
|
}
|
2018-03-02 11:03:21 -07:00
|
|
|
return len(value) - len(rem)
|
2013-08-14 18:10:49 -06:00
|
|
|
}
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
func parseNanoseconds(value string, nbytes int) (ns int, rangeErrString string, err error) {
|
2011-08-10 07:24:45 -06:00
|
|
|
if value[0] != '.' {
|
2011-11-30 09:59:44 -07:00
|
|
|
err = errBad
|
|
|
|
return
|
2011-08-10 07:24:45 -06:00
|
|
|
}
|
2012-12-17 13:52:23 -07:00
|
|
|
if ns, err = atoi(value[1:nbytes]); err != nil {
|
2011-11-30 09:59:44 -07:00
|
|
|
return
|
2011-08-10 07:24:45 -06:00
|
|
|
}
|
|
|
|
if ns < 0 || 1e9 <= ns {
|
2011-11-30 09:59:44 -07:00
|
|
|
rangeErrString = "fractional second"
|
|
|
|
return
|
2011-08-10 07:24:45 -06:00
|
|
|
}
|
|
|
|
// We need nanoseconds, which means scaling by the number
|
|
|
|
// of missing digits in the format, maximum length 10. If it's
|
|
|
|
// longer than 10, we won't scale.
|
|
|
|
scaleDigits := 10 - nbytes
|
|
|
|
for i := 0; i < scaleDigits; i++ {
|
|
|
|
ns *= 10
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2011-12-22 22:28:56 -07:00
|
|
|
|
|
|
|
var errLeadingInt = errors.New("time: bad [0-9]*") // never printed
|
|
|
|
|
|
|
|
// leadingInt consumes the leading [0-9]* from s.
|
2012-10-14 14:50:13 -06:00
|
|
|
func leadingInt(s string) (x int64, rem string, err error) {
|
2011-12-22 22:28:56 -07:00
|
|
|
i := 0
|
|
|
|
for ; i < len(s); i++ {
|
|
|
|
c := s[i]
|
|
|
|
if c < '0' || c > '9' {
|
|
|
|
break
|
|
|
|
}
|
2015-01-07 11:56:06 -07:00
|
|
|
if x > (1<<63-1)/10 {
|
2011-12-22 22:28:56 -07:00
|
|
|
// overflow
|
|
|
|
return 0, "", errLeadingInt
|
|
|
|
}
|
2012-10-14 14:50:13 -06:00
|
|
|
x = x*10 + int64(c) - '0'
|
2015-01-07 11:56:06 -07:00
|
|
|
if x < 0 {
|
|
|
|
// overflow
|
|
|
|
return 0, "", errLeadingInt
|
|
|
|
}
|
2011-12-22 22:28:56 -07:00
|
|
|
}
|
|
|
|
return x, s[i:], nil
|
|
|
|
}
|
|
|
|
|
2016-09-17 22:35:42 -06:00
|
|
|
// leadingFraction consumes the leading [0-9]* from s.
|
|
|
|
// It is used only for fractions, so does not return an error on overflow,
|
|
|
|
// it just stops accumulating precision.
|
|
|
|
func leadingFraction(s string) (x int64, scale float64, rem string) {
|
|
|
|
i := 0
|
|
|
|
scale = 1
|
|
|
|
overflow := false
|
|
|
|
for ; i < len(s); i++ {
|
|
|
|
c := s[i]
|
|
|
|
if c < '0' || c > '9' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if overflow {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if x > (1<<63-1)/10 {
|
|
|
|
// It's possible for overflow to give a positive number, so take care.
|
|
|
|
overflow = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
y := x*10 + int64(c) - '0'
|
|
|
|
if y < 0 {
|
|
|
|
overflow = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x = y
|
|
|
|
scale *= 10
|
|
|
|
}
|
|
|
|
return x, scale, s[i:]
|
|
|
|
}
|
|
|
|
|
2015-01-07 11:56:06 -07:00
|
|
|
var unitMap = map[string]int64{
|
|
|
|
"ns": int64(Nanosecond),
|
|
|
|
"us": int64(Microsecond),
|
|
|
|
"µs": int64(Microsecond), // U+00B5 = micro symbol
|
|
|
|
"μs": int64(Microsecond), // U+03BC = Greek letter mu
|
|
|
|
"ms": int64(Millisecond),
|
|
|
|
"s": int64(Second),
|
|
|
|
"m": int64(Minute),
|
|
|
|
"h": int64(Hour),
|
2011-12-22 22:28:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseDuration parses a duration string.
|
|
|
|
// A duration string is a possibly signed sequence of
|
|
|
|
// decimal numbers, each with optional fraction and a unit suffix,
|
|
|
|
// such as "300ms", "-1.5h" or "2h45m".
|
|
|
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
|
|
func ParseDuration(s string) (Duration, error) {
|
|
|
|
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
|
|
|
|
orig := s
|
2015-01-07 11:56:06 -07:00
|
|
|
var d int64
|
2011-12-22 22:28:56 -07:00
|
|
|
neg := false
|
|
|
|
|
|
|
|
// Consume [-+]?
|
|
|
|
if s != "" {
|
|
|
|
c := s[0]
|
|
|
|
if c == '-' || c == '+' {
|
|
|
|
neg = c == '-'
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Special case: if all that is left is "0", this is zero.
|
|
|
|
if s == "0" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if s == "" {
|
|
|
|
return 0, errors.New("time: invalid duration " + orig)
|
|
|
|
}
|
|
|
|
for s != "" {
|
2015-01-07 11:56:06 -07:00
|
|
|
var (
|
|
|
|
v, f int64 // integers before, after decimal point
|
|
|
|
scale float64 = 1 // value = v + f/scale
|
|
|
|
)
|
2011-12-22 22:28:56 -07:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// The next character must be [0-9.]
|
2015-01-07 11:56:06 -07:00
|
|
|
if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') {
|
2011-12-22 22:28:56 -07:00
|
|
|
return 0, errors.New("time: invalid duration " + orig)
|
|
|
|
}
|
|
|
|
// Consume [0-9]*
|
|
|
|
pl := len(s)
|
2015-01-07 11:56:06 -07:00
|
|
|
v, s, err = leadingInt(s)
|
2011-12-22 22:28:56 -07:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("time: invalid duration " + orig)
|
|
|
|
}
|
|
|
|
pre := pl != len(s) // whether we consumed anything before a period
|
|
|
|
|
|
|
|
// Consume (\.[0-9]*)?
|
|
|
|
post := false
|
|
|
|
if s != "" && s[0] == '.' {
|
|
|
|
s = s[1:]
|
|
|
|
pl := len(s)
|
2016-09-17 22:35:42 -06:00
|
|
|
f, scale, s = leadingFraction(s)
|
2011-12-22 22:28:56 -07:00
|
|
|
post = pl != len(s)
|
|
|
|
}
|
|
|
|
if !pre && !post {
|
|
|
|
// no digits (e.g. ".s" or "-.s")
|
|
|
|
return 0, errors.New("time: invalid duration " + orig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume unit.
|
|
|
|
i := 0
|
|
|
|
for ; i < len(s); i++ {
|
|
|
|
c := s[i]
|
2015-01-07 11:56:06 -07:00
|
|
|
if c == '.' || '0' <= c && c <= '9' {
|
2011-12-22 22:28:56 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
return 0, errors.New("time: missing unit in duration " + orig)
|
|
|
|
}
|
|
|
|
u := s[:i]
|
|
|
|
s = s[i:]
|
|
|
|
unit, ok := unitMap[u]
|
|
|
|
if !ok {
|
|
|
|
return 0, errors.New("time: unknown unit " + u + " in duration " + orig)
|
|
|
|
}
|
2015-01-07 11:56:06 -07:00
|
|
|
if v > (1<<63-1)/unit {
|
|
|
|
// overflow
|
|
|
|
return 0, errors.New("time: invalid duration " + orig)
|
|
|
|
}
|
|
|
|
v *= unit
|
|
|
|
if f > 0 {
|
|
|
|
// float64 is needed to be nanosecond accurate for fractions of hours.
|
|
|
|
// v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit)
|
|
|
|
v += int64(float64(f) * (float64(unit) / scale))
|
|
|
|
if v < 0 {
|
|
|
|
// overflow
|
|
|
|
return 0, errors.New("time: invalid duration " + orig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d += v
|
|
|
|
if d < 0 {
|
|
|
|
// overflow
|
|
|
|
return 0, errors.New("time: invalid duration " + orig)
|
|
|
|
}
|
2011-12-22 22:28:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if neg {
|
2015-01-07 11:56:06 -07:00
|
|
|
d = -d
|
2014-03-10 10:33:45 -06:00
|
|
|
}
|
2015-01-07 11:56:06 -07:00
|
|
|
return Duration(d), nil
|
2011-12-22 22:28:56 -07:00
|
|
|
}
|