【问题标题】:Fixed precision for boost::posix_time::ptimeboost::posix_time::ptime 的固定精度
【发布时间】:2012-02-06 09:13:08
【问题描述】:

我对 boost::posix_time: 有疑问:

这里代码:

boost::posix_time::ptime now(boost::posix_time::microsec_clock::local_time());
std::wstringstream record;
record  << now.time_of_day();

记录显示:例如。 “08:15:30.093750”。它是正确的。正是我所期望的。 但有时我看到奇怪,例如。 “08:15:32”而不是“08:15:32.000000”。 如何获得“08:15:32.000000”?

提前致谢。

解决方案:

std::wstringstream record; 
boost::date_time::time_facet<boost::posix_time::ptime, wchar_t>* timeFacet(new boost::date_time::time_facet<boost::posix_time::ptime, wchar_t>(L"%f"));
record.imbue(std::locale(record.getloc(), timeFacet));
record << now.time_of_day();

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    如果您想要非默认格式,您需要询问:http://www.boost.org/doc/html/date_time/date_time_io.html

    【讨论】:

    • 它有效:std::wstringstream record;boost::date_time::time_facet&lt;boost::posix_time::ptime, wchar_t&gt;* timeFacet(new boost::date_time::time_facet&lt;boost::posix_time::ptime, wchar_t&gt;(L"%f"));record.imbue(std::locale(record.getloc(), timeFacet));record &lt;&lt; now.time_of_day();`
    • @PetervanderSanden:您还可以使用 typedef boost::posix_time::time_facet 使其更短(并且可能更健壮)。
    【解决方案2】:

    使用hours, minutes, and seconds 访问器并根据需要格式化输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2011-05-26
      相关资源
      最近更新 更多