MKTime返回-1,但有效数据

发布于 2025-02-08 16:28:47 字数 989 浏览 1 评论 0原文

我尝试为默认日期找到一种方法(如果日期不有效)。 通用方法正常工作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2025-02-15 16:28:47

mktime(以及POSIX日期功能的其余部分)仅适用于日期&gt; = 1970-01-01 00:00:00, unix epoch

mktime quoth the Manual

返回-1如果时间不能表示为time_t对象

,而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,

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文