Fixes #31 : Use RFC 3339 for datetimes
This commit is contained in:
@@ -192,7 +192,10 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
||||
return l.lexKey
|
||||
}
|
||||
|
||||
if dateRegexp.FindString(l.input[l.pos:]) != "" {
|
||||
dateMatch := dateRegexp.FindString(l.input[l.pos:])
|
||||
if dateMatch != "" {
|
||||
l.ignore()
|
||||
l.pos += len(dateMatch)
|
||||
return l.lexDate
|
||||
}
|
||||
|
||||
@@ -214,8 +217,6 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexDate() tomlLexStateFn {
|
||||
l.ignore()
|
||||
l.pos += 20 // Fixed size of a date in TOML
|
||||
l.emit(tokenDate)
|
||||
return l.lexRvalue
|
||||
}
|
||||
@@ -461,7 +462,7 @@ func (l *tomlLexer) lexNumber() tomlLexStateFn {
|
||||
}
|
||||
|
||||
func init() {
|
||||
dateRegexp = regexp.MustCompile("^\\d{1,4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z")
|
||||
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
|
||||
|
||||
+19
-1
@@ -273,7 +273,13 @@ func TestKeyEqualArrayBoolsWithComments(t *testing.T) {
|
||||
|
||||
func TestDateRegexp(t *testing.T) {
|
||||
if dateRegexp.FindString("1979-05-27T07:32:00Z") == "" {
|
||||
t.Fail()
|
||||
t.Error("basic lexing")
|
||||
}
|
||||
if dateRegexp.FindString("1979-05-27T00:32:00-07:00") == "" {
|
||||
t.Error("offset lexing")
|
||||
}
|
||||
if dateRegexp.FindString("1979-05-27T00:32:00.999999-07:00") == "" {
|
||||
t.Error("nano precision lexing")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,6 +290,18 @@ func TestKeyEqualDate(t *testing.T) {
|
||||
token{Position{1, 7}, tokenDate, "1979-05-27T07:32:00Z"},
|
||||
token{Position{1, 27}, tokenEOF, ""},
|
||||
})
|
||||
testFlow(t, "foo = 1979-05-27T00:32:00-07:00", []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{1, 7}, tokenDate, "1979-05-27T00:32:00-07:00"},
|
||||
token{Position{1, 32}, tokenEOF, ""},
|
||||
})
|
||||
testFlow(t, "foo = 1979-05-27T00:32:00.999999-07:00", []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{1, 7}, tokenDate, "1979-05-27T00:32:00.999999-07:00"},
|
||||
token{Position{1, 39}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFloatEndingWithDot(t *testing.T) {
|
||||
|
||||
@@ -222,7 +222,7 @@ func (p *tomlParser) parseRvalue() interface{} {
|
||||
}
|
||||
return val
|
||||
case tokenDate:
|
||||
val, err := time.Parse(time.RFC3339, tok.val)
|
||||
val, err := time.Parse(time.RFC3339Nano, tok.val)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
|
||||
@@ -88,6 +88,21 @@ func TestSimpleDate(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestDateOffset(t *testing.T) {
|
||||
tree, err := Load("a = 1979-05-27T00:32:00-07:00")
|
||||
assertTree(t, tree, err, map[string]interface{}{
|
||||
"a": time.Date(1979, time.May, 27, 0, 32, 0, 0, time.FixedZone("", -7 * 60 * 60)),
|
||||
})
|
||||
}
|
||||
|
||||
func TestDateNano(t *testing.T) {
|
||||
tree, err := Load("a = 1979-05-27T00:32:00.999999999-07:00")
|
||||
assertTree(t, tree, err, map[string]interface{}{
|
||||
"a": time.Date(1979, time.May, 27, 0, 32, 0, 999999999, time.FixedZone("", -7 * 60 * 60)),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func TestSimpleString(t *testing.T) {
|
||||
tree, err := Load("a = \"hello world\"")
|
||||
assertTree(t, tree, err, map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user