-
Notifications
You must be signed in to change notification settings - Fork 7
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
Implement unix time and rfc3339 parsing #23
Implement unix time and rfc3339 parsing #23
Conversation
Signed-off-by: Oleg Zaytsev <[email protected]>
Signed-off-by: Oleg Zaytsev <[email protected]>
date + time + { | ||
toUnixTimestamp():: $.toUnixTimestamp(self.year, self.month, self.day, self.hour, self.minute, self.second), | ||
}, |
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.
This isn't the most functional approach, but it's easy to use. I'm happy to listen for your suggestions.
// Sub-second precision isn't implemented yet, warn the user about that instead of returning wrong results. | ||
assert !stringContains(input, '.') : 'the provided RFC3339 input "%s" has a dot, most likely representing a sub-second precision, but this function does not support that' % input; | ||
|
||
// We don't support timezones, so string should end with 'Z' or 'z'. | ||
assert std.endsWith(input, 'Z') || std.endsWith(input, 'z') : 'the provided RFC3339 "%s" should end with "Z" or "z". This implementation does not currently support timezones' % input; | ||
|
||
// RFC3339 can separate date and time using 'T', 't' or ' '. | ||
// Find out which one it is and use it. | ||
local sep = | ||
if stringContains(input, 'T') then 'T' | ||
else if stringContains(input, 't') then 't' | ||
else if stringContains(input, ' ') then ' ' | ||
else error 'the provided RFC3339 input "%s" should contain either a "T", or a "t" or space " " as a separator for date and time parts' % input; |
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.
@Duologic as you asked in grafana/jsonnet-libs#1095, I added support for lowercase t/z and also for space separator.
// isNumeric checks that the input is a non-empty string containing only digit characters. | ||
local isNumeric(input) = | ||
assert std.type(input) == 'string' : 'isNumeric() only operates on string inputs, got %s' % std.type(input); | ||
std.foldl( | ||
function(acc, char) acc && std.codepoint('0') <= std.codepoint(char) && std.codepoint(char) <= std.codepoint('9'), | ||
std.stringChars(input), | ||
std.length(input) > 0, | ||
), |
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.
@jdbaldry I borrowed the implementation from your comment here :)
'#toUnixTimestamp': d.fn( | ||
||| | ||
`toUnixTimestamp` calculates the unix timestamp of a given date. | ||
|||, | ||
[ | ||
d.arg('year', d.T.number), | ||
d.arg('month', d.T.number), | ||
d.arg('day', d.T.number), | ||
d.arg('hour', d.T.number), | ||
d.arg('minute', d.T.number), | ||
d.arg('second', d.T.number), | ||
], | ||
), | ||
toUnixTimestamp(year, month, day, hour, minute, second):: | ||
sumYearsSeconds(year) + sumMonthsSeconds(year, month) + sumDaysSeconds(day) + hour * 3600 + minute * 60 + second, |
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.
This was already in github.com/grafana/jsonnet-libs/date, now I'm bringing here as it's useful once we've parsed the RFC3339.
Signed-off-by: Oleg Zaytsev <[email protected]>
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.
Looks great to me, very thorough.
``` | ||
|
||
`parseRFC3339` parses an RFC3339-formatted date & time string into an object containing the 'year', 'month', 'day', 'hour', 'minute' and 'second fields. |
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.
Would be useful to have an example of an RFC3339 date/time for those of us that know the format but not the RFC number.
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.
Sure, followed up in #24
This implements
time
functionstoUnixTimestamp
that calculates a unix timestamp of a date/time, andparseRFC3339
that does some limited parsing of RFC3339 timestamps.