]> Cypherpunks repositories - gostls13.git/commitdiff
time: fix Parse for time zones
authorJoe Tsai <joetsai@digital-static.net>
Sun, 21 Aug 2022 09:52:01 +0000 (02:52 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 23 Aug 2022 18:12:45 +0000 (18:12 +0000)
The hours, minutes, and seconds fields for time zones
should not have any plus or minus signs.
Use getnum instead of atoi since the latter implicitly
handles leading signs, while the former does not.

Fixes #54570

Change-Id: If9600170af3af999739c27d81958e3649946913a
Reviewed-on: https://go-review.googlesource.com/c/go/+/425038
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Rob Pike <r@golang.org>
src/time/format.go
src/time/format_test.go

index a278cd9e6b8f909178676408d5952f85e56a4cd3..1887e6bce60607a177472d100ce0da2bd75abc50 100644 (file)
@@ -1233,12 +1233,12 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
                                sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], "00", value[5:]
                        }
                        var hr, mm, ss int
-                       hr, err = atoi(hour)
+                       hr, _, err = getnum(hour, true)
                        if err == nil {
-                               mm, err = atoi(min)
+                               mm, _, err = getnum(min, true)
                        }
                        if err == nil {
-                               ss, err = atoi(seconds)
+                               ss, _, err = getnum(seconds, true)
                        }
                        zoneOffset = (hr*60+mm)*60 + ss // offset is in seconds
                        switch sign[0] {
index e8caa5e1a47970cc2cfa776638b42f7a366eeef5..5c18ef45deb7663176079825fd2dcd8d08c5ae72 100644 (file)
@@ -604,6 +604,10 @@ var parseErrorTests = []ParseErrorTest{
        // issue 45391.
        {`"2006-01-02T15:04:05Z07:00"`, "0", `parsing time "0" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "0" as "\""`},
        {RFC3339, "\"", `parsing time "\"" as "2006-01-02T15:04:05Z07:00": cannot parse "\"" as "2006"`},
+
+       // issue 54570
+       {RFC3339, "0000-01-01T00:00:00+00:+0", `parsing time "0000-01-01T00:00:00+00:+0" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"`},
+       {RFC3339, "0000-01-01T00:00:00+-0:00", `parsing time "0000-01-01T00:00:00+-0:00" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"`},
 }
 
 func TestParseErrors(t *testing.T) {