首页 > 系统服务 > 详细

Linux获取当前年月日后缀精确到微秒,例如2017-05-06T23:57:07.713171

时间:2017-05-07 00:43:37      阅读:438      评论:0      收藏:0      [点我收藏+]

代码如下:详细见注释

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

int main() {
    struct timeval start;
    struct tm *local_time = NULL;
    static char str_time[100];
    char ms[16];
    gettimeofday( &start, NULL );//获取当前时间,该结构体返回Unix时间戳秒数与微秒数
    local_time = localtime(&start.tv_sec);//将秒转换标准时间
    strftime(str_time, sizeof(str_time), "%Y-%m-%dT%H:%M:%S", local_time);
    sprintf(ms,".%ld",start.tv_usec);
    strcat(str_time,ms);//将毫秒附着在时间后
    printf("time: %s \n", str_time);
    return 0;
}

 

Linux获取当前年月日后缀精确到微秒,例如2017-05-06T23:57:07.713171

原文:http://www.cnblogs.com/fogcell/p/6819192.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!