From f9ba08244db487b8b55172f1d03c88daedfc7db9 Mon Sep 17 00:00:00 2001 From: Allen Date: Fri, 9 Oct 2020 22:55:11 +0800 Subject: [PATCH] Do not allow T-prefix on local dates (#446) Fixes #442 --- lexer.go | 4 +--- lexer_test.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lexer.go b/lexer.go index c4becd9..4b14956 100644 --- a/lexer.go +++ b/lexer.go @@ -772,8 +772,6 @@ func init() { // Group 1: nano precision // Group 2: timezone // - // /!\ also matches the empty string - // // Example matches: // 1979-05-27T07:32:00Z // 1979-05-27T00:32:00-07:00 @@ -788,7 +786,7 @@ func init() { // 1979-05-27 // 07:32:00 // 00:32:00.999999 - dateRegexp = regexp.MustCompile(`^(?:\d{4}-\d{2}-\d{2})?(?:[T ]?\d{2}:\d{2}:\d{2}(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})?)?`) + dateRegexp = regexp.MustCompile(`^(?:\d{4}-\d{2}-\d{2}[T\s]?)?(?:\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 6aa6fab..d999f99 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -317,6 +317,7 @@ func TestDateRegexp(t *testing.T) { "err:date-1year": "9-05-27", "err:date-2year": "79-05-27", "err:date-3year": "979-05-27", + "err:date-T-prefix": "T07:32:00", } for name, value := range cases { @@ -356,6 +357,24 @@ func TestKeyEqualDate(t *testing.T) { {Position{1, 7}, tokenDate, "1979-05-27 07:32:00Z"}, {Position{1, 27}, tokenEOF, ""}, }) + testFlow(t, "foo = 07:32:00", []token{ + {Position{1, 1}, tokenKey, "foo"}, + {Position{1, 5}, tokenEqual, "="}, + {Position{1, 7}, tokenLocalDate, "07:32:00"}, + {Position{1, 15}, tokenEOF, ""}, + }) + testFlow(t, "foo = 07:32:00Z", []token{ + {Position{1, 1}, tokenKey, "foo"}, + {Position{1, 5}, tokenEqual, "="}, + {Position{1, 7}, tokenDate, "07:32:00Z"}, + {Position{1, 16}, tokenEOF, ""}, + }) + testFlow(t, "foo = 00:32:00.999999-07:00", []token{ + {Position{1, 1}, tokenKey, "foo"}, + {Position{1, 5}, tokenEqual, "="}, + {Position{1, 7}, tokenDate, "00:32:00.999999-07:00"}, + {Position{1, 28}, tokenEOF, ""}, + }) } func TestFloatEndingWithDot(t *testing.T) {