time: fix Parse for time zones

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>
This commit is contained in:
Joe Tsai 2022-08-21 02:52:01 -07:00 committed by Gopher Robot
parent f9cdc09497
commit c94633d2f8
2 changed files with 7 additions and 3 deletions

View 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] {

View 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) {