17 // These are predefined layouts for use in Time.Format.
18 // The standard time used in the layouts is:
19 // Mon Jan 2 15:04:05 MST 2006 (MST is GMT-0700)
20 // which is Unix time 1136243045.
21 // (Think of it as 01/02 03:04:05PM '06 -0700.)
22 // An underscore _ represents a space that
23 // may be replaced by a digit if the following number
24 // (a day) has two digits; for compatibility with
25 // fixed-width Unix time formats.
27 // Numeric time zone offsets format as follows:
30 // Replacing the sign in the format with a Z triggers
31 // the ISO 8601 behavior of printing Z instead of an
32 // offset for the UTC zone. Thus:
36 ANSIC = "Mon Jan _2 15:04:05 2006"
37 UnixDate = "Mon Jan _2 15:04:05 MST 2006"
38 RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
39 RFC822 = "02 Jan 06 1504 MST"
40 // RFC822 with Zulu time.
41 RFC822Z = "02 Jan 06 1504 -0700"
42 RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
43 RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
45 RFC3339 = "2006-01-02T15:04:05Z07:00"
49 stdLongMonth = "January"
53 stdLongWeekDay = "Monday"
70 stdISO8601TZ = "Z0700" // prints Z for UTC
71 stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
72 stdNumTZ = "-0700" // always numeric
73 stdNumShortTZ = "-07" // always numeric
74 stdNumColonTZ = "-07:00" // always numeric
77 // nextStdChunk finds the first occurrence of a std string in
78 // layout and returns the text before, the std string, and the text after.
79 func nextStdChunk(layout string) (prefix, std, suffix string) {
80 for i := 0; i < len(layout); i++ {
82 case 'J': // January, Jan
83 if len(layout) >= i+7 && layout[i:i+7] == stdLongMonth {
84 return layout[0:i], stdLongMonth, layout[i+7:]
86 if len(layout) >= i+3 && layout[i:i+3] == stdMonth {
87 return layout[0:i], stdMonth, layout[i+3:]
90 case 'M': // Monday, Mon, MST
91 if len(layout) >= i+6 && layout[i:i+6] == stdLongWeekDay {
92 return layout[0:i], stdLongWeekDay, layout[i+6:]
94 if len(layout) >= i+3 {
95 if layout[i:i+3] == stdWeekDay {
96 return layout[0:i], stdWeekDay, layout[i+3:]
98 if layout[i:i+3] == stdTZ {
99 return layout[0:i], stdTZ, layout[i+3:]
103 case '0': // 01, 02, 03, 04, 05, 06
104 if len(layout) >= i+2 && '1' <= layout[i+1] && layout[i+1] <= '6' {
105 return layout[0:i], layout[i : i+2], layout[i+2:]
109 if len(layout) >= i+2 && layout[i+1] == '5' {
110 return layout[0:i], stdHour, layout[i+2:]
112 return layout[0:i], stdNumMonth, layout[i+1:]
115 if len(layout) >= i+4 && layout[i:i+4] == stdLongYear {
116 return layout[0:i], stdLongYear, layout[i+4:]
118 return layout[0:i], stdDay, layout[i+1:]
121 if len(layout) >= i+2 && layout[i+1] == '2' {
122 return layout[0:i], stdUnderDay, layout[i+2:]
125 case '3', '4', '5': // 3, 4, 5
126 return layout[0:i], layout[i : i+1], layout[i+1:]
129 if len(layout) >= i+2 && layout[i+1] == 'M' {
130 return layout[0:i], layout[i : i+2], layout[i+2:]
134 if len(layout) >= i+2 && layout[i+1] == 'm' {
135 return layout[0:i], layout[i : i+2], layout[i+2:]
138 case '-': // -0700, -07:00, -07
139 if len(layout) >= i+5 && layout[i:i+5] == stdNumTZ {
140 return layout[0:i], layout[i : i+5], layout[i+5:]
142 if len(layout) >= i+6 && layout[i:i+6] == stdNumColonTZ {
143 return layout[0:i], layout[i : i+6], layout[i+6:]
145 if len(layout) >= i+3 && layout[i:i+3] == stdNumShortTZ {
146 return layout[0:i], layout[i : i+3], layout[i+3:]
148 case 'Z': // Z0700, Z07:00
149 if len(layout) >= i+5 && layout[i:i+5] == stdISO8601TZ {
150 return layout[0:i], layout[i : i+5], layout[i+5:]
152 if len(layout) >= i+6 && layout[i:i+6] == stdISO8601ColonTZ {
153 return layout[0:i], layout[i : i+6], layout[i+6:]
157 return layout, "", ""
160 var longDayNames = []string{
170 var shortDayNames = []string{
180 var shortMonthNames = []string{
196 var longMonthNames = []string{
212 func lookup(tab []string, val string) (int, string, os.Error) {
213 for i, v := range tab {
214 if len(val) >= len(v) && val[0:len(v)] == v {
215 return i, val[len(v):], nil
218 return -1, val, errBad
221 func pad(i int, padding string) string {
229 func zeroPad(i int) string { return pad(i, "0") }
231 // Format returns a textual representation of the time value formatted
232 // according to layout. The layout defines the format by showing the
233 // representation of a standard time, which is then used to describe
234 // the time to be formatted. Predefined layouts ANSIC, UnixDate,
235 // RFC3339 and others describe standard representations.
236 func (t *Time) Format(layout string) string {
237 b := new(bytes.Buffer)
238 // Each iteration generates one std value.
240 prefix, std, suffix := nextStdChunk(layout)
241 b.WriteString(prefix)
248 p = strconv.Itoa64(t.Year % 100)
250 p = strconv.Itoa64(t.Year)
252 p = shortMonthNames[t.Month]
254 p = longMonthNames[t.Month]
256 p = strconv.Itoa(t.Month)
260 p = shortDayNames[t.Weekday]
262 p = longDayNames[t.Weekday]
264 p = strconv.Itoa(t.Day)
272 p = strconv.Itoa(t.Hour % 12)
274 p = zeroPad(t.Hour % 12)
276 p = strconv.Itoa(t.Minute)
278 p = zeroPad(t.Minute)
280 p = strconv.Itoa(t.Second)
282 p = zeroPad(t.Second)
283 case stdISO8601TZ, stdISO8601ColonTZ, stdNumTZ, stdNumColonTZ:
284 // Ugly special case. We cheat and take the "Z" variants
285 // to mean "the time zone as formatted for ISO 8601".
286 if t.ZoneOffset == 0 && std[0] == 'Z' {
290 zone := t.ZoneOffset / 60 // convert to minutes
297 p += zeroPad(zone / 60)
298 if std == stdISO8601ColonTZ || std == stdNumColonTZ {
301 p += zeroPad(zone % 60)
318 // No time zone known for this time, but we must print one.
319 // Use the -0700 format.
320 zone := t.ZoneOffset / 60 // convert to minutes
327 p += zeroPad(zone / 60)
328 p += zeroPad(zone % 60)
337 // String returns a Unix-style representation of the time value.
338 func (t *Time) String() string {
342 return t.Format(UnixDate)
345 var errBad = os.ErrorString("bad") // just a marker; not returned to user
347 // ParseError describes a problem parsing a time string.
348 type ParseError struct {
356 // String is the string representation of a ParseError.
357 func (e *ParseError) String() string {
359 return "parsing time " +
360 strconv.Quote(e.Value) + " as " +
361 strconv.Quote(e.Layout) + ": cannot parse " +
362 strconv.Quote(e.ValueElem) + " as " +
363 strconv.Quote(e.LayoutElem)
365 return "parsing time " +
366 strconv.Quote(e.Value) + e.Message
369 // getnum parses s[0:1] or s[0:2] (fixed forces the latter)
370 // as a decimal integer and returns the integer and the
371 // remainder of the string.
372 func getnum(s string, fixed bool) (int, string, os.Error) {
373 if len(s) == 0 || s[0] < '0' || s[0] > '9' {
376 if len(s) == 1 || s[1] < '0' || s[1] > '9' {
380 return int(s[0] - '0'), s[1:], nil
382 return int(s[0]-'0')*10 + int(s[1]-'0'), s[2:], nil
385 func cutspace(s string) string {
386 for len(s) > 0 && s[0] == ' ' {
392 // skip removes the given prefix from value,
393 // treating runs of space characters as equivalent.
394 func skip(value, prefix string) (string, os.Error) {
395 for len(prefix) > 0 {
396 if prefix[0] == ' ' {
397 if len(value) > 0 && value[0] != ' ' {
400 prefix = cutspace(prefix)
401 value = cutspace(value)
404 if len(value) == 0 || value[0] != prefix[0] {
413 // Parse parses a formatted string and returns the time value it represents.
414 // The layout defines the format by showing the representation of a standard
415 // time, which is then used to describe the string to be parsed. Predefined
416 // layouts ANSIC, UnixDate, RFC3339 and others describe standard
419 // Only those elements present in the value will be set in the returned time
420 // structure. Also, if the input string represents an inconsistent time
421 // (such as having the wrong day of the week), the returned value will also
422 // be inconsistent. In any case, the elements of the returned time will be
423 // sane: hours in 0..23, minutes in 0..59, day of month in 0..31, etc.
424 // Years must be in the range 0000..9999.
425 func Parse(alayout, avalue string) (*Time, os.Error) {
427 rangeErrString := "" // set if a value is out of range
428 pmSet := false // do we need to add 12 to the hour?
429 layout, value := alayout, avalue
430 // Each iteration processes one std value.
433 prefix, std, suffix := nextStdChunk(layout)
434 value, err = skip(value, prefix)
436 return nil, &ParseError{alayout, avalue, prefix, value, ""}
440 return nil, &ParseError{alayout, avalue, "", value, ": extra text: " + value}
452 p, value = value[0:2], value[2:]
453 t.Year, err = strconv.Atoi64(p)
454 if t.Year >= 69 { // Unix time starts Dec 31 1969 in some time zones
460 if len(value) < 4 || value[0] < '0' || value[0] > '9' {
464 p, value = value[0:4], value[4:]
465 t.Year, err = strconv.Atoi64(p)
467 t.Month, value, err = lookup(shortMonthNames, value)
469 t.Month, value, err = lookup(longMonthNames, value)
470 case stdNumMonth, stdZeroMonth:
471 t.Month, value, err = getnum(value, std == stdZeroMonth)
472 if t.Month <= 0 || 12 < t.Month {
473 rangeErrString = "month"
476 t.Weekday, value, err = lookup(shortDayNames, value)
478 t.Weekday, value, err = lookup(longDayNames, value)
479 case stdDay, stdUnderDay, stdZeroDay:
480 if std == stdUnderDay && len(value) > 0 && value[0] == ' ' {
483 t.Day, value, err = getnum(value, std == stdZeroDay)
484 if t.Day < 0 || 31 < t.Day {
485 // TODO: be more thorough in date check?
486 rangeErrString = "day"
489 t.Hour, value, err = getnum(value, false)
490 if t.Hour < 0 || 24 <= t.Hour {
491 rangeErrString = "hour"
493 case stdHour12, stdZeroHour12:
494 t.Hour, value, err = getnum(value, std == stdZeroHour12)
495 if t.Hour < 0 || 12 < t.Hour {
496 rangeErrString = "hour"
498 case stdMinute, stdZeroMinute:
499 t.Minute, value, err = getnum(value, std == stdZeroMinute)
500 if t.Minute < 0 || 60 <= t.Minute {
501 rangeErrString = "minute"
503 case stdSecond, stdZeroSecond:
504 t.Second, value, err = getnum(value, std == stdZeroSecond)
505 if t.Second < 0 || 60 <= t.Second {
506 rangeErrString = "second"
508 case stdISO8601TZ, stdISO8601ColonTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ:
509 if std[0] == 'Z' && len(value) >= 1 && value[0] == 'Z' {
514 var sign, hh, mm string
515 if std == stdISO8601ColonTZ || std == stdNumColonTZ {
524 sign, hh, mm, value = value[0:1], value[1:3], value[4:6], value[6:]
525 } else if std == stdNumShortTZ {
530 sign, hh, mm, value = value[0:1], value[1:3], "00", value[3:]
536 sign, hh, mm, value = value[0:1], value[1:3], value[3:5], value[5:]
539 hr, err = strconv.Atoi(hh)
541 min, err = strconv.Atoi(mm)
543 t.ZoneOffset = (hr*60 + min) * 60 // offset is in seconds
547 t.ZoneOffset = -t.ZoneOffset
556 p, value = value[0:2], value[2:]
559 } else if p != "AM" {
567 p, value = value[0:2], value[2:]
570 } else if p != "am" {
574 // Does it look like a time zone?
575 if len(value) >= 3 && value[0:3] == "UTC" {
576 t.Zone, value = value[0:3], value[3:]
580 if len(value) >= 3 && value[2] == 'T' {
581 p, value = value[0:3], value[3:]
582 } else if len(value) >= 4 && value[3] == 'T' {
583 p, value = value[0:4], value[4:]
588 for i := 0; i < len(p); i++ {
589 if p[i] < 'A' || 'Z' < p[i] {
596 // It's a valid format.
598 // Can we find its offset?
599 if offset, found := lookupByName(p); found {
600 t.ZoneOffset = offset
603 if rangeErrString != "" {
604 return nil, &ParseError{alayout, avalue, std, value, ": " + rangeErrString + " out of range"}
607 return nil, &ParseError{alayout, avalue, std, value, ""}
610 if pmSet && t.Hour < 12 {