Ç - 转换的time_t为字符串格式YYYY-MM-DD HH:MM:SS

问题描述:

有没有什么办法来转换一个 time_t的的std ::字符串与格式 YYYY -MM-DD HH:MM:SS 自动同时保持code便携式

Is there any way to convert a time_t to a std::string with the format YYYY-MM-DD HH:MM:SS automatically while keeping the code portable?

使用本地时间来转换 time_t的来一个结构TM 。您可以使用的strftime 从打印所需的数据。

Use localtime to convert the time_t to a struct tm. You can use strftime to print the desired data from that.

char buff[20];
time_t now = time(NULL);
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));