Strptime with timezones and jq

穿精又带淫゛_ 提交于 2019-12-04 05:34:38

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/49262889/strptime-with-timezones-and-jq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!