c++ - 使用 system_clock::to_time_t 的持久性 time_t 警告

标签 c++ time warnings

我在下面编写的函数是尝试允许 32 位和 64 位时间并努力变得更加灵活。我一直收到关于数据转换丢失的警告,这不太合理。

time_t now()
    {
        using namespace std::chrono;

        system_clock::time_point time_point = system_clock::now();            
        time_t now_time = system_clock::to_time_t(time_point);     
        return now_time;   //warning appears here
    }

警告:warning C4244: '=': conversion from '__time64_t' to 'time_t', possible loss of data 在 to_time_t 上找到。我感到困惑的是 cppreference 指示 to_time_t 返回一个 time_t,但是当我导航到实际函数时,它指示它被强制为 64 位时间。

那么,知道是什么原因造成的吗?此外,我对获取系统时间和返回 time_t 的更简洁的实现持开放态度。

我还想补充一点,该示例与此处找到的内容相匹配: cppreference

最佳答案

这很可能是您遇到了 MSVC 2015 的记录错误。坏消息是它已记录在现已停用的 Microsoft Connect 中。

它位于 url https://connect.microsoft.com/VisualStudio/feedback/details/1355735/breaking-change-in-vs-2015-chrono-steady-clock-time-point-not-longer-convertible-to-system-clock-time-point (死的);报告于 05/22/15--06:42。

关于c++ - 使用 system_clock::to_time_t 的持久性 time_t 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48811380/

相关文章:

c++ - 如何调试为按空格拆分数组而编写的代码?

c++ - 如何从封闭的模板类访问嵌套类的方法和字段?

c++ - while(i--) s+= a[i];在 C 和 C++ 中包含未定义的行为?

c++ - 将 chrono::duration 类型转换为 std::tm 类型

javascript - 删除数组的 "this"索引不起作用

c++ - 我们可以重新定义正则表达式吗?

MySQL - 有条件的操作

php - 错误:警告:mysqli_query()至少需要2个参数,其中1个在

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c++ - 如何在 C++ 中使用 time.h 消除警告?