mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] time/kunit: Handle testing negative years
@ 2026-01-30 19:48 Mark Brown
  2026-02-02 11:40 ` [tip: timers/core] time/kunit: Document handling of negative years of is_leap() tip-bot2 for Mark Brown
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Brown @ 2026-01-30 19:48 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Jinjie Ruan
  Cc: Thomas Gleixner, linux-kernel, Mark Brown

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>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-02-02 11:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-30 19:48 [PATCH] time/kunit: Handle testing negative years Mark Brown
2026-02-02 11:40 ` [tip: timers/core] time/kunit: Document handling of negative years of is_leap() tip-bot2 for Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®