[PATCH] kunit: time: use is_leap_year() helper

Jinjie Ruan posted 1 patch 2 weeks ago
kernel/time/time_test.c | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)
[PATCH] kunit: time: use is_leap_year() helper
Posted by Jinjie Ruan 2 weeks ago
Use the is_leap_year() helper from rtc.h instead of
writing it by hand

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 kernel/time/time_test.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/kernel/time/time_test.c b/kernel/time/time_test.c
index 2889763165e5..7c2fb5f775eb 100644
--- a/kernel/time/time_test.c
+++ b/kernel/time/time_test.c
@@ -2,14 +2,7 @@
 
 #include <kunit/test.h>
 #include <linux/time.h>
-
-/*
- * Traditional implementation of leap year evaluation.
- */
-static bool is_leap(long year)
-{
-	return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
-}
+#include <linux/rtc.h>
 
 /*
  * Gets the last day of a month.
@@ -17,7 +10,7 @@ static bool is_leap(long year)
 static int last_day_of_month(long year, int month)
 {
 	if (month == 2)
-		return 28 + is_leap(year);
+		return 28 + is_leap_year(year);
 	if (month == 4 || month == 6 || month == 9 || month == 11)
 		return 30;
 	return 31;
-- 
2.34.1
Re: [PATCH] kunit: time: use is_leap_year() helper
Posted by Mark Brown 1 week ago
On Fri, Jan 23, 2026 at 04:09:40PM +0800, Jinjie Ruan wrote:

> Use the is_leap_year() helper from rtc.h instead of
> writing it by hand

This patch introduces a regression:

[14:26:27] =============== time_test_cases (1 subtest) ================
[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
[14:26:27] # module: time_test
[14:26:27] ================= [FAILED] time_test_cases =================
[14:26:27] ============================================================
[14:26:27] Testing complete. Ran 1 tests: failed: 1

> -/*
> - * Traditional implementation of leap year evaluation.
> - */
> -static bool is_leap(long year)
> -{
> -	return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
> -}

gets replaced by is_leap_year() which is:

static inline bool is_leap_year(unsigned int year)
{
        return (!(year % 4) && (year % 100)) || !(year % 400);
}

which looks like it ought to work out the same from a quick look
although it is written less clearly.  I'll look properly later when I've
got through the rest of the pile of regressions.
Re: [PATCH] kunit: time: use is_leap_year() helper
Posted by Mark Brown 1 week ago
On Fri, Jan 30, 2026 at 03:11:17PM +0000, Mark Brown wrote:
> On Fri, Jan 23, 2026 at 04:09:40PM +0800, Jinjie Ruan wrote:
> 
> > Use the is_leap_year() helper from rtc.h instead of
> > writing it by hand

> This patch introduces a regression:

> > -static bool is_leap(long year)

> gets replaced by is_leap_year() which is:

> static inline bool is_leap_year(unsigned int year)

> which looks like it ought to work out the same from a quick look
> although it is written less clearly.  I'll look properly later when I've
> got through the rest of the pile of regressions.

The issue here is, of course, the change from using long for the new to
using unsigned long.  The test is considering times up to 8000 years
before 1970 which results in us trying to convert a negative year to a
positive value before testing if it's a leap year which gives us the
wrong answer.  I'll send a revert.
Re: [PATCH] kunit: time: use is_leap_year() helper
Posted by Thomas Gleixner 1 week ago
On Fri, Jan 30 2026 at 19:39, Mark Brown wrote:
> On Fri, Jan 30, 2026 at 03:11:17PM +0000, Mark Brown wrote:
>> On Fri, Jan 23, 2026 at 04:09:40PM +0800, Jinjie Ruan wrote:
>> 
>> > Use the is_leap_year() helper from rtc.h instead of
>> > writing it by hand
>
>> This patch introduces a regression:
>
>> > -static bool is_leap(long year)
>
>> gets replaced by is_leap_year() which is:
>
>> static inline bool is_leap_year(unsigned int year)
>
>> which looks like it ought to work out the same from a quick look
>> although it is written less clearly.  I'll look properly later when I've
>> got through the rest of the pile of regressions.
>
> The issue here is, of course, the change from using long for the new to
> using unsigned long.  The test is considering times up to 8000 years
> before 1970 which results in us trying to convert a negative year to a
> positive value before testing if it's a leap year which gives us the
> wrong answer.  I'll send a revert.

Duh. Don't send anything I just zapped the commit from timers/core
Re: [PATCH] kunit: time: use is_leap_year() helper
Posted by Mark Brown 6 days, 17 hours ago
On Fri, Jan 30, 2026 at 10:13:53PM +0100, Thomas Gleixner wrote:
> On Fri, Jan 30 2026 at 19:39, Mark Brown wrote:

> > The issue here is, of course, the change from using long for the new to
> > using unsigned long.  The test is considering times up to 8000 years
> > before 1970 which results in us trying to convert a negative year to a
> > positive value before testing if it's a leap year which gives us the
> > wrong answer.  I'll send a revert.

> Duh. Don't send anything I just zapped the commit from timers/core

OK, that also works just as well - I'd sent the patch before I saw this
mail, sorry.  Thanks!
[tip: timers/core] time/kunit: Use is_leap_year() helper
Posted by tip-bot2 for Jinjie Ruan 1 week, 4 days ago
The following commit has been merged into the timers/core branch of tip:

Commit-ID:     3d431a106947a4a18ff5d3ba270a25df6a136e2f
Gitweb:        https://git.kernel.org/tip/3d431a106947a4a18ff5d3ba270a25df6a136e2f
Author:        Jinjie Ruan <ruanjinjie@huawei.com>
AuthorDate:    Fri, 23 Jan 2026 16:09:40 +08:00
Committer:     Thomas Gleixner <tglx@kernel.org>
CommitterDate: Mon, 26 Jan 2026 16:05:09 +01:00

time/kunit: Use is_leap_year() helper

Use the is_leap_year() helper from rtc.h instead of open coding it.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Link: https://patch.msgid.link/20260123080940.335474-1-ruanjinjie@huawei.com
---
 kernel/time/time_test.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/kernel/time/time_test.c b/kernel/time/time_test.c
index 2889763..7c2fb5f 100644
--- a/kernel/time/time_test.c
+++ b/kernel/time/time_test.c
@@ -2,14 +2,7 @@
 
 #include <kunit/test.h>
 #include <linux/time.h>
-
-/*
- * Traditional implementation of leap year evaluation.
- */
-static bool is_leap(long year)
-{
-	return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
-}
+#include <linux/rtc.h>
 
 /*
  * Gets the last day of a month.
@@ -17,7 +10,7 @@ static bool is_leap(long year)
 static int last_day_of_month(long year, int month)
 {
 	if (month == 2)
-		return 28 + is_leap(year);
+		return 28 + is_leap_year(year);
 	if (month == 4 || month == 6 || month == 9 || month == 11)
 		return 30;
 	return 31;