Convert unix timestamp to hh:mm:ss:SSS (where SSS is milliseconds) in AWK

旧巷老猫 提交于 2019-12-11 01:02:18

问题


How can I convert unix timestamp to hh:mm:ss:SSS (where SSS is milliseconds) in AWK.

For example:

echo 1456478048306 > time
cat time | awk ....... > readable_time_format

回答1:


You can use bash for that:

#!/bin/bash
ts="1456478048306"
unix_epoch=${ts:0:-3}
ms=${ts:((-3)):3}

echo "$(date -d@"${unix_epoch}" +%H:%M:%S):${ms}"

Btw, of course you can also use awk. However awk does not allow to simplify things here a lot, the algorithm is more or less the same:

awk -v ts="1456478048306" '{
    unix_epoch=substr(ts, 0, length(ts)-3)
    ms=substr(ts, length(ts)-2, 3)
    print strftime("%H:%M:%S", unix_epoch) ":" ms
}'


来源:https://stackoverflow.com/questions/35648508/convert-unix-timestamp-to-hhmmsssss-where-sss-is-milliseconds-in-awk

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