Search code examples
bashjqstrptime

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


Solution

  • 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")'