1、boost的date_time库中的月份默认都是用英文来表示的,如Mar是3月。。。,有时我们想输出如下格式的时间到文件中:2020-03-17 22:41:08 899412,下面给出格式化输出方法:
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
std::string ptime_to_string(boost::posix_time::ptime now)
{
std::stringstream ss;
static std::locale loc(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S %f"));
ss.imbue(loc);
ss << now;
return ss.str();
}
int main()
{
boost::posix_time::ptime pNow = boost::posix_time::microsec_clock::local_time();
std::string strNow = ptime_to_string(pNow);
std::cout << strNow << std::endl;
}