c++ - 使用 boost::date_time 库以毫秒为单位格式化时间

我有一个以毫秒为单位的持续时间,理想情况下,我希望使用 boost::date_time 库中的格式化功能对其进行格式化。但是,在创建 boost::posix_time::time_duration 之后,我似乎无法找到一种方法来实际应用格式化字符串。

最佳答案

您需要先将持续时间添加到时间对象,然后像这样输出:

boost::posix_time::time_facet* facet = new boost::posix_time::time_facet("%Y%m%d %H:%M:%S.%f");
std::stringstream date_stream;
date_stream.imbue(std::locale(date_stream.getloc(), facet));
date_stream << boost::posix_time::microsec_clock::universal_time();

输出:

20100326 12:02:08.024820

使用 boost 1.41 测试

https://stackoverflow.com/questions/2662370/

相关文章:

c# - 在 C# 中格式化大数

wpf - 格式化 WPF Listview 列以仅显示 2 个小数点。

c# - JSON 序列化输出具有 C# 转义格式

wpf - 如何在 wpf 中计算 FormattedText 或 GlyphRun 的 'Size

sql - 在 SQL 中显示带有加号 (+) 的肯定结果

regex - 正则表达式中的 C# 字符串格式占位符

javascript - 基于 JavaScript/jQuery 中的格式字符串格式化数字

css - Google Webfonts 在 Firefox、Chrome 和 Opera 中的呈

java - 通过 HotKey 在 Eclipse 中防止代码格式化

python - 使用 Python 在 OpenOffice/Microsoft Word 中格式