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.
9 // These are predefined layouts for use in Time.Format.
10 // The standard time used in the layouts is:
11 // Mon Jan 2 15:04:05 MST 2006
12 // which is Unix time 1136239445. Since MST is GMT-0700,
13 // the standard time can be thought of as
14 // 01/02 03:04:05PM '06 -0700
15 // To define your own format, write down what the standard time would look
16 // like formatted your way; see the values of constants like ANSIC,
17 // StampMicro or Kitchen for examples.
19 // Within the format string, an underscore _ represents a space that may be
20 // replaced by a digit if the following number (a day) has two digits; for
21 // compatibility with fixed-width Unix time formats.
23 // A decimal point followed by one or more zeros represents a fractional
24 // second, printed to the given number of decimal places. A decimal point
25 // followed by one or more nines represents a fractional second, printed to
26 // the given number of decimal places, with trailing zeros removed.
27 // When parsing (only), the input may contain a fractional second
28 // field immediately after the seconds field, even if the layout does not
29 // signify its presence. In that case a decimal point followed by a maximal
30 // series of digits is parsed as a fractional second.
32 // Numeric time zone offsets format as follows:
35 // Replacing the sign in the format with a Z triggers
36 // the ISO 8601 behavior of printing Z instead of an
37 // offset for the UTC zone. Thus:
41 ANSIC = "Mon Jan _2 15:04:05 2006"
42 UnixDate = "Mon Jan _2 15:04:05 MST 2006"
43 RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
44 RFC822 = "02 Jan 06 15:04 MST"
45 RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
46 RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
47 RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
48 RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
49 RFC3339 = "2006-01-02T15:04:05Z07:00"
50 RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
53 Stamp = "Jan _2 15:04:05"
54 StampMilli = "Jan _2 15:04:05.000"
55 StampMicro = "Jan _2 15:04:05.000000"
56 StampNano = "Jan _2 15:04:05.000000000"
61 stdLongMonth = iota + stdNeedDate // "January"
65 stdLongWeekDay // "Monday"
70 stdHour = iota + stdNeedClock // "15"
77 stdLongYear = iota + stdNeedDate // "2006"
79 stdPM = iota + stdNeedClock // "PM"
82 stdISO8601TZ // "Z0700" // prints Z for UTC
83 stdISO8601ColonTZ // "Z07:00" // prints Z for UTC
84 stdNumTZ // "-0700" // always numeric
85 stdNumShortTZ // "-07" // always numeric
86 stdNumColonTZ // "-07:00" // always numeric
87 stdFracSecond0 // ".0", ".00", ... , trailing zeros included
88 stdFracSecond9 // ".9", ".99", ..., trailing zeros omitted
90 stdNeedDate = 1 << 8 // need month, day, year
91 stdNeedClock = 2 << 8 // need hour, minute, second
92 stdArgShift = 16 // extra argument in high bits, above low stdArgShift
93 stdMask = 1<<stdArgShift - 1 // mask out argument
96 // std0x records the std values for "01", "02", ..., "06".
97 var std0x = [...]int{stdZeroMonth, stdZeroDay, stdZeroHour12, stdZeroMinute, stdZeroSecond, stdYear}
99 // nextStdChunk finds the first occurrence of a std string in
100 // layout and returns the text before, the std string, and the text after.
101 func nextStdChunk(layout string) (prefix string, std int, suffix string) {
102 for i := 0; i < len(layout); i++ {
103 switch c := int(layout[i]); c {
104 case 'J': // January, Jan
105 if len(layout) >= i+3 && layout[i:i+3] == "Jan" {
106 if len(layout) >= i+7 && layout[i:i+7] == "January" {
107 return layout[0:i], stdLongMonth, layout[i+7:]
109 return layout[0:i], stdMonth, layout[i+3:]
112 case 'M': // Monday, Mon, MST
113 if len(layout) >= i+3 {
114 if layout[i:i+3] == "Mon" {
115 if len(layout) >= i+6 && layout[i:i+6] == "Monday" {
116 return layout[0:i], stdLongWeekDay, layout[i+6:]
118 return layout[0:i], stdWeekDay, layout[i+3:]
120 if layout[i:i+3] == "MST" {
121 return layout[0:i], stdTZ, layout[i+3:]
125 case '0': // 01, 02, 03, 04, 05, 06
126 if len(layout) >= i+2 && '1' <= layout[i+1] && layout[i+1] <= '6' {
127 return layout[0:i], std0x[layout[i+1]-'1'], layout[i+2:]
131 if len(layout) >= i+2 && layout[i+1] == '5' {
132 return layout[0:i], stdHour, layout[i+2:]
134 return layout[0:i], stdNumMonth, layout[i+1:]
137 if len(layout) >= i+4 && layout[i:i+4] == "2006" {
138 return layout[0:i], stdLongYear, layout[i+4:]
140 return layout[0:i], stdDay, layout[i+1:]
143 if len(layout) >= i+2 && layout[i+1] == '2' {
144 return layout[0:i], stdUnderDay, layout[i+2:]
148 return layout[0:i], stdHour12, layout[i+1:]
151 return layout[0:i], stdMinute, layout[i+1:]
154 return layout[0:i], stdSecond, layout[i+1:]
157 if len(layout) >= i+2 && layout[i+1] == 'M' {
158 return layout[0:i], stdPM, layout[i+2:]
162 if len(layout) >= i+2 && layout[i+1] == 'm' {
163 return layout[0:i], stdpm, layout[i+2:]
166 case '-': // -0700, -07:00, -07
167 if len(layout) >= i+5 && layout[i:i+5] == "-0700" {
168 return layout[0:i], stdNumTZ, layout[i+5:]
170 if len(layout) >= i+6 && layout[i:i+6] == "-07:00" {
171 return layout[0:i], stdNumColonTZ, layout[i+6:]
173 if len(layout) >= i+3 && layout[i:i+3] == "-07" {
174 return layout[0:i], stdNumShortTZ, layout[i+3:]
176 case 'Z': // Z0700, Z07:00
177 if len(layout) >= i+5 && layout[i:i+5] == "Z0700" {
178 return layout[0:i], stdISO8601TZ, layout[i+5:]
180 if len(layout) >= i+6 && layout[i:i+6] == "Z07:00" {
181 return layout[0:i], stdISO8601ColonTZ, layout[i+6:]
183 case '.': // .000 or .999 - repeated digits for fractional seconds.
184 if i+1 < len(layout) && (layout[i+1] == '0' || layout[i+1] == '9') {
187 for j < len(layout) && layout[j] == ch {
190 // String of digits must end here - only fractional second is all digits.
191 if !isDigit(layout, j) {
192 std := stdFracSecond0
193 if layout[i+1] == '9' {
196 std |= (j - (i + 1)) << stdArgShift
197 return layout[0:i], std, layout[j:]
205 var longDayNames = []string{
215 var shortDayNames = []string{
225 var shortMonthNames = []string{
241 var longMonthNames = []string{
257 // match returns true if s1 and s2 match ignoring case.
258 // It is assumed s1 and s2 are the same length.
259 func match(s1, s2 string) bool {
260 for i := 0; i < len(s1); i++ {
264 // Switch to lower-case; 'a'-'A' is known to be a single bit.
267 if c1 != c2 || c1 < 'a' || c1 > 'z' {
275 func lookup(tab []string, val string) (int, string, error) {
276 for i, v := range tab {
277 if len(val) >= len(v) && match(val[0:len(v)], v) {
278 return i, val[len(v):], nil
281 return -1, val, errBad
284 // appendUint appends the decimal form of x to b and returns the result.
285 // If x is a single-digit number and pad != 0, appendUint inserts the pad byte
287 // Duplicates functionality in strconv, but avoids dependency.
288 func appendUint(b []byte, x uint, pad byte) []byte {
293 return append(b, byte('0'+x))
296 b = append(b, byte('0'+x/10))
297 b = append(b, byte('0'+x%10))
304 return append(b, '0')
308 buf[n] = byte(x%10 + '0')
312 buf[n] = byte(x + '0')
313 return append(b, buf[n:]...)
316 // Never printed, just needs to be non-nil for return by atoi.
317 var atoiError = errors.New("time: invalid number")
319 // Duplicates functionality in strconv, but avoids dependency.
320 func atoi(s string) (x int, err error) {
322 if s != "" && s[0] == '-' {
326 q, rem, err := leadingInt(s)
328 if err != nil || rem != "" {
337 // formatNano appends a fractional second, as nanoseconds, to b
338 // and returns the result.
339 func formatNano(b []byte, nanosec uint, n int, trim bool) []byte {
342 for start := len(buf); start > 0; {
344 buf[start] = byte(u%10 + '0')
352 for n > 0 && buf[n-1] == '0' {
360 return append(b, buf[:n]...)
363 // String returns the time formatted using the format string
364 // "2006-01-02 15:04:05.999999999 -0700 MST"
365 func (t Time) String() string {
366 return t.Format("2006-01-02 15:04:05.999999999 -0700 MST")
369 // Format returns a textual representation of the time value formatted
370 // according to layout. The layout defines the format by showing the
371 // representation of the standard time,
372 // Mon Jan 2 15:04:05 -0700 MST 2006
373 // which is then used to describe the time to be formatted. Predefined
374 // layouts ANSIC, UnixDate, RFC3339 and others describe standard
375 // representations. For more information about the formats and the
376 // definition of the standard time, see the documentation for ANSIC.
377 func (t Time) Format(layout string) string {
379 name, offset, abs = t.locabs()
391 max := len(layout) + 10
395 b = make([]byte, 0, max)
397 // Each iteration generates one std value.
399 prefix, std, suffix := nextStdChunk(layout)
401 b = append(b, prefix...)
408 // Compute year, month, day if needed.
409 if year < 0 && std&stdNeedDate != 0 {
410 year, month, day, _ = absDate(abs, true)
413 // Compute hour, minute, second if needed.
414 if hour < 0 && std&stdNeedClock != 0 {
415 hour, min, sec = absClock(abs)
418 switch std & stdMask {
424 b = appendUint(b, uint(y%100), '0')
426 // Pad year to at least 4 digits.
433 b = append(b, "-0"...)
436 b = append(b, "-00"...)
439 b = append(b, "-000"...)
442 b = append(b, "000"...)
444 b = append(b, "00"...)
448 b = appendUint(b, uint(y), 0)
450 b = append(b, month.String()[:3]...)
455 b = appendUint(b, uint(month), 0)
457 b = appendUint(b, uint(month), '0')
459 b = append(b, absWeekday(abs).String()[:3]...)
461 s := absWeekday(abs).String()
464 b = appendUint(b, uint(day), 0)
466 b = appendUint(b, uint(day), ' ')
468 b = appendUint(b, uint(day), '0')
470 b = appendUint(b, uint(hour), '0')
472 // Noon is 12PM, midnight is 12AM.
477 b = appendUint(b, uint(hr), 0)
479 // Noon is 12PM, midnight is 12AM.
484 b = appendUint(b, uint(hr), '0')
486 b = appendUint(b, uint(min), 0)
488 b = appendUint(b, uint(min), '0')
490 b = appendUint(b, uint(sec), 0)
492 b = appendUint(b, uint(sec), '0')
495 b = append(b, "PM"...)
497 b = append(b, "AM"...)
501 b = append(b, "pm"...)
503 b = append(b, "am"...)
505 case stdISO8601TZ, stdISO8601ColonTZ, stdNumTZ, stdNumColonTZ:
506 // Ugly special case. We cheat and take the "Z" variants
507 // to mean "the time zone as formatted for ISO 8601".
508 if offset == 0 && (std == stdISO8601TZ || std == stdISO8601ColonTZ) {
512 zone := offset / 60 // convert to minutes
519 b = appendUint(b, uint(zone/60), '0')
520 if std == stdISO8601ColonTZ || std == stdNumColonTZ {
523 b = appendUint(b, uint(zone%60), '0')
526 b = append(b, name...)
529 // No time zone known for this time, but we must print one.
530 // Use the -0700 format.
531 zone := offset / 60 // convert to minutes
538 b = appendUint(b, uint(zone/60), '0')
539 b = appendUint(b, uint(zone%60), '0')
540 case stdFracSecond0, stdFracSecond9:
541 b = formatNano(b, uint(t.Nanosecond()), std>>stdArgShift, std&stdMask == stdFracSecond9)
547 var errBad = errors.New("bad value for field") // placeholder not passed to user
549 // ParseError describes a problem parsing a time string.
550 type ParseError struct {
558 func quote(s string) string {
559 return "\"" + s + "\""
562 // Error returns the string representation of a ParseError.
563 func (e *ParseError) Error() string {
565 return "parsing time " +
566 quote(e.Value) + " as " +
567 quote(e.Layout) + ": cannot parse " +
568 quote(e.ValueElem) + " as " +
571 return "parsing time " +
572 quote(e.Value) + e.Message
575 // isDigit returns true if s[i] is a decimal digit, false if not or
576 // if s[i] is out of range.
577 func isDigit(s string, i int) bool {
582 return '0' <= c && c <= '9'
585 // getnum parses s[0:1] or s[0:2] (fixed forces the latter)
586 // as a decimal integer and returns the integer and the
587 // remainder of the string.
588 func getnum(s string, fixed bool) (int, string, error) {
596 return int(s[0] - '0'), s[1:], nil
598 return int(s[0]-'0')*10 + int(s[1]-'0'), s[2:], nil
601 func cutspace(s string) string {
602 for len(s) > 0 && s[0] == ' ' {
608 // skip removes the given prefix from value,
609 // treating runs of space characters as equivalent.
610 func skip(value, prefix string) (string, error) {
611 for len(prefix) > 0 {
612 if prefix[0] == ' ' {
613 if len(value) > 0 && value[0] != ' ' {
616 prefix = cutspace(prefix)
617 value = cutspace(value)
620 if len(value) == 0 || value[0] != prefix[0] {
629 // Parse parses a formatted string and returns the time value it represents.
630 // The layout defines the format by showing the representation of the
632 // Mon Jan 2 15:04:05 -0700 MST 2006
633 // which is then used to describe the string to be parsed. Predefined layouts
634 // ANSIC, UnixDate, RFC3339 and others describe standard representations. For
635 // more information about the formats and the definition of the standard
636 // time, see the documentation for ANSIC.
638 // Elements omitted from the value are assumed to be zero or, when
639 // zero is impossible, one, so parsing "3:04pm" returns the time
640 // corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is
641 // 0, this time is before the zero Time).
642 // Years must be in the range 0000..9999. The day of the week is checked
643 // for syntax but it is otherwise ignored.
644 func Parse(layout, value string) (Time, error) {
645 alayout, avalue := layout, value
646 rangeErrString := "" // set if a value is out of range
647 amSet := false // do we need to subtract 12 from the hour for midnight?
648 pmSet := false // do we need to add 12 to the hour?
650 // Time being constructed.
653 month int = 1 // January
664 // Each iteration processes one std value.
667 prefix, std, suffix := nextStdChunk(layout)
668 stdstr := layout[len(prefix) : len(layout)-len(suffix)]
669 value, err = skip(value, prefix)
671 return Time{}, &ParseError{alayout, avalue, prefix, value, ""}
675 return Time{}, &ParseError{alayout, avalue, "", value, ": extra text: " + value}
681 switch std & stdMask {
687 p, value = value[0:2], value[2:]
689 if year >= 69 { // Unix time starts Dec 31 1969 in some time zones
695 if len(value) < 4 || !isDigit(value, 0) {
699 p, value = value[0:4], value[4:]
702 month, value, err = lookup(shortMonthNames, value)
704 month, value, err = lookup(longMonthNames, value)
705 case stdNumMonth, stdZeroMonth:
706 month, value, err = getnum(value, std == stdZeroMonth)
707 if month <= 0 || 12 < month {
708 rangeErrString = "month"
711 // Ignore weekday except for error checking.
712 _, value, err = lookup(shortDayNames, value)
714 _, value, err = lookup(longDayNames, value)
715 case stdDay, stdUnderDay, stdZeroDay:
716 if std == stdUnderDay && len(value) > 0 && value[0] == ' ' {
719 day, value, err = getnum(value, std == stdZeroDay)
720 if day < 0 || 31 < day {
721 rangeErrString = "day"
724 hour, value, err = getnum(value, false)
725 if hour < 0 || 24 <= hour {
726 rangeErrString = "hour"
728 case stdHour12, stdZeroHour12:
729 hour, value, err = getnum(value, std == stdZeroHour12)
730 if hour < 0 || 12 < hour {
731 rangeErrString = "hour"
733 case stdMinute, stdZeroMinute:
734 min, value, err = getnum(value, std == stdZeroMinute)
735 if min < 0 || 60 <= min {
736 rangeErrString = "minute"
738 case stdSecond, stdZeroSecond:
739 sec, value, err = getnum(value, std == stdZeroSecond)
740 if sec < 0 || 60 <= sec {
741 rangeErrString = "second"
743 // Special case: do we have a fractional second but no
744 // fractional second in the format?
745 if len(value) >= 2 && value[0] == '.' && isDigit(value, 1) {
746 _, std, _ := nextStdChunk(layout)
748 if std == stdFracSecond0 || std == stdFracSecond9 {
749 // Fractional second in the layout; proceed normally
752 // No fractional second in the layout but we have one in the input.
754 for ; n < len(value) && isDigit(value, n); n++ {
756 nsec, rangeErrString, err = parseNanoseconds(value, n)
764 p, value = value[0:2], value[2:]
778 p, value = value[0:2], value[2:]
787 case stdISO8601TZ, stdISO8601ColonTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ:
788 if (std == stdISO8601TZ || std == stdISO8601ColonTZ) && len(value) >= 1 && value[0] == 'Z' {
793 var sign, hour, min string
794 if std == stdISO8601ColonTZ || std == stdNumColonTZ {
803 sign, hour, min, value = value[0:1], value[1:3], value[4:6], value[6:]
804 } else if std == stdNumShortTZ {
809 sign, hour, min, value = value[0:1], value[1:3], "00", value[3:]
815 sign, hour, min, value = value[0:1], value[1:3], value[3:5], value[5:]
822 zoneOffset = (hr*60 + mm) * 60 // offset is in seconds
826 zoneOffset = -zoneOffset
831 // Does it look like a time zone?
832 if len(value) >= 3 && value[0:3] == "UTC" {
838 if len(value) >= 3 && value[2] == 'T' {
839 p, value = value[0:3], value[3:]
840 } else if len(value) >= 4 && value[3] == 'T' {
841 p, value = value[0:4], value[4:]
846 for i := 0; i < len(p); i++ {
847 if p[i] < 'A' || 'Z' < p[i] {
854 // It's a valid format.
858 // stdFracSecond0 requires the exact number of digits as specified in
860 ndigit := 1 + (std >> stdArgShift)
861 if len(value) < ndigit {
865 nsec, rangeErrString, err = parseNanoseconds(value, ndigit)
866 value = value[ndigit:]
869 if len(value) < 2 || value[0] != '.' || value[1] < '0' || '9' < value[1] {
870 // Fractional second omitted.
873 // Take any number of digits, even more than asked for,
874 // because it is what the stdSecond case would do.
876 for i < 9 && i+1 < len(value) && '0' <= value[i+1] && value[i+1] <= '9' {
879 nsec, rangeErrString, err = parseNanoseconds(value, 1+i)
882 if rangeErrString != "" {
883 return Time{}, &ParseError{alayout, avalue, stdstr, value, ": " + rangeErrString + " out of range"}
886 return Time{}, &ParseError{alayout, avalue, stdstr, value, ""}
889 if pmSet && hour < 12 {
891 } else if amSet && hour == 12 {
895 // TODO: be more aggressive checking day?
897 return Date(year, Month(month), day, hour, min, sec, nsec, z), nil
900 t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
901 if zoneOffset != -1 {
902 t.sec -= int64(zoneOffset)
904 // Look for local zone with the given offset.
905 // If that zone was in effect at the given time, use it.
906 name, offset, _, _, _ := Local.lookup(t.sec + internalToUnix)
907 if offset == zoneOffset && (zoneName == "" || name == zoneName) {
912 // Otherwise create fake zone to record offset.
913 t.loc = FixedZone(zoneName, zoneOffset)
918 // Look for local zone with the given offset.
919 // If that zone was in effect at the given time, use it.
920 offset, _, ok := Local.lookupName(zoneName)
922 name, off, _, _, _ := Local.lookup(t.sec + internalToUnix - int64(offset))
923 if name == zoneName && off == offset {
924 t.sec -= int64(offset)
930 // Otherwise, create fake zone with unknown offset.
931 t.loc = FixedZone(zoneName, 0)
935 // Otherwise, fall back to UTC.
939 func parseNanoseconds(value string, nbytes int) (ns int, rangeErrString string, err error) {
944 if ns, err = atoi(value[1:nbytes]); err != nil {
947 if ns < 0 || 1e9 <= ns {
948 rangeErrString = "fractional second"
951 // We need nanoseconds, which means scaling by the number
952 // of missing digits in the format, maximum length 10. If it's
953 // longer than 10, we won't scale.
954 scaleDigits := 10 - nbytes
955 for i := 0; i < scaleDigits; i++ {
961 var errLeadingInt = errors.New("time: bad [0-9]*") // never printed
963 // leadingInt consumes the leading [0-9]* from s.
964 func leadingInt(s string) (x int64, rem string, err error) {
966 for ; i < len(s); i++ {
968 if c < '0' || c > '9' {
971 if x >= (1<<63-10)/10 {
973 return 0, "", errLeadingInt
975 x = x*10 + int64(c) - '0'
980 var unitMap = map[string]float64{
981 "ns": float64(Nanosecond),
982 "us": float64(Microsecond),
983 "µs": float64(Microsecond), // U+00B5 = micro symbol
984 "μs": float64(Microsecond), // U+03BC = Greek letter mu
985 "ms": float64(Millisecond),
986 "s": float64(Second),
987 "m": float64(Minute),
991 // ParseDuration parses a duration string.
992 // A duration string is a possibly signed sequence of
993 // decimal numbers, each with optional fraction and a unit suffix,
994 // such as "300ms", "-1.5h" or "2h45m".
995 // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
996 func ParseDuration(s string) (Duration, error) {
997 // [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
1005 if c == '-' || c == '+' {
1010 // Special case: if all that is left is "0", this is zero.
1015 return 0, errors.New("time: invalid duration " + orig)
1018 g := float64(0) // this element of the sequence
1023 // The next character must be [0-9.]
1024 if !(s[0] == '.' || ('0' <= s[0] && s[0] <= '9')) {
1025 return 0, errors.New("time: invalid duration " + orig)
1029 x, s, err = leadingInt(s)
1031 return 0, errors.New("time: invalid duration " + orig)
1034 pre := pl != len(s) // whether we consumed anything before a period
1036 // Consume (\.[0-9]*)?
1038 if s != "" && s[0] == '.' {
1041 x, s, err = leadingInt(s)
1043 return 0, errors.New("time: invalid duration " + orig)
1046 for n := pl - len(s); n > 0; n-- {
1049 g += float64(x) / float64(scale)
1053 // no digits (e.g. ".s" or "-.s")
1054 return 0, errors.New("time: invalid duration " + orig)
1059 for ; i < len(s); i++ {
1061 if c == '.' || ('0' <= c && c <= '9') {
1066 return 0, errors.New("time: missing unit in duration " + orig)
1070 unit, ok := unitMap[u]
1072 return 0, errors.New("time: unknown unit " + u + " in duration " + orig)
1081 return Duration(f), nil