#include <chrono>
#include <string>
#include <iostream>
#include <iomanip>
std::string get_format_time()
{
auto now = std::chrono::system_clock::now();
auto timet = std::chrono::system_clock::to_time_t(now);
//std::cout << "timet: " << timet << std::endl; //timet: 1637235226
auto localTime = *std::gmtime(&timet);
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now).time_since_epoch().count() % 1000;
//std::cout << "ms: " << ms << std::endl; //ms: 632
std::stringstream ss;
std::string str;
ss << std::put_time(&localTime, "%Y-%m-%dT%H:%M:%S") << "." << std::setfill('0') << std::setw(3) << ms;
ss >> str;
return str;
}
int main()
{
std::cout << get_format_time() << std::endl;
// 输出:2021-11-18T13:45:49.047
return 0;
}
#include <chrono>
#include <string>
#include <iostream>
#include <iomanip>
std::string format_timestamp(int timestamp)
{
auto sec = std::chrono::seconds(timestamp);
std::chrono::time_point<std::chrono::system_clock> now(sec);
auto timet = std::chrono::system_clock::to_time_t(now);
auto localTime = *std::gmtime(&timet);
std::stringstream ss;
std::string str;
ss << std::put_time(&localTime, "%Y-%m-%dT%H:%M:%S");
ss >> str;
return str;
}
int main()
{
std::cout << format_timestamp(1630238012) << std::endl;
// 输出:2021-08-29T11:53:32
return 0;
}