首页 > 解决方案 > 带有 to_iso_extended_string 的当前 UTC 日期字符串,格式为毫秒

问题描述

我想以毫秒精度生成当前的 UTC 日期时间,但我只能以秒或微秒精度获得它。是否有可能以毫秒精度获得它?

std::cout << to_iso_extended_string(microsec_clock::universal_time()) + "Z" << std::endl;

"2020-02-27T13:05:46.543801Z"

std::cout << to_iso_extended_string(second_clock::universal_time()) + "Z" << std::endl;

"2020-02-27T13:11:00Z"

预期格式:

"2020-02-27T13:05:46.543Z"

标签: c++boost

解决方案


我认为您可以从微秒版本中获取子字符串。

就像是:

std::string microsec_time = to_iso_extended_string(microsec_clock::universal_time());

std::string millisec_time = microsec_time.substr(0, microsec_time.size()-3);

std::cout << millisec_time << 'Z' << std::endl;

它应该给你你期望的输出。


推荐阅读