Skip to content

Commit c94633d

Browse files
dsnetgopherbot
authored andcommitted
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Joseph Tsai <[email protected]> Auto-Submit: Joseph Tsai <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent f9cdc09 commit c94633d

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/time/format.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1233,12 +1233,12 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
12331233
sign, hour, min, seconds, value = value[0:1], value[1:3], value[3:5], "00", value[5:]
12341234
}
12351235
var hr, mm, ss int
1236-
hr, err = atoi(hour)
1236+
hr, _, err = getnum(hour, true)
12371237
if err == nil {
1238-
mm, err = atoi(min)
1238+
mm, _, err = getnum(min, true)
12391239
}
12401240
if err == nil {
1241-
ss, err = atoi(seconds)
1241+
ss, _, err = getnum(seconds, true)
12421242
}
12431243
zoneOffset = (hr*60+mm)*60 + ss // offset is in seconds
12441244
switch sign[0] {

src/time/format_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ var parseErrorTests = []ParseErrorTest{
604604
// issue 45391.
605605
{`"2006-01-02T15:04:05Z07:00"`, "0", `parsing time "0" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "0" as "\""`},
606606
{RFC3339, "\"", `parsing time "\"" as "2006-01-02T15:04:05Z07:00": cannot parse "\"" as "2006"`},
607+
608+
// issue 54570
609+
{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"`},
610+
{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"`},
607611
}
608612

609613
func TestParseErrors(t *testing.T) {

0 commit comments

Comments
 (0)