C语言 以微秒为单位在 C 中获取时间戳?

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

Get a timestamp in C in microseconds?

ctime

提问by Kristina Brooks

How do I get a microseconds timestamp in C?

如何在 C 中获得微秒时间戳?

I'm trying to do:

我正在尝试做:

struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_usec;

But this returns some nonsense value that if I get two timestamps, the second one can be smaller or bigger than the first (second one should alwaysbe bigger). Would it be possible to convert the magic integer returned by gettimeofdayto a normal number which can actually be worked with?

但这会返回一些无意义的值,如果我得到两个时间戳,第二个可以比第一个小或大(第二个应该总是更大)。是否可以将gettimeofday返回的魔术整数转换为实际可以使用的正常数字?

回答by unwind

You need to add in the seconds, too:

您还需要在几秒钟内添加:

unsigned long time_in_micros = 1000000 * tv.tv_sec + tv.tv_usec;

Note that this will only last for about 232/106=~ 4295 seconds, or roughly 71 minutes though (on a typical 32-bit system).

请注意,这只会持续大约 2 32/10 6=~ 4295 秒,或者大约 71 分钟(在典型的 32 位系统上)。

回答by Rob?

You have two choices for getting a microsecond timestamp. The first (and best) choice, is to use the timevaltype directly:

获取微秒时间戳有两种选择。第一个(也是最好的)选择是timeval直接使用类型:

struct timeval GetTimeStamp() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv;
}

The second, and for me less desirable, choice is to build a uint64_t out of a timeval:

第二个,对我来说不太理想,选择是从 a 中构建一个 uint64_t timeval

uint64_t GetTimeStamp() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
}

回答by janneb

struct timeval contains two components, the second and the microsecond. A timestamp with microsecond precision is represented as seconds since the epoch stored in the tv_sec field and the fractional microseconds in tv_usec. Thus you cannot just ignore tv_sec and expect sensible results.

struct timeval 包含两个组件,秒和微秒。具有微秒精度的时间戳表示为自纪元存储在 tv_sec 字段和 tv_usec 中的小数微秒以来的秒数。因此,您不能只是忽略 tv_sec 并期望获得合理的结果。

If you use Linux or *BSD, you can use timersub() to subtract two struct timeval values, which might be what you want.

如果您使用 Linux 或 *BSD,您可以使用 timersub() 来减去两个 struct timeval 值,这可能是您想要的。

回答by zoul

But this returns some nonsense value that if I get two timestamps, the second one can be smaller or bigger than the first (second one should always be bigger).

但这会返回一些无意义的值,如果我得到两个时间戳,第二个可以比第一个小或大(第二个应该总是更大)。

What makes you think that? The value is probably OK. It's the same situation as with seconds and minutes – when you measure time in minutes and seconds, the number of seconds rolls over to zero when it gets to sixty.

什么让你有那个想法?该值可能还可以。这与秒和分的情况相同——当您以分和秒为单位测量时间时,秒数会在达到 60 时归零。

To convert the returned value into a “linear” number you could multiply the number of seconds and add the microseconds. But if I count correctly, one year is about 1e6*60*60*24*360?μsec and that means you'll need more than 32 bits to store the result:

要将返回值转换为“线性”数字,您可以乘以秒数并加上微秒。但如果我算对了,一年大约是 1e6*60*60*24*360?μsec,这意味着你需要超过 32 位来存储结果:

$ perl -E '$_=1e6*60*60*24*360; say int log($_)/log(2)'
44

That's probably one of the reasons to split the original returned value into two pieces.

这可能是将原始返回值分成两部分的原因之一。

回答by user6269400

use an unsigned long long (i.e. a 64 bit unit) to represent the system time:

使用 unsigned long long(即 64 位单位)来表示系统时间:

typedef unsigned long long u64;

u64 u64useconds;
struct timeval tv;

gettimeofday(&tv,NULL);
u64useconds = (1000000*tv.tv_sec) + tv.tv_usec;

回答by Osaid

First we need to know on the range of microseconds i.e. 000_000 to 999_999 (1000000 microseconds is equal to 1second). tv.tv_usec will return value from 0 to 999999 not 000000 to 999999so when using it with seconds we might get 2.1seconds instead of 2.000001 seconds because when only talking about tv_usec 000001 is essentially 1. Its better if you insert

首先我们需要知道微秒的范围,即 000_000 到 999_999(1000000 微秒等于 1 秒)。tv.tv_usec 将返回从 0 到 999999 而不是000000 到 999999 的值,所以当用秒来使用它时,我们可能会得到 2.1 秒而不是 2.000001 秒,因为当只谈论 tv_usec 000001 本质上是 1。如果你插入它会更好

if(tv.tv_usec<10)
{
 printf("00000");
} 
else if(tv.tv_usec<100&&tv.tv_usec>9)// i.e. 2digits
{
 printf("0000");
}

and so on...

等等...