使用 VC141 将 high_resolution_clock::time_point 转换为 time_t [英] Convert high_resolution_clock::time_point to time_t with VC141

查看:29
本文介绍了使用 VC141 将 high_resolution_clock::time_point 转换为 time_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 2013 中我刚刚使用

In Visual Studio 2013 I just used

#include <chrono>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <sstream>

std::string Time_Point_String(const std::chrono::high_resolution_clock::time_point & timePoint)
{
    time_t timeNow = std::chrono::system_clock::to_time_t(timePoint);

    tm time = *localtime(&timeNow);

    std::stringstream timeString;
timeString << std::setfill('0') << 1900 + time.tm_year << "-" << std::setw(2) << time.tm_mon + 1 << "-" << std::setw(2) << time.tm_mday << " " << std::setw(2) << time.tm_hour << ":" << std::setw(2) << time.tm_min << ":" << std::setw(2) << time.tm_sec;

    return timeString.str();
}

int main()
{
    const std::chrono::high_resolution_clock::time_point & timePoint = std::chrono::high_resolution_clock::now();

    std::cout << Time_Point_String(timePoint);
    return 0;
}

但使用 Visual Studio 2017 时出现编译器错误:

But with Visual Studio 2017 I get a compiler error:

错误 C2664 '__time64_t std::chrono::system_clock::to_time_t(const std::chrono::system_clock::time_point &) noexcept':无法从 'const std::chrono::steady_clock 转换参数 1::time_point' 到 'const std::chrono::system_clock::time_point &'

Error C2664 '__time64_t std::chrono::system_clock::to_time_t(const std::chrono::system_clock::time_point &) noexcept': cannot convert argument 1 from 'const std::chrono::steady_clock::time_point' to 'const std::chrono::system_clock::time_point &'

因此不再可能将 high_resolution_clock::time_point 转换为不同的 time_point,例如 system_clock::time_point,并且有无法将high_resolution_clock::time_point 直接转换为time_t 吗?

So it isn't possible anymore to convert a high_resolution_clock::time_point to a different time_point like system_clock::time_point and there is no possibility to convert high_resolution_clock::time_point to time_t directly?

我该如何处理这种情况?是否有可能(一些SO帖子说它们只是完全不同的时钟并且转换没有意义)?就我所见,该函数完成了我在 Visual Studio 2013 应用程序中的预期工作,它为高分辨率 time_point 提供了正确的本地时间.

How can I handle this situation? Is it possible at all (some SO postings say they are just completely different clocks and conversion doesn't make sense)? As far as I've seen, the function did what I expected it to do in Visual Studio 2013 application, it has provided the right local time for a high_resolution time_point.

推荐答案

这来自于 std::chrono::high_resolution_clockstd::chrono::system_clockstd::chrono:: 的类型别名稳定时钟:

Class std::chrono::high_resolution_clock 表示实现提供的具有最小滴答周期的时钟.它可能是 std::chrono::system_clockstd::chrono::steady_clock 的别名,或第三个独立时钟.

Class std::chrono::high_resolution_clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.

这意味着 std::chrono::high_resolution_clock::time_point 可以是 std::chrono::system_clock::time_point 的类型别名,也可以是另一种类型.从您的问题中,可以猜测它对 MSVC2013 有效,使您的代码有效,但不适用于 MSVC2017,使您的代码无效.

This means std::chrono::high_resolution_clock::time_point can be a type alias for std::chrono::system_clock::time_point, or it can be an other type. From your question, one can guess it does for MSVC2013, making your code valid, but is not for MSVC2017, making your code invalid.

总而言之,以下代码可能有效,也可能无效,以未指定的方式(这取决于编译器及其目标架构):

As a conclusion, the following code might or might not be valid, in an unspecified manner (it depends on the compiler and its target architecture):

std::chrono::high_resolution_clock::time_point timePoint = /* something */;
std::chrono::system_clock::to_time_t(timePoint);

这篇关于使用 VC141 将 high_resolution_clock::time_point 转换为 time_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆