Skip to content
Snippets Groups Projects
Commit ff6bc682 authored by Peter Limkilde Svendsen's avatar Peter Limkilde Svendsen Committed by Jeremy Soller
Browse files

Add more gmtime() tests

parent 3a0d2177
No related branches found
No related tags found
No related merge requests found
Unix epoch:
tm_sec = 0
tm_min = 0
tm_hour = 0
tm_mday = 1
tm_mon = 0
tm_year = 70
tm_wday = 4
tm_yday = 0
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
1970-03-01 00:00:00:
tm_sec = 0
tm_min = 0
tm_hour = 0
tm_mday = 1
tm_mon = 2
tm_year = 70
tm_wday = 0
tm_yday = 59
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
1970-12-31 23:59:59:
tm_sec = 59
tm_min = 59
tm_hour = 23
tm_mday = 31
tm_mon = 11
tm_year = 70
tm_wday = 4
tm_yday = 364
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
1972-02-29 23:59:59:
tm_sec = 59
tm_min = 59
tm_hour = 23
tm_mday = 29
tm_mon = 1
tm_year = 72
tm_wday = 2
tm_yday = 59
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
1972-12-31 23:59:59:
tm_sec = 59
tm_min = 59
tm_hour = 23
tm_mday = 31
tm_mon = 11
tm_year = 72
tm_wday = 0
tm_yday = 365
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
2000-02-29 23:59:59:
tm_sec = 59
tm_min = 59
tm_hour = 23
tm_mday = 29
tm_mon = 1
tm_year = 100
tm_wday = 2
tm_yday = 59
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
2000-12-31 23:59:59:
tm_sec = 59
tm_min = 59
tm_hour = 23
tm_mday = 31
tm_mon = 11
tm_year = 100
tm_wday = 0
tm_yday = 365
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
2100-03-01 00:00:00:
tm_sec = 0
tm_min = 0
tm_hour = 0
tm_mday = 1
tm_mon = 2
tm_year = 200
tm_wday = 1
tm_yday = 59
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
2100-12-31 23:59:59:
tm_sec = 59
tm_min = 59
tm_hour = 23
tm_mday = 31
tm_mon = 11
tm_year = 200
tm_wday = 5
tm_yday = 364
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
Year 2038, pre-i32 overflow:
tm_sec = 7
tm_min = 14
tm_hour = 3
tm_mday = 19
tm_mon = 0
tm_year = 138
tm_wday = 2
tm_yday = 18
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
Year 2038, post-i32 overflow:
tm_sec = 8
tm_min = 14
tm_hour = 3
tm_mday = 19
tm_mon = 0
tm_year = 138
tm_wday = 2
tm_yday = 18
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
Year 2106, pre-u32 overflow:
tm_sec = 15
tm_min = 28
tm_hour = 6
tm_mday = 7
tm_mon = 1
tm_year = 206
tm_wday = 0
tm_yday = 37
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
Year 2106, post-u32 overflow:
tm_sec = 16
tm_min = 28
tm_hour = 6
tm_mday = 7
tm_mon = 1
tm_year = 206
tm_wday = 0
tm_yday = 37
tm_isdst = 0
tm_gmtoff = 0
tm_zone = UTC
......@@ -5,17 +5,109 @@
#include "test_helpers.h"
void print_tm(const struct tm *tm_ptr) {
printf(" tm_sec = %d\n", tm_ptr->tm_sec);
printf(" tm_min = %d\n", tm_ptr->tm_min);
printf(" tm_hour = %d\n", tm_ptr->tm_hour);
printf(" tm_mday = %d\n", tm_ptr->tm_mday);
printf(" tm_mon = %d\n", tm_ptr->tm_mon);
printf(" tm_year = %d\n", tm_ptr->tm_year);
printf(" tm_wday = %d\n", tm_ptr->tm_wday);
printf(" tm_yday = %d\n", tm_ptr->tm_yday);
printf(" tm_isdst = %d\n", tm_ptr->tm_isdst);
printf(" tm_gmtoff = %ld\n", tm_ptr->tm_gmtoff);
printf(" tm_zone = %s\n", tm_ptr->tm_zone);
}
int main(void) {
time_t a = 0;
struct tm expected = { .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_year = 70,
.tm_wday = 4, .tm_yday = 0, .tm_isdst = 0, .tm_gmtoff = 0, .tm_zone = "UTC" };
struct tm *info = gmtime(&a);
if (info->tm_sec != expected.tm_sec || info->tm_min != expected.tm_min ||
info->tm_hour != expected.tm_hour || info->tm_mday != expected.tm_mday ||
info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday ||
info->tm_yday != expected.tm_yday || info->tm_isdst != expected.tm_isdst ||
info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) {
exit(EXIT_FAILURE);
}
time_t unix_epoch_seconds = 0;
// Exercise the different branches of the leap year logic
// 1970: common year
time_t y1970_mar01_seconds = 5097600; // 1970-03-01 00:00:00
time_t y1970_dec31_seconds = 31535999; // 1970-12-31 23:59:59
// 1972: leap year
time_t y1972_feb29_seconds = 68255999; // 1972-02-29 23:59:59
time_t y1972_dec31_seconds = 94694399; // 1972-12-31 23:59:59
// 2000: leap year
time_t y2000_feb29_seconds = 951868799; // 2000-02-29 23:59:59
time_t y2000_dec31_seconds = 978307199; // 2000-12-31 23:59:59
// 2100: common year
time_t y2100_mar01_seconds = 4107542400; // 2100-03-01 00:00:00
time_t y2100_dec31_seconds = 4133980799; // 2100-12-31 23:59:59
// Year 2038-related corner cases
time_t y2038_pre_seconds = 2147483647;
time_t y2038_post_seconds = 2147483648;
time_t y2106_pre_seconds = 4294967295;
time_t y2106_post_seconds = 4294967296;
struct tm *result_tm = NULL;
puts("Unix epoch:");
result_tm = gmtime(&unix_epoch_seconds);
print_tm(result_tm);
puts("");
puts("1970-03-01 00:00:00:");
result_tm = gmtime(&y1970_mar01_seconds);
print_tm(result_tm);
puts("");
puts("1970-12-31 23:59:59:");
result_tm = gmtime(&y1970_dec31_seconds);
print_tm(result_tm);
puts("");
puts("1972-02-29 23:59:59:");
result_tm = gmtime(&y1972_feb29_seconds);
print_tm(result_tm);
puts("");
puts("1972-12-31 23:59:59:");
result_tm = gmtime(&y1972_dec31_seconds);
print_tm(result_tm);
puts("");
puts("2000-02-29 23:59:59:");
result_tm = gmtime(&y2000_feb29_seconds);
print_tm(result_tm);
puts("");
puts("2000-12-31 23:59:59:");
result_tm = gmtime(&y2000_dec31_seconds);
print_tm(result_tm);
puts("");
puts("2100-03-01 00:00:00:");
result_tm = gmtime(&y2100_mar01_seconds);
print_tm(result_tm);
puts("");
puts("2100-12-31 23:59:59:");
result_tm = gmtime(&y2100_dec31_seconds);
print_tm(result_tm);
puts("");
puts("Year 2038, pre-i32 overflow:");
result_tm = gmtime(&y2038_pre_seconds);
print_tm(result_tm);
puts("");
puts("Year 2038, post-i32 overflow:");
result_tm = gmtime(&y2038_post_seconds);
print_tm(result_tm);
puts("");
puts("Year 2106, pre-u32 overflow:");
result_tm = gmtime(&y2106_pre_seconds);
print_tm(result_tm);
puts("");
puts("Year 2106, post-u32 overflow:");
result_tm = gmtime(&y2106_post_seconds);
print_tm(result_tm);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment