mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mark Brown <broonie@kernel.org>
To: John Stultz <jstultz@google.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	 Stephen Boyd <sboyd@kernel.org>,
	Jinjie Ruan <ruanjinjie@huawei.com>
Cc: Thomas Gleixner <tglx@kernel.org>,
	linux-kernel@vger.kernel.org,  Mark Brown <broonie@kernel.org>
Subject: [PATCH] time/kunit: Handle testing negative years
Date: Fri, 30 Jan 2026 19:48:35 +0000	[thread overview]
Message-ID: <20260130-kunit-fix-leap-year-v1-1-92ddf55dffd7@kernel.org> (raw)

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>


             reply	other threads:[~2026-01-30 19:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-30 19:48 Mark Brown [this message]
2026-02-02 11:40 ` [tip: timers/core] time/kunit: Document handling of negative years of is_leap() tip-bot2 for Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260130-kunit-fix-leap-year-v1-1-92ddf55dffd7@kernel.org \
    --to=broonie@kernel.org \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ruanjinjie@huawei.com \
    --cc=sboyd@kernel.org \
    --cc=tglx@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®