-
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
Parsing of milliseconds in dates #1409
Comments
is progress in #1413 (comment) kind of abandoned? |
hi @juliangamble
You can see detailed usage in the manual or jq-1.6 manual.yml. |
I think this is an issue about
|
Struggled today with this, problem is that the unix "strptime" doesn't support milliseconds. As such, I switched to using a regex replace
So I'm transforming this "2015-03-05T23:51:47.487Z" to "2015-03-05T23:51:47Z" |
When you want to format a timestamp which contains offset:
This will transform the following: And will work with this as well: |
was this solved?
|
You could use gojq:
|
If you do not care about preserving the number of milliseconds anyway, you can abuse the fact that parsing $ echo '"2015-03-05T23:51:47.487Z"' | jq 'strptime("%Y-%m-%dT%H:%M:%S.%GZ")'
[
2015,
2,
5,
23,
51,
47,
4,
63
] |
this seems to be platform-dependent... @hvdijk example above does not work for me: $ echo '"2015-03-05T23:51:47.487Z"' | jq 'strptime("%Y-%m-%dT%H:%M:%S.%GZ")'
jq: error (at <stdin>:1): date "2015-03-05T23:51:47.487Z" does not match format "%Y-%m-%dT%H:%M:%S.%GZ" I think this is because I am on macos ... I have similar problem with |
We might need to import an implementation of |
I ran into this issue today while massaging logs. Did this workaround: Put text below into # replace fromdateiso8601 and fromdate with ones that supports fractional seconds
# NOTE: does not support timezones
# Usage:
# $ jq -n -L . 'include "fromdate"; "2024-02-13T11:10:32.123Z" | fromdate'
# 1707822632.123
def fromdateiso8601:
( capture("(?<y>\\d+)-(?<m>\\d+)-(?<d>\\d+)T(?<H>\\d+):(?<M>\\d+):(?<S>\\d+)(?<F>\\.\\d+)?Z") as {$y,$m,$d,$H,$M,$S,$F}
| [$y,$m,$d,$H,$M,$S,0,0]
| map(tonumber)
| .[1] |= .-1 # month starts at 0
| mktime + ($F | if . then tonumber else 0 end)
) // error("date \"\(.)\" does not match format");
def fromdate: fromdateiso8601; Had a quick look into fixing it in jq, is a bit messy. Maybe the least painful way is to modify your own |
I suspect not. The standard requires ksh solves it by having a |
@hvdijk Thanks for the info and break down! if someone would look into this i think there are at least two viable paths as i see it:
Another concern is how to support both integer seconds and optionally fractions. A maybe not optimal but straight forward solution is to redefine Also i'm also not sure if something like this should be coordinated with other date/time improvement, there is a bunch of issues or old open PRs touching similar things. |
nicowilliams pointed out in #1413 that there is a lot of variance in other implementations. Ruby appears to use
In Python, there is an open issue to add support for this in some way, python/cpython#100929. No way has been picked yet. In Ruby, I cannot find anything indicating support for this. In R, with In .NET (
Good point. Since you specifically say |
Great summary, learned a lot, and it seems we're not alone with this mess :) personally i like the R approach with the @hvdijk is this something you would like to work on? not sure how much time/motivation i have to put into it atm. |
I'm happy to have a more in-depth look into this and related |
👍 sounds good |
Representation in
|
Thanks 👍 that summary will be very useful for me or someone that want to implement support for this. By wrapping you mean have a functions that parse/massaga the format in way that I wonder if an alternativ is to import some @nicowilliams when you have time: any thought? |
Basically yes, I was thinking we can implement |
Yes, I think we're going to have to import an implementation of |
Yes, I agree with the need. I was just making some scripts that I wrote on Linux available cross-platform, so they will also work on Windows. One of the issues in doing so is that I need to modify many of the |
Here the author writes:
#364 (comment)
What we need is
similar to how python does it.
The text was updated successfully, but these errors were encountered: