代码一
#include<iostream>
#include<chrono>
int main()
{
auto nowTime = std::chrono::system_clock::now();
auto nowTimeT = std::chrono::system_clock::to_time_t(nowTime);
auto pNowTm = std::localtime(&nowTimeT);
char buf[128];
std::strftime(buf, sizeof(buf), "%Y-%m-%d-%H-%M-%S", pNowTm);
std::string strNowTime(buf);
std::cout << "strNowTime:" << strNowTime << std::endl;
return 0;
}
代码二
#include<iostream>
#include"boost/date_time/posix_time/posix_time.hpp"
int main()
{
boost::posix_time::ptime nowTime = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_facet* pFacet = new boost::posix_time::time_facet();
pFacet->format("%Y%m%d%H%M%S%F");
std::stringstream strStream;
strStream.imbue(std::locale(std::locale::classic(), pFacet));
strStream << nowTime;
auto strNowTime = strStream.str();
std::cout << "strNowTime:" << strNowTime << std::endl;
//delete pFacet;不需要,写上反而会抛异常
return 0;
}
标签:std,-%,格式化,buf,C++,posix,time,字符串,boost
From: https://blog.csdn.net/2401_85919417/article/details/143219558