c++ - mktime 返回 -1 但有效数据

标签 c++ mktime

我尝试找到默认日期的方法(如果日期无效)。 常见的方式工作正常:

set(2022,6,17,12,12,0,0,0)

void KTime::Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST, bool isUTC)
{
    assert(nYear >= 1900);
    assert(nMonth >= 1 && nMonth <= 12);
    assert(nDay >= 1 && nDay <= 31);
    assert(nHour >= 0 && nHour <= 23);
    assert(nMin >= 0 && nMin <= 59);
    assert(nSec >= 0 && nSec <= 59);

    struct tm atm;
    atm.tm_sec = nSec;
    atm.tm_min = nMin;
    atm.tm_hour = nHour;
    atm.tm_mday = nDay;
    atm.tm_mon = nMonth - 1;        // tm_mon is 0 based
    atm.tm_year = nYear - 1900;     // tm_year is 1900 based
    atm.tm_isdst = nDST;
    m_time = isUTC ? utc_mktime(&atm) : mktime(&atm);

    assert(m_time != -1);       // indicates an illegal input time
}

但如果我设置为相同的功能:

set(1900,1,1,0,0,0,0,0)

我会得到一个 mktime = -1 知道我的逻辑炸弹在哪里吗?

最佳答案

mktime(以及其余的 POSIX 日期函数)仅适用于 >= 1970-01-01 00:00:00,the UNIX epoch 的日期.

mktimequoth the manual ,

returns -1 if time cannot be represented as a time_t object

1900 绝对不能表示为 time_t,因为它早了 70 年。

关于c++ - mktime 返回 -1 但有效数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72668066/

相关文章:

c++ - Windows 8.1 蓝牙 LE 无法获取设备接口(interface)

c - 奇怪的 mktime() 行为

c++ - 如何找出对 C++ 符号的引用

c++ - 执行函数时导致崩溃的变量

c - C 中的 mktime() 返回 -1(总是)

c - C语言-Loadrunner Web中如何区分两个日期时间?

php - 前0个月的参数导致错误的输出

C - mktime() 在不应该返回 -1 的地方返回 -1

c++ - 如何发送原始 JSON Post 请求 C++

c++ - 对图像应用透视 - 单应性