Skip to content

Commit 9f2c611

Browse files
MyChaOS87bradfitz
authored andcommitted
time: add support for parsing timezones denoted by sign and offset
IANA Zoneinfo does not provide names for all timezones. Some are denoted by a sign and an offset only. E.g: Europe/Turkey is currently +03 or America/La_Paz which is -04 (https://data.iana.org/time-zones/releases/tzdata2018c.tar.gz) Fixes golang#24071 Change-Id: I9c230a719945e1263c5b52bab82084d22861be3e Reviewed-on: https://go-review.googlesource.com/98157 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 3d69ef3 commit 9f2c611

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/time/format.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,11 @@ func parseTimeZone(value string) (length int, ok bool) {
11171117
length = parseGMT(value)
11181118
return length, true
11191119
}
1120+
// Special Case 3: Some time zones are not named, but have +/-00 format
1121+
if value[0] == '+' || value[0] == '-' {
1122+
length = parseSignedOffset(value)
1123+
return length, true
1124+
}
11201125
// How many upper-case letters are there? Need at least three, at most five.
11211126
var nUpper int
11221127
for nUpper = 0; nUpper < 6; nUpper++ {
@@ -1153,21 +1158,29 @@ func parseGMT(value string) int {
11531158
if len(value) == 0 {
11541159
return 3
11551160
}
1161+
1162+
return 3 + parseSignedOffset(value)
1163+
}
1164+
1165+
// parseSignedOffset parses a signed timezone offset (e.g. "+03" or "-04").
1166+
// The function checks for a signed number in the range -14 through +12 excluding zero.
1167+
// Returns length of the found offset string or 0 otherwise
1168+
func parseSignedOffset(value string) int {
11561169
sign := value[0]
11571170
if sign != '-' && sign != '+' {
1158-
return 3
1171+
return 0
11591172
}
11601173
x, rem, err := leadingInt(value[1:])
11611174
if err != nil {
1162-
return 3
1175+
return 0
11631176
}
11641177
if sign == '-' {
11651178
x = -x
11661179
}
11671180
if x == 0 || x < -14 || 12 < x {
1168-
return 3
1181+
return 0
11691182
}
1170-
return 3 + len(value) - len(rem)
1183+
return len(value) - len(rem)
11711184
}
11721185

11731186
func parseNanoseconds(value string, nbytes int) (ns int, rangeErrString string, err error) {

src/time/format_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ var parseTimeZoneTests = []ParseTimeZoneTest{
427427
{"ESASTT hi", 0, false}, // run of upper-case letters too long.
428428
{"ESATY hi", 0, false}, // five letters must end in T.
429429
{"WITA hi", 4, true}, // Issue #18251
430+
{"+03 hi", 3, true}, // Issue #24071
431+
{"-04 hi", 3, true}, // Issue #24071
430432
}
431433

432434
func TestParseTimeZone(t *testing.T) {

0 commit comments

Comments
 (0)