Repository navigation
[Bug] 内核mktime函数bug #11686
Copy link
Copy link
Closed
Labels
Arch: ARM/AArch64BSP related with armBSP related with armBSPBSP: NuvotonBSP related with NuvotonBSP related with NuvotonKernelPR has src relate codePR has src relate codebugThis PR/issue is a bug in the current code.This PR/issue is a bug in the current code.
Description
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(×tamp);
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);
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Arch: ARM/AArch64BSP related with armBSP related with armBSPBSP: NuvotonBSP related with NuvotonBSP related with NuvotonKernelPR has src relate codePR has src relate codebugThis PR/issue is a bug in the current code.This PR/issue is a bug in the current code.
Activity
github-actions commentedon Aug 5, 2026
如需认领并处理此 Issue,请评论
/claim。To claim and work on this issue, comment
/claim.github-actions commentedon Aug 5, 2026
以下 Issue 可能与当前问题相关;这是自动检索结果,尚未确认重复:
The following issues may be related. These automated suggestions are not confirmed duplicates:
milo-9 commentedon Aug 5, 2026
这个bug已经被修复了 #11140
Memory-afk commentedon Aug 5, 2026
@milo-9 这个bug并没有修复完全,只是修复了规范化年份不正确的问题,按照上面的例子加420天之后依然存在问题
Memory-afk commentedon Aug 5, 2026
在标准的unix还需要考虑小于0的情况
milo-9 commentedon Aug 11, 2026
[fix][kernel] fix timegm month overflow calculation
[fix][kernel] fix timegm month overflow calculation
[fix][kernel] fix timegm month overflow calculation
[fix][kernel] fix timegm month overflow calculation
[fix][kernel] fix timegm month overflow calculation
[fix][kernel] fix timegm month overflow calculation
[fix][libc] fix timegm month overflow calculation
[fix][libc] fix timegm month overflow calculation
[fix][libc] fix timegm month overflow calculation