900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Linux C获取当前时间(精确到微秒)

Linux C获取当前时间(精确到微秒)

时间:2022-07-07 10:26:15

相关推荐

Linux C获取当前时间(精确到微秒)

获取当前的时间的秒数和微秒数本方法需要用到gettimeofday()函数,该函数需要引入的头文件是sys/time.h

// sys/time.hstruct timeval{long tv_sec; //秒 long tv_usec; //微秒 }; struct timezone {int tz_minuteswest; //格林威治时间往西方的时差,即和格林威治差了多少分钟 int tz_dsttime;//DST 时间的修正方式}; /*** @params struct timeval* @params struct timezone* @return 成功时返回0,失败时返回-1 */int gettimeofday (struct timeval * tv, struct timezone * tz);

#include <stdlib.h>#include <stdio.h>#include <sys/time.h>#include <unistd.h>int main(){struct timeval tv;gettimeofday(&tv,NULL);printf("second:%ld\n",tv.tv_sec);//秒printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); //毫秒printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec); //微秒sleep(3); printf("3s later:");gettimeofday(&tv,NULL);printf("second:%ld\n",tv.tv_sec);//秒printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); //毫秒printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec); //微秒return 0;}

参考文献:

/li_wen01/article/details/80422340

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。