首页 > 编程语言 > 详细

C/C++ mktime 不符合预期

时间:2021-02-09 18:04:07      阅读:26      评论:0      收藏:0      [点我收藏+]
#include <time.h>

#include <chrono>
#include <iostream>
#include <string>

int main() {
    time_t now = time(nullptr); //获取UTC时间
    tm local_tm_now;
    if (localtime_r(&now, &local_tm_now) == nullptr) {
        return 1;
    }
    const time_t local_now = std::mktime(&local_tm_now); //得到当地日历时间
    tm local_tm_create;

    std::string str_time = "20210209101004";
    if (strptime(str_time.c_str(), "%Y%m%d%H%M%S", &local_tm_create) == nullptr) {
        return 2;
    }
    if (difftime(local_now, std::mktime(&local_tm_create)) >= 1800) {
        return 3; // GCC10 GLIBC2.29 will return here
    }

    return 0; // GCC4 GLIBC2.21 will return here
}

解决方法:
tm local_tm_create.tm_isdst = 0;

https://stackoverflow.com/questions/25374877/mktime-function-of-libc-returns-different-values-for-the-same-input

C/C++ mktime 不符合预期

原文:https://www.cnblogs.com/stdpain/p/14392606.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!