Printing current time in milliseconds or nanoseconds with printf builtin

后端 未结 2 1196
独厮守ぢ
独厮守ぢ 2021-01-05 07:05

We can print the current time with the builtin printf function, without needing to invoke an external command like date, like this:



        
2条回答
  •  既然无缘
    2021-01-05 07:45

    In bash 5, you can get microsecond precision from EPOCHREALTIME. However, printf itself has no way to access that directly, so you need to extract the microseconds yourself.

    $ echo $EPOCHREALTIME; printf '%(%F:%T)T.%d\n' "$EPOCHSECONDS" "${EPOCHREALTIME#*.}"; echo $EPOCHREALTIME
    1554006709.936990
    2019-03-31:00:31:49.937048
    1554006709.937083
    

    This takes a little time, but the result appears to be accurate to about 0.05 milliseconds.

提交回复
热议问题