From 46351921cbe11403a4fcab00e76fa03d99a7aef2 Mon Sep 17 00:00:00 2001 From: Uwe Kleine-König Date: Mon, 28 Apr 2025 12:06:49 +0200 Subject: rtc: test: Emit the seconds-since-1970 value instead of days-since-1970 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is easier to handle because you can just consult date(1) to convert between a seconds-since-1970 value and a date string: $ date --utc -d @3661 Thu Jan 1 01:01:01 AM UTC 1970 $ date -d "Jan 1 12:00:00 AM UTC 1900" +%s -2208988800 The intended side effect is that this prepares the test for dates before 1970. The division of a negative value by 86400 doesn't result in the desired days-since-1970 value as e.g. secs=-82739 should map to days=-1. Signed-off-by: Uwe Kleine-König Signed-off-by: Alexandre Mergnat Link: https://lore.kernel.org/r/20250428-enable-rtc-v4-3-2b2f7e3f9349@baylibre.com Signed-off-by: Alexandre Belloni --- drivers/rtc/lib_test.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'drivers/rtc/lib_test.c') diff --git a/drivers/rtc/lib_test.c b/drivers/rtc/lib_test.c index c30c759662e3..c0faddc04c9b 100644 --- a/drivers/rtc/lib_test.c +++ b/drivers/rtc/lib_test.c @@ -46,16 +46,13 @@ static void rtc_time64_to_tm_test_date_range(struct kunit *test, int years) struct rtc_time result; time64_t secs; - s64 days; for (secs = 0; secs <= total_secs; secs += 86400) { rtc_time64_to_tm(secs, &result); - days = div_s64(secs, 86400); - #define FAIL_MSG "%d/%02d/%02d (%2d) : %lld", \ - year, month, mday, yday, days + year, month, mday, yday, secs KUNIT_ASSERT_EQ_MSG(test, year - 1900, result.tm_year, FAIL_MSG); KUNIT_ASSERT_EQ_MSG(test, month - 1, result.tm_mon, FAIL_MSG); -- cgit v1.2.3 From da62b49830f83d1c80bfa1f2208db0cc6c19a1f9 Mon Sep 17 00:00:00 2001 From: Uwe Kleine-König Date: Mon, 28 Apr 2025 12:06:50 +0200 Subject: rtc: test: Also test time and wday outcome of rtc_time64_to_tm() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To cover calculation of the time and wday in the rtc kunit test also check tm_hour, tm_min, tm_sec and tm_wday of the rtc_time calculated by rtc_time64_to_tm(). There are no surprises, the two tests making use of rtc_time64_to_tm_test_date_range() continue to succeed. Signed-off-by: Uwe Kleine-König Signed-off-by: Alexandre Mergnat Link: https://lore.kernel.org/r/20250428-enable-rtc-v4-4-2b2f7e3f9349@baylibre.com Signed-off-by: Alexandre Belloni --- drivers/rtc/lib_test.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'drivers/rtc/lib_test.c') diff --git a/drivers/rtc/lib_test.c b/drivers/rtc/lib_test.c index c0faddc04c9b..b1ac0701d42e 100644 --- a/drivers/rtc/lib_test.c +++ b/drivers/rtc/lib_test.c @@ -6,8 +6,10 @@ /* * Advance a date by one day. */ -static void advance_date(int *year, int *month, int *mday, int *yday) +static void advance_date(int *year, int *month, int *mday, int *yday, int *wday) { + *wday = (*wday + 1) % 7; + if (*mday != rtc_month_days(*month - 1, *year)) { ++*mday; ++*yday; @@ -43,23 +45,29 @@ static void rtc_time64_to_tm_test_date_range(struct kunit *test, int years) int month = 1; int mday = 1; int yday = 1; + int wday = 4; /* Jan 1st 1970 was a Thursday */ struct rtc_time result; time64_t secs; + const time64_t sec_offset = ((1 * 60) + 2) * 60 + 3; for (secs = 0; secs <= total_secs; secs += 86400) { - rtc_time64_to_tm(secs, &result); + rtc_time64_to_tm(secs + sec_offset, &result); - #define FAIL_MSG "%d/%02d/%02d (%2d) : %lld", \ - year, month, mday, yday, secs + #define FAIL_MSG "%d/%02d/%02d (%2d, %d) : %lld", \ + year, month, mday, yday, wday, secs + sec_offset KUNIT_ASSERT_EQ_MSG(test, year - 1900, result.tm_year, FAIL_MSG); KUNIT_ASSERT_EQ_MSG(test, month - 1, result.tm_mon, FAIL_MSG); KUNIT_ASSERT_EQ_MSG(test, mday, result.tm_mday, FAIL_MSG); KUNIT_ASSERT_EQ_MSG(test, yday, result.tm_yday, FAIL_MSG); + KUNIT_ASSERT_EQ_MSG(test, 1, result.tm_hour, FAIL_MSG); + KUNIT_ASSERT_EQ_MSG(test, 2, result.tm_min, FAIL_MSG); + KUNIT_ASSERT_EQ_MSG(test, 3, result.tm_sec, FAIL_MSG); + KUNIT_ASSERT_EQ_MSG(test, wday, result.tm_wday, FAIL_MSG); - advance_date(&year, &month, &mday, &yday); + advance_date(&year, &month, &mday, &yday, &wday); } } -- cgit v1.2.3 From ccb2dba3c19f04d2203a4b630180bf50fe710d22 Mon Sep 17 00:00:00 2001 From: Uwe Kleine-König Date: Mon, 28 Apr 2025 12:06:51 +0200 Subject: rtc: test: Test date conversion for dates starting in 1900 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While the RTC framework intends to only handle dates after 1970 for consumers, time conversion must also work for earlier dates to cover e.g. storing dates beyond an RTC's range_max. This is most relevant for the rtc-mt6397 driver that has range_min = RTC_TIMESTAMP_BEGIN_1900; range_max = mktime64(2027, 12, 31, 23, 59, 59); and so needs working support for timestamps in 1900 starting in less than three years. So shift the tested interval of timestamps to also cover years 1900 to 1970. Signed-off-by: Uwe Kleine-König Signed-off-by: Alexandre Mergnat Link: https://lore.kernel.org/r/20250428-enable-rtc-v4-5-2b2f7e3f9349@baylibre.com Signed-off-by: Alexandre Belloni --- drivers/rtc/lib_test.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'drivers/rtc/lib_test.c') diff --git a/drivers/rtc/lib_test.c b/drivers/rtc/lib_test.c index b1ac0701d42e..0eebad1fe2a0 100644 --- a/drivers/rtc/lib_test.c +++ b/drivers/rtc/lib_test.c @@ -41,15 +41,15 @@ static void rtc_time64_to_tm_test_date_range(struct kunit *test, int years) */ time64_t total_secs = ((time64_t)years) / 400 * 146097 * 86400; - int year = 1970; + int year = 1900; int month = 1; int mday = 1; int yday = 1; - int wday = 4; /* Jan 1st 1970 was a Thursday */ + int wday = 1; /* Jan 1st 1900 was a Monday */ struct rtc_time result; time64_t secs; - const time64_t sec_offset = ((1 * 60) + 2) * 60 + 3; + const time64_t sec_offset = RTC_TIMESTAMP_BEGIN_1900 + ((1 * 60) + 2) * 60 + 3; for (secs = 0; secs <= total_secs; secs += 86400) { @@ -72,7 +72,7 @@ static void rtc_time64_to_tm_test_date_range(struct kunit *test, int years) } /* - * Checks every day in a 160000 years interval starting on 1970-01-01 + * Checks every day in a 160000 years interval starting on 1900-01-01 * against the expected result. */ static void rtc_time64_to_tm_test_date_range_160000(struct kunit *test) @@ -81,7 +81,7 @@ static void rtc_time64_to_tm_test_date_range_160000(struct kunit *test) } /* - * Checks every day in a 1000 years interval starting on 1970-01-01 + * Checks every day in a 1000 years interval starting on 1900-01-01 * against the expected result. */ static void rtc_time64_to_tm_test_date_range_1000(struct kunit *test) -- cgit v1.2.3