Skip to contentSkip to conversation

time/kunit: Handle testing negative years

PATCH Applied Mark Brown 2 participants 2 messages Jan 30 – Feb 2 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 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")
---
 kernel/time/time_test.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/time/time_test.c +10 -2
index 7c2fb5f775eb..3c60fad07b6f 100644
@@ -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,

Signature · 1 line
Mark Brown <[email protected]>
merge bot Applied
#1 lore raw

[tip: timers/core] time/kunit: Document handling of negative years of is_leap()

The following commit has been merged into the timers/core branch of tip:

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 <[email protected]>
AuthorDate:    Fri, 30 Jan 2026 19:48:35 
Committer:     Thomas Gleixner <[email protected]>
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 ]

---
 kernel/time/time_test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/time/time_test.c +3 -1
index 2889763..1b99180 100644
@@ -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)
 {