1 /*
2 * Copyright 2022 Bjarki Arge Andreasen
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/rtc.h>
10 #include <zephyr/sys/timeutil.h>
11
12 #include <time.h>
13
14 /* Wed Dec 31 2025 23:59:55 GMT+0000 */
15 #define RTC_TEST_TIME_COUNTING_SET_TIME (1767225595UL)
16 #define RTC_TEST_TIME_COUNTING_LIMIT (RTC_TEST_TIME_COUNTING_SET_TIME + 10UL)
17 #define RTC_TEST_TIME_COUNTING_POLL_LIMIT (30U)
18
19 static const struct device *rtc = DEVICE_DT_GET(DT_ALIAS(rtc));
20
ZTEST(rtc_api,test_time_counting)21 ZTEST(rtc_api, test_time_counting)
22 {
23 struct rtc_time datetime_set;
24 struct rtc_time datetime_get;
25 time_t timer_get;
26 time_t timer_set = RTC_TEST_TIME_COUNTING_SET_TIME;
27 time_t timer_get_last = timer_set;
28 uint8_t i;
29
30 gmtime_r(&timer_set, (struct tm *)(&datetime_set));
31
32 datetime_set.tm_isdst = -1;
33 datetime_set.tm_nsec = 0;
34
35 zassert_equal(rtc_set_time(rtc, &datetime_set), 0, "Failed to set time");
36
37 for (i = 0; i < RTC_TEST_TIME_COUNTING_POLL_LIMIT; i++) {
38 /* Get time */
39 zassert_equal(rtc_get_time(rtc, &datetime_get), 0, "Failed to get time");
40
41 timer_get = timeutil_timegm((struct tm *)(&datetime_get));
42
43 /* Validate if time incrementing */
44 zassert_true(timer_get_last <= timer_get, "Time is decrementing");
45
46 /* Check if limit reached */
47 if (timer_get == RTC_TEST_TIME_COUNTING_LIMIT) {
48 break;
49 }
50
51 /* Save last timer get */
52 timer_get_last = timer_get;
53
54 /* Limit polling rate */
55 k_msleep(500);
56 }
57
58 zassert_true(i < RTC_TEST_TIME_COUNTING_POLL_LIMIT,
59 "Timeout occurred waiting for time to increment");
60 }
61