3
我需要以這種格式生成時間戳yyyymmdd。基本上我想創建一個當前日期擴展名的文件名。 (例如:log.20100817)便攜式在c/C++中創建時間戳的方式
我需要以這種格式生成時間戳yyyymmdd。基本上我想創建一個當前日期擴展名的文件名。 (例如:log.20100817)便攜式在c/C++中創建時間戳的方式
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char date[9];
time_t t = time(0);
struct tm *tm;
tm = gmtime(&t);
strftime(date, sizeof(date), "%Y%m%d", tm);
printf("log.%s\n", date);
return EXIT_SUCCESS;
}
另一種替代方法:Boost.Date_Time。
你實際上不需要'tm'臨時文件 - 你可以直接將'gmtime(&t)'的返回值傳遞給'strftime()'。 – caf 2010-08-18 02:18:24
@caf:我認爲重點是要顯示'gmtime'返回的內容。通常你不會只打印它。 – bitmask 2013-07-30 16:20:50