Linux中是否有shell命令以毫秒为单位获取时间?
当前回答
date +%s%N返回秒数+当前纳秒。
因此,echo $(($(date +%s%N)/1000000))就是您所需要的。
例子:
$ echo $(($(date +%s%N)/1000000))
1535546718115
Date +%s返回自epoch以来的秒数,如果有用的话。
其他回答
date +%s%N返回秒数+当前纳秒。
因此,echo $(($(date +%s%N)/1000000))就是您所需要的。
例子:
$ echo $(($(date +%s%N)/1000000))
1535546718115
Date +%s返回自epoch以来的秒数,如果有用的话。
当你从4.1版本开始使用GNU AWK时,你可以加载时间库并执行以下操作:
$ awk '@load "time"; BEGIN{printf "%.6f", gettimeofday()}'
这将以秒为单位打印自1970-01-01T00:00:00以来的当前时间,精度为亚秒。
the_time = gettimeofday() Return the time in seconds that has elapsed since 1970-01-01 UTC as a floating-point value. If the time is unavailable on this platform, return -1 and set ERRNO. The returned time should have sub-second precision, but the actual precision may vary based on the platform. If the standard C gettimeofday() system call is available on this platform, then it simply returns the value. Otherwise, if on MS-Windows, it tries to use GetSystemTimeAsFileTime(). source: GNU awk manual
在Linux系统上,标准C函数getimeofday()以微秒精度返回时间。
我想从bash生成值,并在Java代码中使用该值转换回日期(Java .util)。
以下命令为我在bash文件中生成值:
日期+ % s000
在大多数情况下,其他答案可能已经足够了,但当我在BusyBox系统上遇到问题时,我想补充一下我的意见。
该系统不支持%N格式选项,也没有Python或Perl解释器。
在绞尽脑汁之后,我们(感谢戴夫!)想出了这个:
adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
它从adjtimex(通常用于设置系统时钟选项)的输出中提取秒和微秒,并打印它们而不添加新行(因此它们粘在一起)。请注意,microseconds字段必须预先用0填充,但这不会影响seconds字段,因为它长于6位数字。由此,将微秒转换为毫秒应该是很简单的。
如果你需要一个尾随的新行(也许因为它看起来更好),那么试试
adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }' && printf "\n"
还要注意,这需要adjtimex和awk可用。如果没有,那么与BusyBox你可以在本地指向他们:
ln -s /bin/busybox ./adjtimex
ln -s /bin/busybox ./awk
然后把上面的称为
./adjtimex | ./awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
当然,你也可以把它们放在你的PATH中
编辑:
以上这些都适用于我的BusyBox设备。在Ubuntu上我尝试了同样的事情,并意识到adjtimex有不同的版本。在Ubuntu上,这可以输出以秒为单位的时间,小数位为微秒(包括后面的新行)
sudo apt-get install adjtimex
adjtimex -p | awk '/raw time:/ { print $6 }'
不过我不会在Ubuntu上这么做。我会使用date +%s%N
纯bash解决方案
自bash 5.0(2019年1月7日发布)以来,您可以使用内置变量EPOCHREALTIME,它包含自epoch以来的秒数,包括小数点后精确到微秒(echo $EPOCHREALTIME输出类似1547624774.371210)。通过移除。最后三个地方是毫秒:
要么使用
(( t = ${EPOCHREALTIME/./} / 1000 ))
或者类似的东西
t=${EPOCHREALTIME/./} # remove the dot (s → µs)
t=${t%???} # remove the last three digits (µs → ms)
无论哪种方式t都是1547624774371。