mktime(&time_check)仅返回“ -1”

时间:2018-08-28 20:30:06

标签: 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)

条件每次都是假的,但是为什么呢?日期正确吗?

1 个答案:

答案 0 :(得分:2)

有必要初始化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 )