MKTime返回-1,但有效数据
我尝试为默认日期找到一种方法(如果日期不有效)。 通用方法正常工作:
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 知道我的逻辑炸弹在哪里?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mktime
(以及POSIX日期功能的其余部分)仅适用于日期&gt; = 1970-01-01 00:00:00, unix epoch 。mktime
, quoth the Manual ,,而1900绝对不能表示为time_t,因为它早70年。
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,and 1900 definitely can't be represented as a time_t, since it's 70 years early.