eagle
eagle

Reputation: 890

Strptime with timezones and jq

Not sure what I am doing wrong here

getting_data | gunzip | jq -r '.time_field | strptime("%Y-%m-%dT%H:%M:%S.%fZ")'

The error comes back as such:

jq: error (at <stdin>:0): date "2018-03-13T14:00:17.1614661Z" does not 
match format "%Y-%m-%dT%H:%M:%S.%fZ"

The desired output would be 2018-03-13 14:00:17

Upvotes: 2

Views: 6993

Answers (2)

eagle
eagle

Reputation: 890

So I found a workaround to get around the ZULU offset and the nano-seconds since I do not really care so much about the nano-seconds. Not sure if it is efficient

echo '{"time_field": "2018-03-13T14:00:17.1234567Z"}' | jq -r '
.time_field 
| split(".")[0] 
| strptime("%Y-%m-%dT%H:%M:%S") 
| mktime 
| strftime("%F %X")'

Upvotes: 5

Charles Duffy
Charles Duffy

Reputation: 295679

The issue is not the timezone, but the nanoseconds field; %f is not available in standard strptime for C.

If you know your format won't change, there's no particular reason to use strptime or strftime at all:

jq -r '.time_field | sub("^(?<date>[[:digit:]-]+)T(?<time>[[:digit:]:]+)[.].*";
                         "\(.date) \(.time)")' \
  <<<'{"time_field": "2018-03-13T14:00:17.1614661Z"}'

...properly emits:

2018-03-13 14:00:17

Upvotes: 2

Related Questions