//获取时间戳,保存到字符串变量datetimestr中
time_t tDate = time(0);
struct tm * nowDate = localtime( & tDate );
timeval tp;
gettimeofday(&tp, 0);
time_t curtime = tp.tv_sec;
struct tm *nowTime = localtime(&curtime);
char datetimestr[128];
memset(datetimestr,0,sizeof(datetimestr));
sprintf(datetimestr,"%04d-%02d-%02d_%02d:%02d:%02d-%03ld", //datetimestr中保存了时间戳,可以根据具体需求调整格式和内容
nowDate->tm_year+1900,
nowDate->tm_mon+1,
nowDate->tm_mday,
nowTime->tm_hour,
nowTime->tm_min,
nowTime->tm_sec,
tp.tv_usec / 1000);
//char[]类型和string类型字符串拼接
int video_chn=1;
std::string videoName="c1.wmv";
//方法1 string先转char*,然后使用c语言sprintf拼接
char filename[512];
sprintf(filename,"../results/channelID%d_time%s_file%s.avi",video_chn,datetimestr,videoName.c_str());
//方法2 直接使用std::string直接拼接
//std::string filename="../results/channelID"+to_string(video_chn)+"_time"+datetimestr+"_file"+videoName+".avi";
C++功能模块7:获取字符串类型的当前时间
最新推荐文章于 2024-07-16 08:06:01 发布