Skip to content
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 doesn't support fractional seconds #1117

Open
bryanlarsen opened this issue Mar 21, 2016 · 10 comments
Open

fromdate doesn't support fractional seconds #1117

bryanlarsen opened this issue Mar 21, 2016 · 10 comments

Comments

@bryanlarsen
Copy link

Using jq-1.5 from https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64

AFAIK, fractional seconds are valid ISO8601.

jq: error (at <stdin>:1180): date "2016-03-17T17:06:59.764Z" does not match format "%Y-%m-%dT%H:%M:%SZ"

I don't care about the milliseconds, perhaps some people do. That's a harder problem, since you output in an int. But for people who don't care, perhaps you can make a fromdate variant that drops the milliseconds.

The workaround for now for me is:

| sub(".[0-9]+Z$"; "Z") | fromdate

@mauvm
Copy link

mauvm commented Aug 22, 2016

Thanks for the workaround! This helps parsing Bunyan log records. I use a slightly altered regex to make sure it's a literal dot:

| sub("\\.[0-9]+Z$"; "Z") | fromdate

@phiresky
Copy link

phiresky commented Nov 8, 2017

This is especially annoying as JSON.stringify produces these strings (e.g. JSON.stringify({now: new Date()}) => {now: "2017-11-08T01:00:36.383Z"} which are annoying to parse with jq.

@DBarthe
Copy link

DBarthe commented Mar 20, 2019

To parse iso8601 dates with timezone offset (#1053 ) and fractional seconds, this should work :

sub("\\.[0-9]+\\+"; "+") | strptime("%Y-%m-%dT%H:%M:%S%z")

@elyscape
Copy link

That'll only work with a positive timezone offset. An improved version that supports all offsets would be:

sub("(?<time>T[0-9:]+)(\\.\\d+)?(?<tz>Z|[+\\-]\\d{2}:?(\\d\\d)?)$"; .time + .tz) | strptime("%Y-%m-%dT%H:%M:%S%z")

It's a lot uglier, though.

@jahmad-fareportal
Copy link

That'll only work with a positive timezone offset. An improved version that supports all offsets would be:

sub("(?<time>T[0-9:]+)(\\.\\d+)?(?<tz>Z|[+\\-]\\d{2}:?(\\d\\d)?)$"; .time + .tz) | strptime("%Y-%m-%dT%H:%M:%S%z")

It's a lot uglier, though.

@elyscape That works the best !!

@n2N8Z
Copy link

n2N8Z commented May 28, 2021

@DBarthe @elyscape these regexes delete the fractional seconds, which doesn't address the problem reported in this issue. Using these modified timestamps in a sort_by will result in incorrect order.

@elyscape
Copy link

elyscape commented May 28, 2021

I mean yes, but it works around not being able to parse the date at all.

@mluis7
Copy link

mluis7 commented Aug 12, 2021

Yet another workaround to convert an iso8601 date as "2021-08-11T00:49:38.359Z" to a Unix timestamp in milliseconds. For example, the startedDateTime value from a HAR file

 ((.startedDateTime[0:19] + "Z"|fromdateiso8601)*1000 + (.startedDateTime[20:23]|tonumber))

Result: 1628642978359

date -d "2021-08-11T00:49:38.359Z" '+%s'
1628642978

So: (1628642978 * 1000) + 359

@andrewpollock
Copy link

My use case is processing https://nvd.nist.gov/developers/vulnerabilities data in https://csrc.nist.gov/schema/nvd/api/2.0/cve_api_json_2.0.schema, which seems to be in RFC 3339 format, e.g.:

1988-10-01T04:00:00.000

@wader
Copy link
Member

wader commented Feb 14, 2024

I posted a workaround in another issue that might be interesting #1409 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests