Skip to content

[Bug] 内核mktime函数bug #11686

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);

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