gmtime.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "test_helpers.h"
  6. void print_tm(const struct tm *tm_ptr) {
  7. printf(" tm_sec = %d\n", tm_ptr->tm_sec);
  8. printf(" tm_min = %d\n", tm_ptr->tm_min);
  9. printf(" tm_hour = %d\n", tm_ptr->tm_hour);
  10. printf(" tm_mday = %d\n", tm_ptr->tm_mday);
  11. printf(" tm_mon = %d\n", tm_ptr->tm_mon);
  12. printf(" tm_year = %d\n", tm_ptr->tm_year);
  13. printf(" tm_wday = %d\n", tm_ptr->tm_wday);
  14. printf(" tm_yday = %d\n", tm_ptr->tm_yday);
  15. printf(" tm_isdst = %d\n", tm_ptr->tm_isdst);
  16. printf(" tm_gmtoff = %ld\n", tm_ptr->tm_gmtoff);
  17. printf(" tm_zone = %s\n", tm_ptr->tm_zone);
  18. }
  19. int main(void) {
  20. time_t unix_epoch_seconds = 0;
  21. // Exercise the different branches of the leap year logic
  22. // 1970: common year
  23. time_t y1970_mar01_seconds = 5097600; // 1970-03-01 00:00:00
  24. time_t y1970_dec31_seconds = 31535999; // 1970-12-31 23:59:59
  25. // 1972: leap year
  26. time_t y1972_feb29_seconds = 68255999; // 1972-02-29 23:59:59
  27. time_t y1972_dec31_seconds = 94694399; // 1972-12-31 23:59:59
  28. // 2000: leap year
  29. time_t y2000_feb29_seconds = 951868799; // 2000-02-29 23:59:59
  30. time_t y2000_dec31_seconds = 978307199; // 2000-12-31 23:59:59
  31. // 2100: common year
  32. time_t y2100_mar01_seconds = 4107542400; // 2100-03-01 00:00:00
  33. time_t y2100_dec31_seconds = 4133980799; // 2100-12-31 23:59:59
  34. // Year 2038-related corner cases
  35. time_t y2038_pre_seconds = 2147483647;
  36. time_t y2038_post_seconds = 2147483648;
  37. time_t y2106_pre_seconds = 4294967295;
  38. time_t y2106_post_seconds = 4294967296;
  39. struct tm *result_tm = NULL;
  40. puts("Unix epoch:");
  41. result_tm = gmtime(&unix_epoch_seconds);
  42. print_tm(result_tm);
  43. puts("");
  44. puts("1970-03-01 00:00:00:");
  45. result_tm = gmtime(&y1970_mar01_seconds);
  46. print_tm(result_tm);
  47. puts("");
  48. puts("1970-12-31 23:59:59:");
  49. result_tm = gmtime(&y1970_dec31_seconds);
  50. print_tm(result_tm);
  51. puts("");
  52. puts("1972-02-29 23:59:59:");
  53. result_tm = gmtime(&y1972_feb29_seconds);
  54. print_tm(result_tm);
  55. puts("");
  56. puts("1972-12-31 23:59:59:");
  57. result_tm = gmtime(&y1972_dec31_seconds);
  58. print_tm(result_tm);
  59. puts("");
  60. puts("2000-02-29 23:59:59:");
  61. result_tm = gmtime(&y2000_feb29_seconds);
  62. print_tm(result_tm);
  63. puts("");
  64. puts("2000-12-31 23:59:59:");
  65. result_tm = gmtime(&y2000_dec31_seconds);
  66. print_tm(result_tm);
  67. puts("");
  68. puts("2100-03-01 00:00:00:");
  69. result_tm = gmtime(&y2100_mar01_seconds);
  70. print_tm(result_tm);
  71. puts("");
  72. puts("2100-12-31 23:59:59:");
  73. result_tm = gmtime(&y2100_dec31_seconds);
  74. print_tm(result_tm);
  75. puts("");
  76. puts("Year 2038, pre-i32 overflow:");
  77. result_tm = gmtime(&y2038_pre_seconds);
  78. print_tm(result_tm);
  79. puts("");
  80. puts("Year 2038, post-i32 overflow:");
  81. result_tm = gmtime(&y2038_post_seconds);
  82. print_tm(result_tm);
  83. puts("");
  84. puts("Year 2106, pre-u32 overflow:");
  85. result_tm = gmtime(&y2106_pre_seconds);
  86. print_tm(result_tm);
  87. puts("");
  88. puts("Year 2106, post-u32 overflow:");
  89. result_tm = gmtime(&y2106_post_seconds);
  90. print_tm(result_tm);
  91. }