如何从 Linux 中的 C 获取当前时间(以毫秒为单位)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3756323/
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-03 23:35:49  来源:igfitidea点击:

How to get the current time in milliseconds from C in Linux?

clinuxposix

提问by LLL

How do I get the current time on Linux in milliseconds?

如何以毫秒为单位获取 Linux 上的当前时间?

回答by Donal Fellows

Use gettimeofday()to get the time in seconds and microseconds. Combining and rounding to milliseconds is left as an exercise.

使用gettimeofday()以获得秒和毫秒的时间。合并和四舍五入到毫秒留作练习。

回答by yadab

You have to do something like this:

你必须做这样的事情:

struct timeval  tv;
gettimeofday(&tv, NULL);

double time_in_mill = 
         (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ; // convert tv_sec & tv_usec to millisecond

回答by Eric Wang

Following is the util function to get current timestamp in milliseconds:

以下是以毫秒为单位获取当前时间戳的 util 函数:

#include <sys/time.h>

long long current_timestamp() {
    struct timeval te; 
    gettimeofday(&te, NULL); // get current time
    long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
    // printf("milliseconds: %lld\n", milliseconds);
    return milliseconds;
}

About timezone:

关于时区

gettimeofday()support to specify timezone, I use NULL, which ignore the timezone, but you can specify a timezone, if need.

gettimeofday()支持指定时区,我使用NULL,它忽略时区,但如果需要,您可以指定时区。



@Update - timezone

@Update - 时区

Since the longrepresentation of time is not relevant to or effected by timezone itself, so setting tzparam of gettimeofday() is not necessary, since it won't make any difference.

由于long时间的表示与时区本身无关或不受时区本身的影响,因此tz不需要设置gettimeofday() 的参数,因为它不会产生任何区别。

And, according to manpage of gettimeofday(), the use of the timezonestructure is obsolete, thus the tzargument should normally be specified as NULL, for details please check the man page.

并且,根据 的手册gettimeofday(),该timezone结构的使用已过时,因此tz参数通常应指定为 NULL,有关详细信息,请查看手册页。

回答by Dan Moulding

This can be achieved using the POSIX clock_gettimefunction.

这可以使用POSIXclock_gettime函数来实现。

In the current version of POSIX, gettimeofdayis marked obsolete. This means it may be removed from a future version of the specification. Application writers are encouraged to use the clock_gettimefunction instead of gettimeofday.

在当前版本的 POSIX 中,gettimeofday标记为 obsolete。这意味着它可能会从规范的未来版本中删除。鼓励应用程序编写者使用该clock_gettime函数而不是gettimeofday.

Here is an example of how to use clock_gettime:

以下是如何使用的示例clock_gettime

#define _POSIX_C_SOURCE 200809L

#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <time.h>

void print_current_time_with_ms (void)
{
    long            ms; // Milliseconds
    time_t          s;  // Seconds
    struct timespec spec;

    clock_gettime(CLOCK_REALTIME, &spec);

    s  = spec.tv_sec;
    ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds
    if (ms > 999) {
        s++;
        ms = 0;
    }

    printf("Current time: %"PRIdMAX".%03ld seconds since the Epoch\n",
           (intmax_t)s, ms);
}

If your goal is to measure elapsed time, and your system supports the "monotonic clock" option, then you should consider using CLOCK_MONOTONICinstead of CLOCK_REALTIME.

如果您的目标是测量经过的时间,并且您的系统支持“单调时钟”选项,那么您应该考虑使用CLOCK_MONOTONIC代替CLOCK_REALTIME