简体   繁体   English

修正了boost :: posix_time :: ptime的精度

[英]Fixed precision for boost::posix_time::ptime

I have problem with boost::posix_time: 我有boost :: posix_time的问题:

Here code: 这里代码:

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

record shows: eg. 记录显示:例如。 "08:15:30.093750". “08:15:30.093750”。 It is correct. 它是正确的。 Exactly what I expect. 正是我所期待的。 But sometimes I saw strange, eg. 但有时我看到很奇怪,例如。 "08:15:32" in stead of "08:15:32.000000". “08:15:32”代替“08:15:32.000000”。 How to get "08:15:32.000000"? 怎么得到“08:15:32.000000”?

Thanks in advance. 提前致谢。

Solution: 解:

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();

如果您需要非默认格式,则需要提出要求: http//www.boost.org/doc/html/date_time/date_time_io.html

使用小时,分钟和秒访问器并根据需要格式化输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM