]> Cypherpunks repositories - gostls13.git/commitdiff
time.Parse should not require minutes for time zone
authorJan H. Hosang <jan.hosang@gmail.com>
Tue, 24 Aug 2010 21:41:26 +0000 (07:41 +1000)
committerRob Pike <r@golang.org>
Tue, 24 Aug 2010 21:41:26 +0000 (07:41 +1000)
Fixes #1026.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/1962048

src/pkg/time/format.go
src/pkg/time/time_test.go

index 4ea09a110171d434ea5a8ac4170cd908985c3a6f..8166d2e77acc231a8509e5f3995c585ed14cb3af 100644 (file)
@@ -70,6 +70,7 @@ const (
        stdISO8601TZ      = "Z0700"  // prints Z for UTC
        stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
        stdNumTZ          = "-0700"  // always numeric
+       stdNumShortTZ     = "-07"    // always numeric
        stdNumColonTZ     = "-07:00" // always numeric
 )
 
@@ -134,13 +135,16 @@ func nextStdChunk(layout string) (prefix, std, suffix string) {
                                return layout[0:i], layout[i : i+2], layout[i+2:]
                        }
 
-               case '-': // -0700, -07:00
+               case '-': // -0700, -07:00, -07
                        if len(layout) >= i+5 && layout[i:i+5] == stdNumTZ {
                                return layout[0:i], layout[i : i+5], layout[i+5:]
                        }
                        if len(layout) >= i+6 && layout[i:i+6] == stdNumColonTZ {
                                return layout[0:i], layout[i : i+6], layout[i+6:]
                        }
+                       if len(layout) >= i+3 && layout[i:i+3] == stdNumShortTZ {
+                               return layout[0:i], layout[i : i+3], layout[i+3:]
+                       }
                case 'Z': // Z0700, Z07:00
                        if len(layout) >= i+5 && layout[i:i+5] == stdISO8601TZ {
                                return layout[0:i], layout[i : i+5], layout[i+5:]
@@ -496,7 +500,7 @@ func Parse(alayout, avalue string) (*Time, os.Error) {
                        if t.Second < 0 || 60 <= t.Second {
                                rangeErrString = "second"
                        }
-               case stdISO8601TZ, stdISO8601ColonTZ, stdNumTZ, stdNumColonTZ:
+               case stdISO8601TZ, stdISO8601ColonTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ:
                        if std[0] == 'Z' && len(value) >= 1 && value[0] == 'Z' {
                                value = value[1:]
                                t.Zone = "UTC"
@@ -513,6 +517,12 @@ func Parse(alayout, avalue string) (*Time, os.Error) {
                                        break
                                }
                                sign, hh, mm, value = value[0:1], value[1:3], value[4:6], value[6:]
+                       } else if std == stdNumShortTZ {
+                               if len(value) < 3 {
+                                       err = errBad
+                                       break
+                               }
+                               sign, hh, mm, value = value[0:1], value[1:3], "00", value[3:]
                        } else {
                                if len(value) < 5 {
                                        err = errBad
index 79933080e0e8887c7596bb4b8787eaccb00b79ad..1574b0834f7621288169af30f38463be6ab03b5e 100644 (file)
@@ -169,6 +169,7 @@ var parseTests = []ParseTest{
        ParseTest{"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1},
        ParseTest{"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1},
        ParseTest{"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00", true, false, 1},
+       ParseTest{"custom: \"2006-01-02 15:04:05-07\"", "2006-01-02 15:04:05-07", "2010-02-04 21:00:57-08", true, false, 1},
        // Amount of white space should not matter.
        ParseTest{"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1},
        ParseTest{"ANSIC", ANSIC, "Thu      Feb     4     21:00:57     2010", false, true, 1},