挺简单的,一看就懂
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h> // for gettimeofday()
#include <sys/time.h> // for timeval
int main()
{
struct timeval tv;
gettimeofday(&tv, NULL);
std::stringstream time_str;
//time_str << std::put_time(localtime(&tv.tv_sec), "%Y%m%dT%H%M%S"); //example: 20190511T134540
time_str << std::put_time(localtime(&tv.tv_sec), "%Y-%m-%d %H:%M:%S"); //example: 2019-05-11 13:47:15
int mill_time = tv.tv_usec / 1000;
std::string millitime_str = time_str.str() + "." + std::to_string(mill_time); //example: 2019-05-11 13:47:15.299
return millitime_str;
}