Skip to content

Commit

Permalink
Allow space to separate date and time
Browse files Browse the repository at this point in the history
Fixes #231
  • Loading branch information
mbialon committed Sep 18, 2019
1 parent 26fd12f commit 4e6c8cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func (l *tomlLexer) run() {
}

func init() {
dateRegexp = regexp.MustCompile(`^\d{1,4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})`)
dateRegexp = regexp.MustCompile(`^\d{1,4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})`)
}

// Entry point
Expand Down
9 changes: 9 additions & 0 deletions lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ func TestDateRegexp(t *testing.T) {
if dateRegexp.FindString("1979-05-27T00:32:00.999999-07:00") == "" {
t.Error("nano precision lexing")
}
if dateRegexp.FindString("1979-05-27 07:32:00Z") == "" {
t.Error("space delimiter lexing")
}
}

func TestKeyEqualDate(t *testing.T) {
Expand All @@ -320,6 +323,12 @@ func TestKeyEqualDate(t *testing.T) {
{Position{1, 7}, tokenDate, "1979-05-27T00:32:00.999999-07:00"},
{Position{1, 39}, tokenEOF, ""},
})
testFlow(t, "foo = 1979-05-27 07:32:00Z", []token{
{Position{1, 1}, tokenKey, "foo"},
{Position{1, 5}, tokenEqual, "="},
{Position{1, 7}, tokenDate, "1979-05-27 07:32:00Z"},
{Position{1, 27}, tokenEOF, ""},
})
}

func TestFloatEndingWithDot(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ func (p *tomlParser) parseRvalue() interface{} {
}
return val
case tokenDate:
val, err := time.ParseInLocation(time.RFC3339Nano, tok.val, time.UTC)
layout := time.RFC3339Nano
if !strings.Contains(tok.val, "T") {
layout = strings.Replace(layout, "T", " ", 1)
}
val, err := time.ParseInLocation(layout, tok.val, time.UTC)
if err != nil {
p.raiseError(tok, "%s", err)
}
Expand Down
9 changes: 8 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestFloatsWithExponents(t *testing.T) {
tree, err := Load("a = 5e+22\nb = 5E+22\nc = -5e+22\nd = -5e-22\ne = 6.626e-34")
assertTree(t, tree, err, map[string]interface{}{
"a": float64(5e+22),
"b": float64(5E+22),
"b": float64(5e+22),
"c": float64(-5e+22),
"d": float64(-5e-22),
"e": float64(6.626e-34),
Expand Down Expand Up @@ -225,6 +225,13 @@ func TestDateNano(t *testing.T) {
})
}

func TestDateSpaceDelimiter(t *testing.T) {
tree, err := Load("odt4 = 1979-05-27 07:32:00Z")
assertTree(t, tree, err, map[string]interface{}{
"odt4": time.Date(1979, time.May, 27, 7, 32, 0, 0, time.UTC),
})
}

func TestSimpleString(t *testing.T) {
tree, err := Load("a = \"hello world\"")
assertTree(t, tree, err, map[string]interface{}{
Expand Down

0 comments on commit 4e6c8cb

Please sign in to comment.