-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fromdate should support timezone offsets #1053
Comments
Supporting timezones in general is problematic. Currently we use the C library for dealing with parsing and formatting datetime strings. The more features of the C library we use, the harder it will be to stop using it later. Also, the C library's datetime capabilities vary quite a bit from one version/platform to another, so the more features we use, the more issues we can expect users to file that are ultimately caused by C library issues. Limiting datetime strings to UTC ISO8601 was an explicit choice to limit this pain. Adding timezone offset support when parsing is doable though, and probably worth the effort. I'm much less interested in adding timezone offsets when formatting datetime strings. |
Hi, thanks for the replay. AFAIK, the format I put to be parsed is a valid ISO8601 date. I've got the impression, that only the "timezone" 'Z' is working at jq, or could you give me an example of a different zone being parsed? BR, Chris |
I wasn't clear. Only the "Z" zone is being parsed.
|
One of the nice things about dates being in UTC is that then dates can be compared and ordered lexicographically. So I'm inclined to only support timezone offsets in Incidentally, you can use |
Hi, |
@CFoltin You should use (And, of course, I see that glibc doesn't like a |
+1 for having timezone support in Although dates aren't in the JSON standard, I see a bunch of use cases for All my incoming JSON dates include the |
@aliekens About your specific use case, since jq 1.5 has support for regular expressions and string substitutions and replacements, you can use this to rearrange the string so that the timezone appears where it can be parsed. |
Yay, I have figured out a way to correctly parse datetimes with timezones in
Some notes:
|
In the meantime, the FAQ has a question: Q: How are named capture variables used? |
I have tested aliekens in jq-1.5-1-a5b5cbe, and I get every time the same output: |
Another workaround, in case your gmt offset is already a float (remember to reverse the offset to get UTC):
|
Ugly manual parsing, but, hey, it works: echo '{"date":"2020-01-30T02:35:20-08:00"}' | \
jq 'def parseDate(date): date | capture("(?<no_tz>.*)(?<tz_sgn>[-+])(?<tz_hr>\\d{2}):(?<tz_min>\\d{2})$") | (.no_tz + "Z" | fromdateiso8601) - (.tz_sgn + "60" | tonumber) * ((.tz_hr | tonumber) * 60 + (.tz_min | tonumber)); parseDate(.date)' |
My main use case for this is consuming timestamps in json produced by go, which will (without going to some extra work) serialize times as e.g. It's entirely reasonable to only support UTC for the "broken down" datetime representation, but like it or not there's a lot of json data out there which is stored with a zone offset. There is also no need to support parsing named time zones (which are not permitted by either RFC3339 or ISO-8601). It's misleading to claim to support iso8601 date format if you don't support data in formats which conform to the spec and are produced by the standard libraries of common programming languages. @erhhung's solution is a reasonable one. I'd extend it a little to
That is,
Note that the above specifically does not handle "implicitly local time" dates (those lacking either Building that data massaging into |
For a workaround, please check #1117 |
There are external small libraries in C that specifically do ISO8601 parsing. https://github.com/chansen/c-dt has great test suite and no dependencies AFAIK. Would a PR building on that be accepted? Currently JQ hardly supports ISO8601 at all, as it does not support a single line in the following list of valid ISO8601 strings:
Building on @chansen's lib would change that. |
On platforms where But not all platforms have equally good C time function support, so, yes, @fatso83, I think we'd consider a replacement of the C library's time functions with something like @chansen's |
Are people asking for |
I don't know about others and the general function for date parsing, but I assumed a function that indicated parsing iso8601 would do just that, so support for tz in strings - without involved workarounds - is much wanted 😃 |
That would be nice, but mainly I think primarily people just want I don't really see how |
@fatso83 @adam-azarchs try this: def fromdateiso8601: first(strptime("%Y-%m-%dT%H:%M:%S%z")?,strptime("%Y-%m-%dT%H:%M:%SZ"))|timegm; The |
Linux
macOS
|
Hello, I meet an unsupported date format... The format is "YYYY-MM-DDTHH:MM:SS.xxx[+-]HHMM". See example below. I made a fromdateiso8601gmt function to support it:
That is far to be optimal, but it is better than nothing. $ echo '
"2024-02-06T16:53:19.1234Z"
"2024-02-06T17:53:19.1234+0100"
"2024-02-06T14:53:19.1234-0200"
"2024-02-05T23:53:19.123Z"
"2024-02-06T00:53:19.123+0100"
"2024-02-06T01:53:19.123+0200"
' | jq "$def_fromdateiso8601gmt"'fromdateiso8601gmt'
1707238399.1234
1707238399.1234
1707238399.1234
1707177199.123
1707177199.123
1707177199.123 |
Hi,
parsing the following command:
"2015-12-24T09:29:30+01:00" | fromdate
gives the error
jq: error (at :49): date "2015-12-24T09:29:30+01:00" does not match format "%Y-%m-%dT%H:%M:%SZ"
although it seems pretty ok for. Only timezone "Z" seems to work.
BR, Chris from FreeMind
The text was updated successfully, but these errors were encountered: