Jq: Parsing of milliseconds in dates

Created on 13 May 2017  路  4Comments  路  Source: stedolan/jq

Here the author writes:

Would it also parse ISO timestamps like 2015-03-05T19:40:53.324Z? Eg. what you get from javascript new Date().toISOString().

https://github.com/stedolan/jq/issues/364#issuecomment-77442886

What we need is

strptime("%Y-%m-%dT%H:%M:%S.%fZ")

similar to how python does it.

feature request

Most helpful comment

I think this is an issue about %f support which jq-1.6 does not support.

 % echo '"2015-03-05T23:51:47.487Z"' | jq 'strptime("%Y-%m-%dT%H:%M:%S.%fZ")'
jq: error (at <stdin>:1): date "2015-03-05T23:51:47.487Z" does not match format "%Y-%m-%dT%H:%M:%S.%fZ"
 % python -c 'from datetime import datetime; print(datetime.strptime("2015-03-05T23:51:47.487Z", "%Y-%m-%dT%H:%M:%S.%fZ"))'
2015-03-05 23:51:47.487000

All 4 comments

is progress in https://github.com/stedolan/jq/pull/1413#issuecomment-351315539 kind of abandoned?

hi @juliangamble
jq-1.6 had supports strptime usage.you can try it

root@oss-001:test_jq#  echo '"2015-03-05T23:51:47Z"' | jq 'strptime("%Y-%m-%dT%H:%M:%SZ")'
[
  2015,
  2,
  5,
  23,
  51,
  47,
  4,
  63
]

You can see detailed usage in the manual or jq-1.6 manual.yml.
https://github.com/stedolan/jq/blob/master/docs/content/manual/v1.6/manual.yml

I think this is an issue about %f support which jq-1.6 does not support.

 % echo '"2015-03-05T23:51:47.487Z"' | jq 'strptime("%Y-%m-%dT%H:%M:%S.%fZ")'
jq: error (at <stdin>:1): date "2015-03-05T23:51:47.487Z" does not match format "%Y-%m-%dT%H:%M:%S.%fZ"
 % python -c 'from datetime import datetime; print(datetime.strptime("2015-03-05T23:51:47.487Z", "%Y-%m-%dT%H:%M:%S.%fZ"))'
2015-03-05 23:51:47.487000

Struggled today with this, problem is that the unix "strptime" doesn't support milliseconds. As such, I switched to using a regex replace

.last_updated |= sub("(?<time>.*)\\..*Z"; "\(.time)Z")

So I'm transforming this "2015-03-05T23:51:47.487Z" to "2015-03-05T23:51:47Z"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kaihendry picture kaihendry  路  4Comments

rokka-n picture rokka-n  路  4Comments

ve3ied picture ve3ied  路  4Comments

tbelaire picture tbelaire  路  4Comments

neowulf picture neowulf  路  3Comments