如何使用jq将日期转换为毫秒



在我的json文件file1。json,我有一个"horodate"。带毫秒的字段:

{
"horodate" : "2023-03-03T10:10:10.425Z"
}

我想在bash脚本中使用jq 1.6将此日期转换为毫秒精度的时间戳:

{
"horodate" : "1677838210425"
}

我试过了:

jq '[.[] | .horodate |= (strptime("%Y-%m-%dT%H:%M:%S.%sZ")| mktime)]' file1.json > file2.json

或:

jq '[.[] | .horodate |= ( sub(".[0-9]+Z$"; "Z") | fromdate * 1000 + (.[20:23]|tonumber))]' file1.json > file2.json
# Input: ISO8601 time in the format specified by $fmt where 
# $fmt should end with "%SZ",
# and where the value of %S may include milliseconds.
# Output: milliseconds since the Unix epoch
def toMilliseconds($fmt):
def toM:
capture("(?<string>.*)[.](?<milliseconds>[0-9]+)Z")
| .string += "Z"
| .milliseconds |= tonumber;
(toM // {string: ., milliseconds: 0})
| (.string | strptime($fmt) | mktime) * 1000 + .milliseconds ;
{
"horodate" : "2023-03-03T10:10:10.425Z"
}
| .horodate
| toMilliseconds("%Y-%m-%dT%H:%M:%SZ")

输出:

1677838210425

相关内容

  • 没有找到相关文章

最新更新