C++功能模块7:获取字符串类型的当前时间

//获取时间戳,保存到字符串变量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";
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值