]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/time/format.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / time / format.go
1 package time
2
3 import (
4 "bytes"
5 "os"
6 "strconv"
7 )
8
9 const (
10 numeric = iota
11 alphabetic
12 separator
13 plus
14 minus
15 )
16
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.
26 //
27 // Numeric time zone offsets format as follows:
28 // -0700 ±hhmm
29 // -07:00 ±hh:mm
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:
33 // Z0700 Z or ±hhmm
34 // Z07:00 Z or ±hh:mm
35 const (
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"
44 Kitchen = "3:04PM"
45 RFC3339 = "2006-01-02T15:04:05Z07:00"
46 )
47
48 const (
49 stdLongMonth = "January"
50 stdMonth = "Jan"
51 stdNumMonth = "1"
52 stdZeroMonth = "01"
53 stdLongWeekDay = "Monday"
54 stdWeekDay = "Mon"
55 stdDay = "2"
56 stdUnderDay = "_2"
57 stdZeroDay = "02"
58 stdHour = "15"
59 stdHour12 = "3"
60 stdZeroHour12 = "03"
61 stdMinute = "4"
62 stdZeroMinute = "04"
63 stdSecond = "5"
64 stdZeroSecond = "05"
65 stdLongYear = "2006"
66 stdYear = "06"
67 stdPM = "PM"
68 stdpm = "pm"
69 stdTZ = "MST"
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
75 )
76
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++ {
81 switch 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:]
85 }
86 if len(layout) >= i+3 && layout[i:i+3] == stdMonth {
87 return layout[0:i], stdMonth, layout[i+3:]
88 }
89
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:]
93 }
94 if len(layout) >= i+3 {
95 if layout[i:i+3] == stdWeekDay {
96 return layout[0:i], stdWeekDay, layout[i+3:]
97 }
98 if layout[i:i+3] == stdTZ {
99 return layout[0:i], stdTZ, layout[i+3:]
100 }
101 }
102
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:]
106 }
107
108 case '1': // 15, 1
109 if len(layout) >= i+2 && layout[i+1] == '5' {
110 return layout[0:i], stdHour, layout[i+2:]
111 }
112 return layout[0:i], stdNumMonth, layout[i+1:]
113
114 case '2': // 2006, 2
115 if len(layout) >= i+4 && layout[i:i+4] == stdLongYear {
116 return layout[0:i], stdLongYear, layout[i+4:]
117 }
118 return layout[0:i], stdDay, layout[i+1:]
119
120 case '_': // _2
121 if len(layout) >= i+2 && layout[i+1] == '2' {
122 return layout[0:i], stdUnderDay, layout[i+2:]
123 }
124
125 case '3', '4', '5': // 3, 4, 5
126 return layout[0:i], layout[i : i+1], layout[i+1:]
127
128 case 'P': // PM
129 if len(layout) >= i+2 && layout[i+1] == 'M' {
130 return layout[0:i], layout[i : i+2], layout[i+2:]
131 }
132
133 case 'p': // pm
134 if len(layout) >= i+2 && layout[i+1] == 'm' {
135 return layout[0:i], layout[i : i+2], layout[i+2:]
136 }
137
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:]
141 }
142 if len(layout) >= i+6 && layout[i:i+6] == stdNumColonTZ {
143 return layout[0:i], layout[i : i+6], layout[i+6:]
144 }
145 if len(layout) >= i+3 && layout[i:i+3] == stdNumShortTZ {
146 return layout[0:i], layout[i : i+3], layout[i+3:]
147 }
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:]
151 }
152 if len(layout) >= i+6 && layout[i:i+6] == stdISO8601ColonTZ {
153 return layout[0:i], layout[i : i+6], layout[i+6:]
154 }
155 }
156 }
157 return layout, "", ""
158 }
159
160 var longDayNames = []string{
161 "Sunday",
162 "Monday",
163 "Tuesday",
164 "Wednesday",
165 "Thursday",
166 "Friday",
167 "Saturday",
168 }
169
170 var shortDayNames = []string{
171 "Sun",
172 "Mon",
173 "Tue",
174 "Wed",
175 "Thu",
176 "Fri",
177 "Sat",
178 }
179
180 var shortMonthNames = []string{
181 "---",
182 "Jan",
183 "Feb",
184 "Mar",
185 "Apr",
186 "May",
187 "Jun",
188 "Jul",
189 "Aug",
190 "Sep",
191 "Oct",
192 "Nov",
193 "Dec",
194 }
195
196 var longMonthNames = []string{
197 "---",
198 "January",
199 "February",
200 "March",
201 "April",
202 "May",
203 "June",
204 "July",
205 "August",
206 "September",
207 "October",
208 "November",
209 "December",
210 }
211
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
216 }
217 }
218 return -1, val, errBad
219 }
220
221 func pad(i int, padding string) string {
222 s := strconv.Itoa(i)
223 if i < 10 {
224 s = padding + s
225 }
226 return s
227 }
228
229 func zeroPad(i int) string { return pad(i, "0") }
230
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.
239 for {
240 prefix, std, suffix := nextStdChunk(layout)
241 b.WriteString(prefix)
242 if std == "" {
243 break
244 }
245 var p string
246 switch std {
247 case stdYear:
248 p = strconv.Itoa64(t.Year % 100)
249 case stdLongYear:
250 p = strconv.Itoa64(t.Year)
251 case stdMonth:
252 p = shortMonthNames[t.Month]
253 case stdLongMonth:
254 p = longMonthNames[t.Month]
255 case stdNumMonth:
256 p = strconv.Itoa(t.Month)
257 case stdZeroMonth:
258 p = zeroPad(t.Month)
259 case stdWeekDay:
260 p = shortDayNames[t.Weekday]
261 case stdLongWeekDay:
262 p = longDayNames[t.Weekday]
263 case stdDay:
264 p = strconv.Itoa(t.Day)
265 case stdUnderDay:
266 p = pad(t.Day, " ")
267 case stdZeroDay:
268 p = zeroPad(t.Day)
269 case stdHour:
270 p = zeroPad(t.Hour)
271 case stdHour12:
272 p = strconv.Itoa(t.Hour % 12)
273 case stdZeroHour12:
274 p = zeroPad(t.Hour % 12)
275 case stdMinute:
276 p = strconv.Itoa(t.Minute)
277 case stdZeroMinute:
278 p = zeroPad(t.Minute)
279 case stdSecond:
280 p = strconv.Itoa(t.Second)
281 case stdZeroSecond:
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' {
287 p = "Z"
288 break
289 }
290 zone := t.ZoneOffset / 60 // convert to minutes
291 if zone < 0 {
292 p = "-"
293 zone = -zone
294 } else {
295 p = "+"
296 }
297 p += zeroPad(zone / 60)
298 if std == stdISO8601ColonTZ || std == stdNumColonTZ {
299 p += ":"
300 }
301 p += zeroPad(zone % 60)
302 case stdPM:
303 if t.Hour >= 12 {
304 p = "PM"
305 } else {
306 p = "AM"
307 }
308 case stdpm:
309 if t.Hour >= 12 {
310 p = "pm"
311 } else {
312 p = "am"
313 }
314 case stdTZ:
315 if t.Zone != "" {
316 p = t.Zone
317 } else {
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
321 if zone < 0 {
322 p = "-"
323 zone = -zone
324 } else {
325 p = "+"
326 }
327 p += zeroPad(zone / 60)
328 p += zeroPad(zone % 60)
329 }
330 }
331 b.WriteString(p)
332 layout = suffix
333 }
334 return b.String()
335 }
336
337 // String returns a Unix-style representation of the time value.
338 func (t *Time) String() string {
339 if t == nil {
340 return "<nil>"
341 }
342 return t.Format(UnixDate)
343 }
344
345 var errBad = os.ErrorString("bad") // just a marker; not returned to user
346
347 // ParseError describes a problem parsing a time string.
348 type ParseError struct {
349 Layout string
350 Value string
351 LayoutElem string
352 ValueElem string
353 Message string
354 }
355
356 // String is the string representation of a ParseError.
357 func (e *ParseError) String() string {
358 if e.Message == "" {
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)
364 }
365 return "parsing time " +
366 strconv.Quote(e.Value) + e.Message
367 }
368
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' {
374 return 0, s, errBad
375 }
376 if len(s) == 1 || s[1] < '0' || s[1] > '9' {
377 if fixed {
378 return 0, s, errBad
379 }
380 return int(s[0] - '0'), s[1:], nil
381 }
382 return int(s[0]-'0')*10 + int(s[1]-'0'), s[2:], nil
383 }
384
385 func cutspace(s string) string {
386 for len(s) > 0 && s[0] == ' ' {
387 s = s[1:]
388 }
389 return s
390 }
391
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] != ' ' {
398 return "", errBad
399 }
400 prefix = cutspace(prefix)
401 value = cutspace(value)
402 continue
403 }
404 if len(value) == 0 || value[0] != prefix[0] {
405 return "", errBad
406 }
407 prefix = prefix[1:]
408 value = value[1:]
409 }
410 return value, nil
411 }
412
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
417 // representations.
418 //
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) {
426 var t Time
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.
431 for {
432 var err os.Error
433 prefix, std, suffix := nextStdChunk(layout)
434 value, err = skip(value, prefix)
435 if err != nil {
436 return nil, &ParseError{alayout, avalue, prefix, value, ""}
437 }
438 if len(std) == 0 {
439 if len(value) != 0 {
440 return nil, &ParseError{alayout, avalue, "", value, ": extra text: " + value}
441 }
442 break
443 }
444 layout = suffix
445 var p string
446 switch std {
447 case stdYear:
448 if len(value) < 2 {
449 err = errBad
450 break
451 }
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
455 t.Year += 1900
456 } else {
457 t.Year += 2000
458 }
459 case stdLongYear:
460 if len(value) < 4 || value[0] < '0' || value[0] > '9' {
461 err = errBad
462 break
463 }
464 p, value = value[0:4], value[4:]
465 t.Year, err = strconv.Atoi64(p)
466 case stdMonth:
467 t.Month, value, err = lookup(shortMonthNames, value)
468 case stdLongMonth:
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"
474 }
475 case stdWeekDay:
476 t.Weekday, value, err = lookup(shortDayNames, value)
477 case stdLongWeekDay:
478 t.Weekday, value, err = lookup(longDayNames, value)
479 case stdDay, stdUnderDay, stdZeroDay:
480 if std == stdUnderDay && len(value) > 0 && value[0] == ' ' {
481 value = value[1:]
482 }
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"
487 }
488 case stdHour:
489 t.Hour, value, err = getnum(value, false)
490 if t.Hour < 0 || 24 <= t.Hour {
491 rangeErrString = "hour"
492 }
493 case stdHour12, stdZeroHour12:
494 t.Hour, value, err = getnum(value, std == stdZeroHour12)
495 if t.Hour < 0 || 12 < t.Hour {
496 rangeErrString = "hour"
497 }
498 case stdMinute, stdZeroMinute:
499 t.Minute, value, err = getnum(value, std == stdZeroMinute)
500 if t.Minute < 0 || 60 <= t.Minute {
501 rangeErrString = "minute"
502 }
503 case stdSecond, stdZeroSecond:
504 t.Second, value, err = getnum(value, std == stdZeroSecond)
505 if t.Second < 0 || 60 <= t.Second {
506 rangeErrString = "second"
507 }
508 case stdISO8601TZ, stdISO8601ColonTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ:
509 if std[0] == 'Z' && len(value) >= 1 && value[0] == 'Z' {
510 value = value[1:]
511 t.Zone = "UTC"
512 break
513 }
514 var sign, hh, mm string
515 if std == stdISO8601ColonTZ || std == stdNumColonTZ {
516 if len(value) < 6 {
517 err = errBad
518 break
519 }
520 if value[3] != ':' {
521 err = errBad
522 break
523 }
524 sign, hh, mm, value = value[0:1], value[1:3], value[4:6], value[6:]
525 } else if std == stdNumShortTZ {
526 if len(value) < 3 {
527 err = errBad
528 break
529 }
530 sign, hh, mm, value = value[0:1], value[1:3], "00", value[3:]
531 } else {
532 if len(value) < 5 {
533 err = errBad
534 break
535 }
536 sign, hh, mm, value = value[0:1], value[1:3], value[3:5], value[5:]
537 }
538 var hr, min int
539 hr, err = strconv.Atoi(hh)
540 if err == nil {
541 min, err = strconv.Atoi(mm)
542 }
543 t.ZoneOffset = (hr*60 + min) * 60 // offset is in seconds
544 switch sign[0] {
545 case '+':
546 case '-':
547 t.ZoneOffset = -t.ZoneOffset
548 default:
549 err = errBad
550 }
551 case stdPM:
552 if len(value) < 2 {
553 err = errBad
554 break
555 }
556 p, value = value[0:2], value[2:]
557 if p == "PM" {
558 pmSet = true
559 } else if p != "AM" {
560 err = errBad
561 }
562 case stdpm:
563 if len(value) < 2 {
564 err = errBad
565 break
566 }
567 p, value = value[0:2], value[2:]
568 if p == "pm" {
569 pmSet = true
570 } else if p != "am" {
571 err = errBad
572 }
573 case stdTZ:
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:]
577 break
578 }
579
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:]
584 } else {
585 err = errBad
586 break
587 }
588 for i := 0; i < len(p); i++ {
589 if p[i] < 'A' || 'Z' < p[i] {
590 err = errBad
591 }
592 }
593 if err != nil {
594 break
595 }
596 // It's a valid format.
597 t.Zone = p
598 // Can we find its offset?
599 if offset, found := lookupByName(p); found {
600 t.ZoneOffset = offset
601 }
602 }
603 if rangeErrString != "" {
604 return nil, &ParseError{alayout, avalue, std, value, ": " + rangeErrString + " out of range"}
605 }
606 if err != nil {
607 return nil, &ParseError{alayout, avalue, std, value, ""}
608 }
609 }
610 if pmSet && t.Hour < 12 {
611 t.Hour += 12
612 }
613 return &t, nil
614 }