c++ - mktime(&time_check) 只返回 "-1"

标签 c++ time

所以我的代码基本上是这样的:

    #include <time.h>

    struct tm time_check;
    time_check.tm_year = 2018;
    time_check.tm_mon  = 03;
    time_check.tm_mday = 28;

    if(mktime(&time_check) != -1)

条件每次都是错误的,但为什么?日期是正确的,不是吗?

最佳答案

需要初始化tm结构,年份要减去1900。 试试这个代码:

    struct tm time_check;
time_t now;
time( &now );
localtime_s( &time_check, &now );
time_check.tm_year = 2018-1900;
time_check.tm_mon = 03;
time_check.tm_mday = 28;
if( mktime( &time_check ) != -1 )

关于c++ - mktime(&time_check) 只返回 "-1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52065767/

相关文章:

c++ - NTP 对于分布式时间同步有多好?

c++ - 如何实现信号量?这个实现是正确的还是错误的?

c++ - 如何在 C 的头文件中正确编写 extern 数组的声明?

c++ - 内存泄漏和嵌入式 linux

c++ - 如何在 C++ 中有效地将 double 转换为 int?

获取当前时间的 Java 代码

php - 在 PHP 中添加时间

c++ - 有符号/无符号比较给出意想不到的结果

linux - 在服务器 : Etc/UTC or Etc/Universal? 中设置哪个时区

javascript - 检查日期是今天、昨天还是过去 7 天