Skip to content

Commit 132efd1

Browse files
authored
Specifically disallow timezones of "-00", "-00:00" and "-0000", per section ISO8601 section 3.4.2. (#8)
1 parent e705ade commit 132efd1

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

error.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var (
99
// ErrZoneCharacters indicates an incorrect amount of characters was passed to ParseISOZone.
1010
ErrZoneCharacters = errors.New("iso8601: Expected between 3 and 6 characters for zone information")
1111

12+
// ErrInvalidZone indicates an invalid timezone per the standard that doesn't violate any specific
13+
// character parsing rules.
14+
ErrInvalidZone = errors.New("iso8601: Specified zone is invalid")
15+
1216
// ErrRemainingData indicates that there is extra data after a `Z` character.
1317
ErrRemainingData = errors.New("iso8601: Unexepected remaining data after `Z`")
1418

iso8601.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ func ParseISOZone(inp []byte) (*time.Location, error) {
8080
if neg {
8181
offset = -offset
8282
}
83+
if neg && offset == 0 {
84+
return nil, ErrInvalidZone
85+
}
8386
return time.FixedZone("", offset), nil
8487
}
8588

iso8601_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ type TestCase struct {
1616
Second int
1717
MilliSecond int
1818

19-
Zone float64
19+
Zone float64
20+
ShouldFailParse bool
2021
}
2122

2223
var cases = []TestCase{
@@ -179,13 +180,49 @@ var cases = []TestCase{
179180
MilliSecond: 502,
180181
Zone: 5.75,
181182
},
183+
{
184+
Using: "2017-04-24T09:41:34.502+00",
185+
Year: 2017, Month: 4, Day: 24,
186+
Hour: 9, Minute: 41, Second: 34,
187+
MilliSecond: 502,
188+
Zone: 0,
189+
},
190+
{
191+
Using: "2017-04-24T09:41:34.502+0000",
192+
Year: 2017, Month: 4, Day: 24,
193+
Hour: 9, Minute: 41, Second: 34,
194+
MilliSecond: 502,
195+
Zone: 0,
196+
},
197+
{
198+
Using: "2017-04-24T09:41:34.502+00:00",
199+
Year: 2017, Month: 4, Day: 24,
200+
Hour: 9, Minute: 41, Second: 34,
201+
MilliSecond: 502,
202+
Zone: 0,
203+
},
204+
{
205+
Using: "2017-04-24T09:41:34.502-00",
206+
ShouldFailParse: true,
207+
},
208+
{
209+
Using: "2017-04-24T09:41:34.502-0000",
210+
ShouldFailParse: true,
211+
},
212+
{
213+
Using: "2017-04-24T09:41:34.502-00:00",
214+
ShouldFailParse: true,
215+
},
182216
}
183217

184218
func TestParse(t *testing.T) {
185219
for _, c := range cases {
186220
t.Run(c.Using, func(t *testing.T) {
187221
d, err := Parse([]byte(c.Using))
188222
if err != nil {
223+
if c.ShouldFailParse {
224+
return
225+
}
189226
t.Fatal(err)
190227
}
191228
t.Log(d)
@@ -227,6 +264,9 @@ func TestParseString(t *testing.T) {
227264
t.Run(c.Using, func(t *testing.T) {
228265
d, err := ParseString(c.Using)
229266
if err != nil {
267+
if c.ShouldFailParse {
268+
return
269+
}
230270
t.Fatal(err)
231271
}
232272
t.Log(d)

0 commit comments

Comments
 (0)