diff --git a/tests/expected/time/asctime.stdout b/tests/expected/time/asctime.stdout index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..f21cdff5f4046c9e94d90c6a7089c2e23c39bd13 100644 --- a/tests/expected/time/asctime.stdout +++ b/tests/expected/time/asctime.stdout @@ -0,0 +1,5 @@ +Thu Jan 1 00:00:00 1970 +Sun Jan 1 00:00:00 1000 +Sat Dec 31 23:59:60 9999 +Sun Jan-99 00:00:00 -999 +Sat Dec999 99:99:99 9999 diff --git a/tests/time/asctime.c b/tests/time/asctime.c index 480de464a7863ec44dd1ff06c2cccd1c7daf5539..2525eddf0abcddb2904539169bf746a3da606770 100644 --- a/tests/time/asctime.c +++ b/tests/time/asctime.c @@ -6,12 +6,34 @@ #include "test_helpers.h" int main(void) { - time_t a = 0; - struct tm *time_info = gmtime(&a); + time_t unix_epoch = 0; + struct tm *unix_epoch_tm_ptr = gmtime(&unix_epoch); - char *time_string = asctime(time_info); + char *time_string = NULL; - if (time_string == NULL || strcmp(time_string, "Thu Jan 1 00:00:00 1970\n") != 0) { - exit(EXIT_FAILURE); - } + /* Min/max non-UB-causing values according to ISO C11 and newer. */ + struct tm iso_c_min_tm = {.tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_mon = 0, .tm_year = 1000-1900, .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0, .tm_gmtoff = 0, .tm_zone = NULL}; + struct tm iso_c_max_tm = {.tm_sec = 60, .tm_min = 59, .tm_hour = 23, .tm_mday = 31, .tm_mon = 11, .tm_year = 9999-1900, .tm_wday = 6, .tm_yday = 0, .tm_isdst = 0, .tm_gmtoff = 0, .tm_zone = NULL}; + + /* Min/max non-UB-causing values according to POSIX (issue 7). These + * will cause UB according to the ISO standard! */ + struct tm posix_min_tm = {.tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = -99, .tm_mon = 0, .tm_year = -999-1900, .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0, .tm_gmtoff = 0, .tm_zone = NULL}; + struct tm posix_max_tm = {.tm_sec = 99, .tm_min = 99, .tm_hour = 99, .tm_mday = 999, .tm_mon = 11, .tm_year = 9999-1900, .tm_wday = 6, .tm_yday = 0, .tm_isdst = 0, .tm_gmtoff = 0, .tm_zone = NULL}; + + time_string = asctime(unix_epoch_tm_ptr); + printf("%s", time_string); + + time_string = asctime(&iso_c_min_tm); + printf("%s", time_string); + + time_string = asctime(&iso_c_max_tm); + printf("%s", time_string); + + time_string = asctime(&posix_min_tm); + printf("%s", time_string); + + time_string = asctime(&posix_max_tm); + printf("%s", time_string); + + return 0; }