[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20260130-kunit-fix-leap-year-v1-1-92ddf55dffd7@kernel.org>
Date: Fri, 30 Jan 2026 19:48:35 +0000
From: Mark Brown <broonie@...nel.org>
To: John Stultz <jstultz@...gle.com>, Thomas Gleixner <tglx@...utronix.de>,
Stephen Boyd <sboyd@...nel.org>, Jinjie Ruan <ruanjinjie@...wei.com>
Cc: Thomas Gleixner <tglx@...nel.org>, linux-kernel@...r.kernel.org,
Mark Brown <broonie@...nel.org>
Subject: [PATCH] time/kunit: Handle testing negative years
In 3d431a106947a ("time/kunit: Use is_leap_year() helper") we replaced
the local is_leap() function with is_leap_year() from the RTC code.
Unfortunately the two aren't exactly equivalent, we move from using a
signed value for the year to an unsigned one. Since the KUnit tests
cover a 16000 year range around the epoch they use year values that are
very comfortably negative and hence get mishandled when passed into
is_leap_year():
[14:26:27] # time64_to_tm_test_date_range: ASSERTION FAILED at kernel/time/time_test.c:73
[14:26:27] Expected month - 1 == result.tm_mon, but
[14:26:27] month - 1 == 2 (0x2)
[14:26:27] result.tm_mon == 1 (0x1)
[14:26:27] -77996/03/01 (59) : -29206923
[14:26:27] # time64_to_tm_test_date_range.speed: slow
[14:26:27] [FAILED] time64_to_tm_test_date_range
Revert to using our local implementation, adding a comment warning of
the issue while we're at it.
Fixes: 3d431a106947a ("time/kunit: Use is_leap_year() helper")
Signed-off-by: Mark Brown <broonie@...nel.org>
---
kernel/time/time_test.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/kernel/time/time_test.c b/kernel/time/time_test.c
index 7c2fb5f775eb..3c60fad07b6f 100644
--- a/kernel/time/time_test.c
+++ b/kernel/time/time_test.c
@@ -2,7 +2,15 @@
#include <kunit/test.h>
#include <linux/time.h>
-#include <linux/rtc.h>
+
+/*
+ * Traditional implementation of leap year evaluation, note that long
+ * is a signed type and the tests do cover negative year values.
+ */
+static bool is_leap(long year)
+{
+ return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
+}
/*
* Gets the last day of a month.
@@ -10,7 +18,7 @@
static int last_day_of_month(long year, int month)
{
if (month == 2)
- return 28 + is_leap_year(year);
+ return 28 + is_leap(year);
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
return 31;
---
base-commit: 3d431a106947a4a18ff5d3ba270a25df6a136e2f
change-id: 20260130-kunit-fix-leap-year-67b934d31a3a
Best regards,
--
Mark Brown <broonie@...nel.org>
Powered by blists - more mailing lists