失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > [c/c++][Linux] 时间戳转换为tm time_t timeval

[c/c++][Linux] 时间戳转换为tm time_t timeval

时间:2020-10-07 21:23:19

相关推荐

[c/c++][Linux] 时间戳转换为tm time_t timeval

ref

/GreenTeemo/article/details/102586265

说明

时间戳为/03/25 11:09:42 .761197,其中.761197为微秒。

code

#include <stdio.h>#include <time.h>#include <sys/time.h>int main(){char time[100] = "/03/25 11:09:42 .761197";char time_format[100] = "%Y/%m/%d %H:%M:%S";printf("origin is %s\n", time);/* to tm */struct tm tm;strptime(time, time_format, &tm);printf("tmis %04d-%02d-%02d %02d:%02d:%02d\n", 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);/* to time_t */time_t tt;tt = mktime(&tm);printf("time_t is %lu\n", tt);/* to timeval */struct timeval tv;tv.tv_sec = tt;tv.tv_usec = atoi(time + 21);// 21 = strlen("/03/25 11:09:42 .")printf("timeval is %ld.%06ld\n", tv.tv_sec, tv.tv_usec);return 0;}

output

origin is /03/25 11:09:42 .761197tmis -03-25 11:09:42time_t is 1585105782timeval is 1585105782.761197

如果觉得《[c/c++][Linux] 时间戳转换为tm time_t timeval》对你有帮助,请点赞、收藏,并留下你的观点哦!

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