From 4e6c8cb0c18ea1db26ffdaf3a41c96b244c25ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Bia=C5=82o=C5=84?= Date: Wed, 18 Sep 2019 09:46:31 +0200 Subject: [PATCH] Allow space to separate date and time Fixes #231 --- lexer.go | 2 +- lexer_test.go | 9 +++++++++ parser.go | 6 +++++- parser_test.go | 9 ++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lexer.go b/lexer.go index 6254d390..10609809 100644 --- a/lexer.go +++ b/lexer.go @@ -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 diff --git a/lexer_test.go b/lexer_test.go index 313b83c5..de91d922 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -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) { @@ -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) { diff --git a/parser.go b/parser.go index a7498e49..09fec259 100644 --- a/parser.go +++ b/parser.go @@ -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) } diff --git a/parser_test.go b/parser_test.go index 78345755..9c848ad6 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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), @@ -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{}{