[PATCH] time/kunit: Handle testing negative years

Mark Brown posted 1 patch 1 week ago
kernel/time/time_test.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
[PATCH] time/kunit: Handle testing negative years
Posted by Mark Brown 1 week ago
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@kernel.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@kernel.org>
[tip: timers/core] time/kunit: Document handling of negative years of is_leap()
Posted by tip-bot2 for Mark Brown 5 days, 4 hours ago
The following commit has been merged into the timers/core branch of tip:

Commit-ID:     24989330fb99189cf9ec4accef6ac81c7b1c31f7
Gitweb:        https://git.kernel.org/tip/24989330fb99189cf9ec4accef6ac81c7b1c31f7
Author:        Mark Brown <broonie@kernel.org>
AuthorDate:    Fri, 30 Jan 2026 19:48:35 
Committer:     Thomas Gleixner <tglx@kernel.org>
CommitterDate: Mon, 02 Feb 2026 12:37:54 +01:00

time/kunit: Document handling of negative years of is_leap()

The code local is_leap() helper was tried to be replaced by the RTC
is_leap_year() function. Unfortunately the two aren't exactly equivalent,
as the kunit variant uses a signed value for the year and the RTC 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().

The change was reverted, so add a comment which prevents further attempts
to do so.

[ tglx: Adapted to the revert ]

Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Link: https://patch.msgid.link/20260130-kunit-fix-leap-year-v1-1-92ddf55dffd7@kernel.org
---
 kernel/time/time_test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/time/time_test.c b/kernel/time/time_test.c
index 2889763..1b99180 100644
--- a/kernel/time/time_test.c
+++ b/kernel/time/time_test.c
@@ -4,7 +4,9 @@
 #include <linux/time.h>
 
 /*
- * Traditional implementation of leap year evaluation.
+ * Traditional implementation of leap year evaluation, but note that long
+ * is a signed type and the tests do cover negative year values. So this
+ * can't use the is_leap_year() helper from rtc.h.
  */
 static bool is_leap(long year)
 {