Linux 命令以毫秒为单位获取时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16548528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:56:11  来源:igfitidea点击:

Command to get time in milliseconds

linuxbashshelltime

提问by MOHAMED

Is there a shell command in Linux to get the time in milliseconds?

Linux 中是否有 shell 命令以毫秒为单位获取时间?

采纳答案by Alper

date +%s%Nreturns the number of seconds + current nanoseconds.

date +%s%N返回秒数 + 当前纳秒。

Therefore, echo $(($(date +%s%N)/1000000))is what you need.

因此,echo $(($(date +%s%N)/1000000))正是您所需要的。

Example:

例子:

$ echo $(($(date +%s%N)/1000000))
1535546718115

date +%sreturns the number of seconds since the epoch, if that's useful.

date +%s如果有用,则返回自纪元以来的秒数。

回答by Michael Defort

  • 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 +"%T.%N"以纳秒为单位返回当前时间。

    06:46:41.431857000
    
  • date +"%T.%6N"返回当前时间,纳秒四舍五入到前 6 位数字,即微秒。

    06:47:07.183172
    
  • date +"%T.%3N"返回当前时间,纳秒四舍五入到前 3 位数字,即毫秒。

    06:47:42.773
    

In general, every field of the datecommand's format can be given an optional field width.

通常,date命令格式的每个字段都可以指定一个可选的字段宽度。

回答by maoyang

A Python script like this:

像这样的 Python 脚本:

import time
cur_time = int(time.time()*1000)

回答by fedorqui 'SO stop harming'

Nano is 10?9and milli 10?3. Hence, we can use the three first characters of nanoseconds to get the milliseconds:

纳米是 10 ?9和毫 10 ?3。因此,我们可以使用纳秒的前三个字符来获取毫秒:

date +%s%3N

From man date:

来自man date

%Nnanoseconds (000000000..999999999)

%sseconds since 1970-01-01 00:00:00 UTC

%N纳秒 (000000000..999999999)

自 1970-01-01 00:00:00 UTC 以来的%s

Source: Server Fault's How do I get the current Unix time in milliseconds in Bash?.

来源:Server Fault 的如何在 Bash 中以毫秒为单位获取当前的 Unix 时间?.

回答by Joshua Cook

On OS X, where datedoes not support the %Nflag, I recommend installing coreutilsusing Homebrew. This will give you access to a command called gdatethat will behave as datedoes on Linux systems.

date不支持该%N标志的OS X 上,我建议coreutils使用Homebrew 进行安装。这将使您可以访问一个名为的命令,该命令的gdate行为与dateLinux 系统上的行为相同。

brew install coreutils

For a more "native" experience, you can always add this to your .bash_aliases:

为了获得更“原生”的体验,您可以随时将其添加到您的.bash_aliases

alias date='gdate'

Then execute

然后执行

$ date +%s%N

回答by pghalliday

The other answers are probably sufficient in most cases but I thought I'd add my two cents as I ran into a problem on a BusyBoxsystem.

在大多数情况下,其他答案可能就足够了,但我想我会在BusyBox系统上遇到问题时加两分钱。

The system in question did not support the %Nformat option and doesn't have no Python or Perl interpreter.

有问题的系统不支持%N格式选项,也没有 Python 或 Perl 解释器。

After much head scratching, we (thanks Dave!) came up with this:

经过多次挠头,我们(感谢戴夫!)想出了这个:

adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", ) }'

It extracts the seconds and microseconds from the output of adjtimex(normally used to set options for the system clock) and prints them without new lines (so they get glued together). Note that the microseconds field has to be pre-padded with zeros, but this doesn't affect the seconds field which is longer than six digits anyway. From this it should be trivial to convert microseconds to milliseconds.

它从adjtimex(通常用于设置系统时钟的选项)的输出中提取秒和微秒,并在没有换行的情况下打印它们(因此它们粘在一起)。请注意,微秒字段必须预先填充零,但这不会影响超过六位数的秒字段。由此,将微秒转换为毫秒应该是微不足道的。

If you need a trailing new line (maybe because it looks better) then try

如果您需要尾随新行(可能是因为它看起来更好),请尝试

adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", ) }' && printf "\n"

Also note that this requires adjtimexand awkto be available. If not then with BusyBox you can point to them locally with:

另请注意,这需要adjtimex并且awk可用。如果没有,那么使用 BusyBox,您可以使用以下命令在本地指向它们:

ln -s /bin/busybox ./adjtimex
ln -s /bin/busybox ./awk

And then call the above as

然后将上面的称为

./adjtimex | ./awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", ) }'

Or of course you could put them in your PATH

或者当然你可以把它们放在你的 PATH

EDIT:

编辑:

The above worked on my BusyBox device. On Ubuntu I tried the same thing and realised that adjtimexhas different versions. On Ubuntu this worked to output the time in seconds with decimal places to microseconds (including a trailing new line)

以上适用于我的 BusyBox 设备。在 Ubuntu 上,我尝试了同样的事情并意识到它adjtimex有不同的版本。在 Ubuntu 上,这可以以秒为单位输出时间,小数位到微秒(包括尾随的新行)

sudo apt-get install adjtimex
adjtimex -p | awk '/raw time:/ { print  }'

I wouldn't do this on Ubuntu though. I would use date +%s%N

不过我不会在 Ubuntu 上这样做。我会用date +%s%N

回答by Thamme Gowda

datecommand didnt provide milli seconds on OS X, so used an alias from python

date命令在 OS X 上没有提供毫秒,所以使用了 python 的别名

millis(){  python -c "import time; print(int(time.time()*1000))"; }

OR

或者

alias millis='python -c "import time; print(int(time.time()*1000))"'

回答by Chet

I just wanted to add to Alper's answerwhat I had to do to get this stuff working:

我只是想在Alper 的回答中添加我必须做的事情才能使这些东西正常工作:

On Mac, you'll need brew install coreutils, so we can use gdate. Otherwise on Linux, it's just date. And this function will help you time commands without having to create temporary files or anything:

在 Mac 上,您需要brew install coreutils,因此我们可以使用gdate. 否则在 Linux 上,它只是date. 此功能将帮助您计时命令,而无需创建临时文件或任何东西:

function timeit() {
    start=`gdate +%s%N`
    bash -c 
    end=`gdate +%s%N`
    runtime=$(((end-start)/1000000000.0))
    echo " seconds"
}

And you can use it with a string:

您可以将它与字符串一起使用:

timeit 'tsc --noEmit'

回答by Bastian Bittorf

Here is a somehow portable hack for Linux for getting time in milliseconds:

这是 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

The output is:

输出是:

3010

This is a very cheap operation, which works with shell internals and procfs.

这是一个非常便宜的操作,它适用于 shell 内部和 procfs。

回答by Lorinczy Zsigmond

Perl can be used for this, even on exotic platforms like AIX. Example:

Perl 可以用于此目的,即使在像 AIX 这样的特殊平台上也是如此。例子:

#!/usr/bin/perl -w

use strict;
use Time::HiRes qw(gettimeofday);

my ($t_sec, $usec) = gettimeofday ();
my $msec= int ($usec/1000);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
    localtime ($t_sec);

printf "%04d-%02d-%02d %02d:%02d:%02d %03d\n",
    1900+$year, 1+$mon, $mday, $hour, $min, $sec, $msec;