我尝试找到默认日期的方法(如果日期无效)。 常见的方式工作正常:
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 的日期.
mktime
,quoth 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/