2
0

localtime.c 1021 B

12345678910111213141516171819202122232425262728293031
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. int day = 60 * 60 * 24;
  6. time_t inputs[] = { -(day * 33), -day, -1, -500, 0, 1, 1531454950 };
  7. for (int i = 0; i < (sizeof(inputs) / sizeof(time_t)); i += 1) {
  8. struct tm* t = localtime(&inputs[i]);
  9. printf(
  10. "Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n",
  11. t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec
  12. );
  13. }
  14. time_t input = 1531461823;
  15. fputs(ctime(&input), stdout); // Omit newline
  16. char ctime_r_buffer[26];
  17. /* ctime_r() generally returns the address of the provided buffer,
  18. * but may return NULL upon error according to the spec. */
  19. char *ctime_r_result = ctime_r(&input, ctime_r_buffer);
  20. if (ctime_r_result == ctime_r_buffer) {
  21. fputs(ctime_r_result, stdout);
  22. }
  23. else {
  24. printf("Unexpected pointer from ctime_r: %p\n", ctime_r_result);
  25. }
  26. }