获取关于C中微秒时间戳? [英] Get a timestamp in C in microseconds?

查看:1478
本文介绍了获取关于C中微秒时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得微秒时间戳使用C?

How do I get a microseconds timestamp in C?

我想要做的:

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

但是,这会返回一些胡说八道的价值,如果我得到两个时间戳,第二个可以比第一次更小或更大(第二个应该的总是更大)。有没有可能通过返回的神奇整数转换的 gettimeofday的以它实际上可以用?工作了正常数量

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). Would it be possible to convert the magic integer returned by gettimeofday to a normal number which can actually be worked with?

推荐答案

您需要在秒添加,也:

unsigned long time_in_micros = 1000000 * tv_sec + tv_usec;

请注意,这将仅持续约2 32 / 10 6 =〜4295秒,或者虽然大致71分钟(一个典型的32位系统上)。

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

这篇关于获取关于C中微秒时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆