提问人:afp_2008 提问时间:11/9/2023 最后编辑:afp_2008 更新时间:11/9/2023 访问量:56
如何将纪元计数(毫秒)转换为计时time_point
How to convert epoch count in milliseconds to chrono time_point
问:
我正在尝试将以毫秒为单位的纪元计数(例如,)转换为 C++ 。我尝试了以下命令,但没有一个对我有用。1699491717000
chrono
time_point
我尝试了 Howard Hinnant 解决方案,但编译器不喜欢它
std::chrono::system_clock::time_point tp{std::chrono::milliseconds{epoch_count_ms}}; // Compiler says // No matching constructor for initialization of 'std::chrono::milliseconds' (aka 'duration<long, ratio<1, 1000>>')
-
std::chrono::milliseconds epoch_duration(epoch_count_ms); std::chrono::system_clock::time_point tp(epoch_duration); // Compiler says: // No matching constructor for initialization of 'std::chrono::milliseconds' (aka 'duration<long, ratio<1, 1000>>')
有人可以帮我吗?
答:
3赞
Howard Hinnant
11/9/2023
#1
epoch_count_ms
必须是整型。如果它是 a 或 a ,则会导致您看到的错误消息。double
string
您发布的第二条信息性错误消息应包含以下信息,例如:
note: candidate constructor (the implicit copy constructor) not viable:
no known conversion from 'double' to 'const std::chrono::duration<long long, std::ratio<1, 1000>>' for 1st argument class _LIBCPP_TEMPLATE_VIS duration
^^^^^^
这有效:
long long epoch_count_ms = 1699491717000;
std::chrono::system_clock::time_point tp{std::chrono::milliseconds{epoch_count_ms}};
评论
epoch_count_ms