因为总是用到,写一个备份:
#include <iostream>
#include <ctime>
#include <string>
#include <iomanip>
int main() {
std::string timestamp = "1680925517"; // 示例时间戳:2023-04-08 11:45:17
time_t rawtime = std::stoi(timestamp);
struct tm * timeinfo;
char buffer[80];
timeinfo = localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
std::string formattedTime = buffer;
std::cout << "Formatted Time: " << formattedTime << std::endl;
return 0;
}
结果: