linux 获取当前时间到微秒

#include<sys/time.h>

struct timeval start1;
       gettimeofday(&start1,0);
      sprintf(cMsg,"%d:%d\n",start1.tv_sec,start1.tv_usec);
     printf(cMsg);

————————————————————————————————————————————

#include <sys/time.h>
#include <unistd.h>
#include <iostream>

string TimeToString()
{
    char charFormat[30];

    struct timeval nowtimeval;
    gettimeofday(&nowtimeval,0);

    time_t now;
    struct tm *timenow;

    time(&now);
    timenow = localtime(&now);

    sprintf(charFormat,
            "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d%3.3d",
            timenow->tm_year + 1900,
            timenow->tm_mon + 1,
            timenow->tm_mday,
            timenow->tm_hour,
            timenow->tm_min,
            timenow->tm_sec,
            nowtimeval.tv_usec/1000);

    return charFormat;
}

你可能感兴趣的:(linux 获取当前时间到微秒)