参考的网上的模板,直接给出程序:
#include "ctime"
#include "time.h"
using namespace std;
string int2string(int value)
{
stringstream ss;
ss<<value;
return ss.str();
}
time_t t=std::time(0);
struct tm * now = std::localtime( & t );
string file_name;
//the name of bag file is better to be determined by the system time
file_name=int2string(now->tm_year + 1900)+
'-'+int2string(now->tm_mon + 1)+
'-'+int2string(now->tm_mday)+
'-'+int2string(now->tm_hour)+
'-'+int2string(now->tm_min)+
'-'+int2string(now->tm_sec)+
".bag";
整个程序在我的上一篇博客中有,可以看下。如果你是在ubuntu,ros环境下搞的话,这里面需要注意的是在time(0)和localtime()前面加上std::,可能是因为ubuntu或者ros下对这两个函数有重定义,不加的话编译会报错。
当然这里面也涉及到了把int类型转换成string类型,我使用的是stringstream