4

我如何知道从 1970 年 1 月 1 日 00:00:00 到现在使用 timeval 之间的秒数和微秒数?谢谢。

struct timeval {
  long tv_sec; /*seconds since 1/1/1970*/
  long tv_usec; /*microseconds since tv_sec*/
};
4

1 回答 1

4

你打电话gettimeofday()

struct timeval tv;
gettimeofday(&tv, NULL);
tv.tv_sec  /* seconds */
tv.tv_usec /* microseconds */

但是gettimeofday()已经过时了,该手册建议使用clock_gettime(2)代替:

struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
tp.tv_sec  /* seconds */
tp.tv_usec /* nanoseconds divide by 1000 to get microseconds*/
于 2012-12-01T17:27:41.403 回答