<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;
}
本文介绍了一种使用C++获取精确到微秒的时间戳的方法。通过结合`timeval`和`tm`结构体,该方法能够高效地获取当前时间,并将其格式化为字符串形式,便于在应用程序中记录和展示。
170

被折叠的 条评论
为什么被折叠?



