Skip to content

Commit 6d5f4db

Browse files
authored
Merge pull request #4519 from c1728p9/fix_mktime_test
Fix mktime test
2 parents 113ee13 + adcd292 commit 6d5f4db

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

TESTS/mbed_hal/rtc_time/main.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
#include "mbed.h"
2222
#include "mbed_mktime.h"
2323

24+
// Limit the test range to 1935 for IAR only. From the IAR C/C++ Development Guide:
25+
// "The 32-bit interface supports years from 1900 up to 2035 and uses a 32-bit integer
26+
// for time_t."
27+
#ifdef __ICCARM__
28+
#define LOCALTIME_MAX 2082758400 // 1st of january 2036 at 00:00:00
29+
#define MKTIME_YR_MAX 136
30+
#else
31+
#define LOCALTIME_MAX INT_MAX
32+
#define MKTIME_YR_MAX 137
33+
#endif
34+
2435
using namespace utest::v1;
2536

2637
/*
@@ -54,13 +65,17 @@ void test_is_leap_year() {
5465
}
5566

5667
struct tm make_time_info(int year, int month, int day, int hours, int minutes, int seconds) {
57-
struct tm timeinfo;
58-
timeinfo.tm_year = year;
59-
timeinfo.tm_mon = month;
60-
timeinfo.tm_mday = day;
61-
timeinfo.tm_hour = hours;
62-
timeinfo.tm_min = minutes;
63-
timeinfo.tm_sec = seconds;
68+
struct tm timeinfo = {
69+
seconds, // tm_sec
70+
minutes, // tm_min
71+
hours, // tm_hour
72+
day, // tm_mday
73+
month, // tm_mon
74+
year, // tm_year
75+
0, // tm_wday
76+
0, // tm_yday
77+
0, // tm_isdst
78+
};
6479
return timeinfo;
6580
}
6681

@@ -116,7 +131,7 @@ void test_mk_time_out_of_range() {
116131
* test mktime over a large set of values
117132
*/
118133
void test_mk_time() {
119-
for (size_t year = 70; year < 137; ++year) {
134+
for (size_t year = 70; year < MKTIME_YR_MAX; ++year) {
120135
for (size_t month = 0; month < 12; ++month) {
121136
for (size_t day = 1; day < 32; ++day) {
122137
if (month == 1 && is_leap_year(year) && day == 29) {
@@ -172,7 +187,7 @@ void test_local_time_limit() {
172187
* test _rtc_localtime over a large set of values.
173188
*/
174189
void test_local_time() {
175-
for (uint32_t i = 0; i < INT_MAX; i += 3451) {
190+
for (uint32_t i = 0; i < LOCALTIME_MAX; i += 3451) {
176191
time_t copy = (time_t) i;
177192
struct tm* expected = localtime(&copy);
178193
struct tm actual_value;

0 commit comments

Comments
 (0)