date +"%T.%N"
returns the current time with nanoseconds.
06:46:41.431857000
date +"%T.%6N"
returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds.
06:47:07.183172
date +"%T.%3N"
returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.
06:47:42.773
date
命令的格式中的每个字段都可以指定一个可选的字段宽度。%xN
:对于字段宽度来说真不错! - fduffdate +%s%N
返回当前秒数和纳秒数的总和。
因此,echo $(($(date +%s%N)/1000000))
是你所需要的。
示例:
$ echo $(($(date +%s%N)/1000000))
1535546718115
date +%s
返回自计算机元年以来经过的秒数,如果这个信息对你有用的话。
date
不支持%N
。 - yegor256coreutils
软件包中的GNU版日期命令,然后使用gdate
命令。请参考这个问题:https://dev59.com/j2kw5IYBdhLWcg3wkLX2 - PierzNano 是 10−9,而 milli 是 10−3。因此,我们可以使用纳秒的前三个字符来获取毫秒:
date +%s%3N
来自man date
:
%N 纳秒 (000000000..999999999)
%s 自1970年01月01日00时00分00秒UTC以来的秒数
来源:Server Fault的如何在Bash中获取当前Unix时间的毫秒数?。
date
命令不支持 %N
标志,建议使用Homebrew 安装 coreutils
。这将使您可以使用名为 gdate
的命令,其行为与 Linux 系统上的 date
命令相同。brew install coreutils
为了获得更“本地”的体验,您可以将以下内容添加到您的 .bash_aliases
文件中:
对于更好的理解,请查看原文。
alias date='gdate'
然后执行
$ date +%s%N
#!/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
(( t = ${EPOCHREALTIME/[^0-9]/} / 1000 ))
或者
t=${EPOCHREALTIME/[^0-9]/} # remove the decimal separator (s → µs)
t=${t%???} # remove the last three digits (µs → ms)
t
将会是类似于1547624774371
的东西。1695071065,749507
。您可以通过设置 LC_ALL=C
来强制使用句点作为小数点分隔符。 - undefinedLC_ALL
有些复杂,因为我们要么必须在子shell中使用它(这会使设置t
变得复杂),要么手动恢复它。这两种方法都不理想。但是我找到了一个简短的通用解决方案,并更新了答案。 - undefined%N
格式选项,并且没有Python或Perl解释器。adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
adjtimex
的输出中提取秒和微秒(通常用于设置系统时钟选项),并将它们打印在一起,没有换行符。请注意,微秒字段必须预先用零填充,但这不会影响秒字段,因为它本来就比六位数长。从中转换微秒到毫秒应该很容易。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
date
命令在 OS X 上不提供毫秒级别的时间,因此使用来自 Python 的别名。
millis(){ python -c "import time; print(int(time.time()*1000))"; }
或者
alias millis='python -c "import time; print(int(time.time()*1000))"'
编辑:按照@CharlesDuffy的评论。 分叉任何子进程都需要额外的时间。
$ time date +%s%N
1597103627N
date +%s%N 0.00s user 0.00s system 63% cpu 0.006 total
Python仍在改进其VM启动时间,但它并不像预编译代码(如date
)那样快。
在我的计算机上,它需要大约30ms-60ms(即date
所需的6ms的5倍至10倍)。
$ time python -c "import time; print(int(time.time()*1000))"
1597103899460
python -c "import time; print(int(time.time()*1000))" 0.03s user 0.01s system 83% cpu 0.053 total
我认为 awk
比 python
更轻量级,因此 awk
处理的时间范围在6毫秒到12毫秒之间(即 date 的1倍到2倍):
$ time awk '@load "time"; BEGIN{print int(1000 * gettimeofday())}'
1597103729525
awk '@load "time"; BEGIN{print int(1000 * gettimeofday())}' 0.00s user 0.00s system 74% cpu 0.010 total
fork()
创建了一个独立的进程,执行了Python解释器并让它加载库/初始化,写入其结果并退出后,该结果将不再准确。 - Charles Duffy显示日期、时间及时区:
date +"%d-%m-%Y %T.%N %Z"
输出:22-04-2020 18:01:35.970289239 IST
brew install coreutils
,这样我们就可以使用 gdate
。否则,在 Linux 上,只需使用 date
。并且此函数将帮助您计算命令的时间而无需创建临时文件或其他任何内容:function timeit() {
start=`gdate +%s%N`
bash -c $1
end=`gdate +%s%N`
runtime=$(((end-start)/1000000000.0))
echo " seconds"
}
而且你可以将它与字符串一起使用:
timeit 'tsc --noEmit'
start=$(date +%s%N);
$*;
end=$(date +%s%N);
runtime=$(((end-start)/1000000));
echo "$runtime ms"
}```
- Uluaiv
date +%s.%N
会给您纳秒级的时间戳,您可以使用它吗? - Wrikkendate -Ins
- SO Stinks