获取当前时间并转为string

<pre name="code" class="cpp">
#include <time.h>
#include <sys/time.h>

#include <string>
#include <sstream>
using namespace std;

string getTimeInString()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    struct tm *ptm = gmtime(&tv.tv_sec);
    string strTime;
    strTime.reserve(32);
    size_t size = strftime(&strTime[0], 32, "%F %T.", ptm);
    strTime.resize(size);
    stringstream ss;
    ss << tv.tv_usec;
    strTime += ss.str();
    return strTime;
}

 
 


你可能感兴趣的:(获取当前时间并转为string)