Skip to content

[Bug] 内核mktime函数bug #11686

Closed

Description

@Memory-afk

RT-Thread Version

5.0.1

Affected area

Kernel

Hardware/BSP vendor

Nuvoton

Architecture

ARM / AArch64

Board and hardware details

N9H30

Develop Toolchain

GCC

Describe the bug

void time_example(void)
{
struct tm *raw_time;
struct tm _time;

_time.tm_year = 126;
_time.tm_mon = 7;
_time.tm_mday = 5;
_time.tm_hour = 8;
_time.tm_min = 0;
_time.tm_sec = 0;


_time.tm_mday += 420;

time_t timestamp = mktime(&_time);
raw_time = localtime(&timestamp);
raw_time->tm_year += 1900;

//这里应该输出2027/08/29,但实际输出了2027/03/01
rt_kprintf("%04d/%02d/%02d\n", raw_time->tm_year, raw_time->tm_mon, raw_time->tm_mday);

}

Other additional context

//临时解决方案
time_t timegm(struct tm * const t)
{
time_t day;
time_t i;
time_t years;

if(t == RT_NULL)
{
    rt_set_errno(EFAULT);
    return (time_t)-1;
}

years = (time_t)t->tm_year - 70;
if (t->tm_sec > 60)         /* seconds after the minute - [0, 60] including leap second */
{
    t->tm_min += t->tm_sec / 60;
    t->tm_sec %= 60;
}
if (t->tm_min >= 60)        /* minutes after the hour - [0, 59] */
{
    t->tm_hour += t->tm_min / 60;
    t->tm_min %= 60;
}
if (t->tm_hour >= 24)       /* hours since midnight - [0, 23] */
{
    t->tm_mday += t->tm_hour / 24;
    t->tm_hour %= 24;
}
if (t->tm_mon >= 12)        /* months since January - [0, 11] */
{
    t->tm_year += t->tm_mon / 12;
    t->tm_mon %= 12;
}

while (t->tm_mday > (__spm[t->tm_mon + 1] - __spm[t->tm_mon]))
{
    int days_in_month = __spm[t->tm_mon + 1] - __spm[t->tm_mon];
    if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
        days_in_month = 29;
    t->tm_mday -= days_in_month;
    if (++t->tm_mon >= 12) {
        t->tm_mon = 0;
        ++t->tm_year;
    }
}

while (t->tm_mday <= 0) {
    if (--t->tm_mon < 0) {
        t->tm_mon = 11;
        --t->tm_year;
    }
    int days_in_month = __spm[t->tm_mon + 1] - __spm[t->tm_mon];
    if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
        days_in_month = 29;
    t->tm_mday += days_in_month;
}

years = (time_t)t->tm_year - 70;

if (t->tm_year < 70)
{
    rt_set_errno(EINVAL);
    return (time_t) -1;
}

/* Days since 1970 is 365 * number of years + number of leap years since 1970 */
day = years * 365 + (years + 1) / 4;

/* After 2100 we have to substract 3 leap years for every 400 years
 This is not intuitive. Most mktime implementations do not support
 dates after 2059, anyway, so we might leave this out for it's
 bloat. */
if (years >= 131)
{
    years -= 131;
    years /= 100;
    day -= (years >> 2) * 3 + 1;
    if ((years &= 3) == 3)
        years--;
    day -= years;
}

day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
                    (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));

/* day is now the number of days since 'Jan 1 1970' */
i = 7;
t->tm_wday = (int)((day + 4) % i); /* Sunday=0, Monday=1, ..., Saturday=6 */

i = 24;
day *= i;
i = 60;
return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;

}
RTM_EXPORT(timegm);

Activity

  1. added
    bugThis PR/issue is a bug in the current code.
    on Aug 5, 2026
  2. github-actions commented on Aug 5, 2026

    @github-actions

    如需认领并处理此 Issue,请评论 /claim。

    To claim and work on this issue, comment /claim.

  3. github-actions commented on Aug 5, 2026

    @github-actions

    以下 Issue 可能与当前问题相关;这是自动检索结果,尚未确认重复:

    The following issues may be related. These automated suggestions are not confirmed duplicates:

  4. milo-9 commented on Aug 5, 2026

    @milo-9
    Contributor

    这个bug已经被修复了 #11140

  5. Memory-afk commented on Aug 5, 2026

    @Memory-afk
    Author

    @milo-9 这个bug并没有修复完全,只是修复了规范化年份不正确的问题,按照上面的例子加420天之后依然存在问题

  6. Memory-afk commented on Aug 5, 2026

    @Memory-afk
    Author

    在标准的unix还需要考虑小于0的情况

  7. milo-9 commented on Aug 11, 2026

    @milo-9
    Contributor

    @milo-9 这个bug并没有修复完全,只是修复了规范化年份不正确的问题,按照上面的例子加420天之后依然存在问题
    确实,我看得潦草了一点。

  8. added 8 commits that reference this issue on Aug 18, 2026
    4909000
    60d413e
    80b8771
    00764ae
    60cc901
    74d5bf4
    d2e3763
    470bda4
  9. added a commit that references this issue on Aug 21, 2026
    339bffb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arch: ARM/AArch64BSP related with armBSPBSP: NuvotonBSP related with NuvotonKernelPR has src relate codebugThis PR/issue is a bug in the current code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions