【发布时间】:2017-06-10 07:27:51
【问题描述】:
我无法弄清楚如何让 Boost 将日期/时间对象格式化为字符串。我正在尝试设置格式说明符,但它被忽略并继续以预定义的样式输出日期。
通过演示可能更容易解释,所以这里有一些显示问题的示例代码:
#include <boost/date_time.hpp>
#include <iostream>
int main(void)
{
int i = 1485324852;
boost::posix_time::ptime pt = boost::posix_time::from_time_t(i);
boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet();
output_facet->format("%d/%m/%Y %H:%M");
std::stringstream ss;
ss.imbue(std::locale(std::locale::classic(), output_facet));
ss << pt;
std::string strTime = ss.str();
std::cout << "Time " << i << " converted to UI string: " << strTime << "\n";
return 0;
}
它给了我这个输出:
Time 1485324852 converted to UI string: 2017-Jan-25 06:14:12
我的预期是:
Time 1485324852 converted to UI string: 25/01/2017 16:14
我已经告诉output_facet 按照example given in the Boost docs 使用%d/%m/%Y %H:%M 格式说明符,但这被忽略了。
我看不出我在设置格式时哪里出错了。这可能是显而易见的,所以有人可以向我指出吗?
【问题讨论】:
标签: c++ boost boost-date-time