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

标签 c++ datetime boost formatting

我有一个以毫秒为单位的持续时间,理想情况下,我希望使用 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 测试

关于c++ - 使用 boost::date_time 库以毫秒为单位格式化时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2662370/

相关文章:

c++ - 使用 `__DATE__` 或 `__TIME__` 是否违反单一定义规则?

python - 使用 matplotlib 创建条形图,其中 x 轴为日期,宽度为时间增量

c++ - 在 Visual Studio 2010 中将仿函数传递给 boost::thread 失败

c++ - 使用 boost::function 的成员函数模板

python - [咖啡] : Check failed: ShapeEquals(proto) shape mismatch (reshape not set)

c++ - Clion c++ 中友元函数的错误

c++ - 与 Boost 的正则表达式区分大小写的部分匹配

c++ - 如何使 Qt Creator 在没有返回语句时显示错误

Javascript 将字符串转换为日期格式 yyyy-MM-dd

c++ - 使用 ptr_vector 时访问派生类的方法