-
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix quotation for timestamps #515
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Character type for character | ||
|
@@ -600,20 +601,26 @@ func ToNumber(value string) *NumberValue { | |
} | ||
} | ||
|
||
func looksLikeTimeValue(value string) bool { | ||
for i, c := range value { | ||
switch c { | ||
case ':', '1', '2', '3', '4', '5', '6', '7', '8', '9': | ||
continue | ||
case '0': | ||
if i == 0 { | ||
return false | ||
} | ||
continue | ||
// This is a subset of the formats permitted by the regular expression | ||
// defined at http://yaml.org/type/timestamp.html. Note that time.Parse | ||
// cannot handle: "2001-12-14 21:59:43.10 -5" from the examples. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why didn't you provide a layout to handle this case ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I've actually tried supporting the layout, but Go does not seem to support the "-timezone" representation... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK ! I understand ! |
||
var timestampFormats = []string{ | ||
time.RFC3339Nano, | ||
"2006-01-02t15:04:05.999999999Z07:00", // RFC3339Nano with lower-case "t". | ||
time.DateTime, | ||
time.DateOnly, | ||
|
||
// Not in examples, but to preserve backward compatibility by quoting time values. | ||
"15:4", | ||
} | ||
|
||
func isTimestamp(value string) bool { | ||
for _, format := range timestampFormats { | ||
if _, err := time.Parse(format, value); err == nil { | ||
return true | ||
} | ||
return false | ||
} | ||
return true | ||
return false | ||
} | ||
|
||
// IsNeedQuoted whether need quote for passed string or not | ||
|
@@ -637,7 +644,7 @@ func IsNeedQuoted(value string) bool { | |
case ':', ' ': | ||
return true | ||
} | ||
if looksLikeTimeValue(value) { | ||
if isTimestamp(value) { | ||
return true | ||
} | ||
for i, c := range value { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've combine it into the isTimestamp function