Linux中是否有shell命令以毫秒为单位获取时间?
当前回答
在OS X上,date不支持%N标志,我建议使用Homebrew安装coreutils。这将使您能够访问一个名为gdate的命令,其行为与Linux系统上的date相同。
brew install coreutils
为了获得更“原生”的体验,你可以把这个添加到你的.bash_aliases中:
alias date='gdate'
然后执行
$ date +%s%N
其他回答
用时间和时区显示日期
日期+"%d-%m-%Y %T "。% N % Z”
输出22
我想从bash生成值,并在Java代码中使用该值转换回日期(Java .util)。
以下命令为我在bash文件中生成值:
日期+ % s000
Nano是10−9,milli是10−3。因此,我们可以使用纳秒的前三个字符来得到毫秒:
date +%s%3N
男人约会:
%N纳秒(000000000..999999999) 1970-01-01 00:00:00 UTC以来的%s秒
来源:服务器故障如何在Bash中以毫秒为单位获得当前Unix时间?
下面是一个在Linux上以毫秒为单位获取时间的便携式hack:
#!/bin/sh
read up rest </proc/uptime; t1="${up%.*}${up#*.}"
sleep 3 # your command
read up rest </proc/uptime; t2="${up%.*}${up#*.}"
millisec=$(( 10*(t2-t1) ))
echo $millisec
输出结果为:
3010
这是一个非常廉价的操作,它与shell内部程序和procfs一起工作。
当你从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()以微秒精度返回时间。