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

Posted Paprika

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux获取当前年月日后缀精确到微秒,例如2017-05-06T23:57:07.713171相关的知识,希望对你有一定的参考价值。

代码如下:详细见注释

#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的主要内容,如果未能解决你的问题,请参考以下文章