2
0

asctime.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "test_helpers.h"
  6. int main(void) {
  7. time_t unix_epoch = 0;
  8. struct tm *unix_epoch_tm_ptr = gmtime(&unix_epoch);
  9. char *time_string = NULL;
  10. /* Min/max non-UB-causing values according to ISO C11 and newer. */
  11. 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};
  12. 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};
  13. /* Min/max non-UB-causing values according to POSIX (issue 7). These
  14. * will cause UB according to the ISO standard! */
  15. 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};
  16. 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};
  17. time_string = asctime(unix_epoch_tm_ptr);
  18. printf("%s", time_string);
  19. time_string = asctime(&iso_c_min_tm);
  20. printf("%s", time_string);
  21. time_string = asctime(&iso_c_max_tm);
  22. printf("%s", time_string);
  23. time_string = asctime(&posix_min_tm);
  24. printf("%s", time_string);
  25. time_string = asctime(&posix_max_tm);
  26. printf("%s", time_string);
  27. return 0;
  28. }