如何在FreeBSD中获得以毫秒为单位的时间?

问题描述 投票:7回答:5

在Linux中,我可以使用以下命令以毫秒为单位找到当前时间:

date +%s%N

但是在FreeBSD上我只能得到

[13:38 ]#date +%s%N
1299148740N

如何在FreeBSD中获得以毫秒(或纳秒)为单位的时间?

shell freebsd
5个回答
3
投票

例如,使用gettimeofday()

#include <stdio.h>
#include <sys/time.h>
int main(void)
{
  struct timeval time_now;
    gettimeofday(&time_now,NULL);
    printf ("%ld secs, %ld usecs\n",time_now.tv_sec,time_now.tv_usec);

    return 0;
}

6
投票

BSD date命令不支持毫秒。如果您想要具有毫秒支持的date,请安装GNU coreutils软件包。

我在OS X上遇到了这个问题,它的date来自BSD。解决的方法是brew install coreutilsln -sf /usr/local/bin/gdate $HOME/bin,并确保$HOME/binPATH中排在首位。


1
投票

尝试使用tai64n中的tai64n

daemontools

0
投票

这里是最近的Perl的一线工具:

daemontools

((从$ echo | tai64n | tai64nlocal 2011-03-03 09:45:37.833010500 $ ps | tai64n | tai64nlocal 2011-03-03 09:52:30.817146500 PID TTY TIME CMD 2011-03-03 09:52:30.817150500 7154 pts/1 00:00:07 bash 2011-03-03 09:52:30.817157500 20099 pts/1 00:00:00 ps 2011-03-03 09:52:30.817159500 20100 pts/1 00:00:00 tai64n 2011-03-03 09:52:30.817162500 20101 pts/1 00:00:00 tai64nlocal 修改为类似的问题)


0
投票

获得毫秒的另一种方法是使用Python:

perl -MTime::HiRes=gettimeofday -MPOSIX=strftime -e '($s,$us) = gettimeofday(); printf "%d.%06d\n", $s, $us'
1487594425.611120

输出:

this answer

请注意,python 2.x仅返回百分之一秒。如果您的操作系统仅使用Python 2.x以获得更大的粒度,则可能需要稍作调整。

© www.soinside.com 2019 - 2024. All rights reserved.