C++日期时间函数

最近,几次去图书馆用Kinect捕获视频,程序用某同学的,OpenNI完全不了解。前几次都遇到点儿小麻烦,每次生成的文件名相同,下一次在运行前需要重命名下。我就需要生成唯一的文件名,就想到用时间表示先后顺序。

C++中的头文件提供了一个表示日期时间的结构:
struct tm
定义如下:
[cpp]
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
[/cpp]
用localtime可以生成tm结构
[cpp]
struct tm * localtime ( const time_t * timer );
[/cpp]
其中的time_t可以用time()生成
[cpp]
time_t time ( time_t * timer );
[/cpp]
我用在某程序段中的代码,生成文件名的前缀。
[cpp]
time_t timer;
struct tm* tblock;
timer=time(NULL);
tblock = localtime(&timer);
stringstream time_ss;
time_ss«"("«(tblock->tm_year+1900)«""«tblock->tm_mon«""«tblock->tm_mday«"-"«tblock->tm_hour«""«tblock->tm_min«""«tblock->tm_sec«")"; string filename_pre = time_ss.str(); [/cpp]