C TimeVal结构秒和微秒
如何知道1970年1月1日00:00:00到现在使用timeval之间的秒和微秒数?谢谢C TimeVal结构秒和微秒,c,C,如何知道1970年1月1日00:00:00到现在使用timeval之间的秒和微秒数?谢谢 struct timeval { long tv_sec; /*seconds since 1/1/1970*/ long tv_usec; /*microseconds since tv_sec*/ }; 您可以调用gettimeofday() 但是gettimeofday()已经过时,本手册建议改为: 为什么您认为timeval是您问题的解决方案?当您说使用timeval时,您的意思是要将19
struct timeval {
long tv_sec; /*seconds since 1/1/1970*/
long tv_usec; /*microseconds since tv_sec*/
};
您可以调用
gettimeofday()
但是gettimeofday()
已经过时,本手册建议改为:
为什么您认为
timeval
是您问题的解决方案?当您说使用timeval时,您的意思是要将1970年的秒/微秒添加到timeval吗?struct timespec
的第二个字段是tv\u nsec
,而不是tv\u usec
。
struct timeval tv;
gettimeofday(&tv, NULL);
tv.tv_sec /* seconds */
tv.tv_usec /* microseconds */
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
tp.tv_sec /* seconds */
tp.tv_usec /* nanoseconds divide by 1000 to get microseconds*/