I try to find a way for a default date (if date is not valid). Common way works fine:
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
}
But if I set to the same function:
set(1900,1,1,0,0,0,0,0)
I will get a mktime = -1 Any idea where is my logic bomb?
CodePudding user response:
mktime
(and the rest of the POSIX date functions) only work for dates >= 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
and 1900 definitely can't be represented as a time_t, since it's 70 years early.