Skip to content

Commit

Permalink
Require 2-digit hour
Browse files Browse the repository at this point in the history
Fixes #320
  • Loading branch information
arp242 committed Oct 1, 2023
1 parent 9775128 commit 59c762d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 20 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ func (p *parser) valueDatetime(it item) (any, tomlType) {
}
t, err = time.ParseInLocation(dt.fmt, it.val, dt.zone)
if err == nil {
if missingLeadingZero(it.val, dt.fmt) {
p.panicErr(it, errParseDate{it.val})
}
ok = true
break
}
Expand All @@ -382,6 +385,23 @@ func (p *parser) valueDatetime(it item) (any, tomlType) {
return t, p.typeOfPrimitive(it)
}

// Go's time.Parse() will accept numbers without a leading zero; there isn't any
// way to require it. https://github.com/golang/go/issues/29911
//
// Depend on the fact that the separators (- and :) should always be at the same
// location.
func missingLeadingZero(d, l string) bool {
for i, c := range []byte(l) {
if c == '.' || c == 'Z' {
return false
}
if (c < '0' || c > '9') && d[i] != c {
return true
}
}
return false
}

func (p *parser) valueArray(it item) (any, tomlType) {
p.setType(p.currentKey, tomlArray, it.pos)

Expand Down
9 changes: 0 additions & 9 deletions toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,6 @@ func runTomlTest(t *testing.T, includeNext bool, wantFail ...string) {
Parser: parser{},
RunTests: runTests,
SkipTests: []string{
// "15" in time.Parse() accepts both "1" and "01". The TOML
// specification says that times *must* start with a leading
// zero, but this requires writing out own datetime parser.
// I think it's actually okay to just accept both really.
// https://github.com/BurntSushi/toml/issues/320
"invalid/datetime/time-no-leads",
"invalid/local-time/time-no-leads",
"invalid/local-datetime/time-no-leads",

// These tests are fine, just doesn't deal well with empty output.
"valid/comment/noeol",
"valid/comment/nonascii",
Expand Down

0 comments on commit 59c762d

Please sign in to comment.