bug-mktime4.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <time.h>
  2. #include <errno.h>
  3. #include <limits.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. static bool
  8. equal_tm (struct tm const *t, struct tm const *u)
  9. {
  10. return (t->tm_sec == u->tm_sec && t->tm_min == u->tm_min
  11. && t->tm_hour == u->tm_hour && t->tm_mday == u->tm_mday
  12. && t->tm_mon == u->tm_mon && t->tm_year == u->tm_year
  13. && t->tm_wday == u->tm_wday && t->tm_yday == u->tm_yday
  14. && t->tm_isdst == u->tm_isdst && t->tm_gmtoff == u->tm_gmtoff
  15. && t->tm_zone == u->tm_zone);
  16. }
  17. static int
  18. do_test (void)
  19. {
  20. /* Calculate minimum time_t value. This would be simpler with C11,
  21. which has _Generic, but we cannot assume C11. It would also
  22. be simpler with intprops.h, which has TYPE_MINIMUM, but it's
  23. better not to use glibc internals. */
  24. time_t time_t_min = -1;
  25. time_t_min = (0 < time_t_min ? 0
  26. : sizeof time_t_min == sizeof (long int) ? LONG_MIN
  27. : sizeof time_t_min == sizeof (long long int) ? LLONG_MIN
  28. : 1);
  29. if (time_t_min == 1)
  30. {
  31. printf ("unknown time type\n");
  32. return 1;
  33. }
  34. time_t ymin = time_t_min / 60 / 60 / 24 / 366;
  35. bool mktime_should_fail = ymin == 0 || INT_MIN + 1900 < ymin + 1970;
  36. struct tm tm0 = { .tm_year = INT_MIN, .tm_mday = 1, .tm_wday = -1 };
  37. struct tm tm = tm0;
  38. errno = 0;
  39. time_t t = mktime (&tm);
  40. long long int llt = t;
  41. bool mktime_failed = tm.tm_wday == tm0.tm_wday;
  42. if (mktime_failed)
  43. {
  44. if (! mktime_should_fail)
  45. {
  46. printf ("mktime failed but should have succeeded\n");
  47. return 1;
  48. }
  49. if (errno == 0)
  50. {
  51. printf ("mktime failed without setting errno");
  52. return 1;
  53. }
  54. if (t != (time_t) -1)
  55. {
  56. printf ("mktime returned %lld but did not set tm_wday\n", llt);
  57. return 1;
  58. }
  59. if (! equal_tm (&tm, &tm0))
  60. {
  61. printf ("mktime (P) failed but modified *P\n");
  62. return 1;
  63. }
  64. }
  65. else
  66. {
  67. if (mktime_should_fail)
  68. {
  69. printf ("mktime succeeded but should have failed\n");
  70. return 1;
  71. }
  72. struct tm *lt = localtime (&t);
  73. if (lt == NULL)
  74. {
  75. printf ("mktime returned a value rejected by localtime\n");
  76. return 1;
  77. }
  78. if (! equal_tm (lt, &tm))
  79. {
  80. printf ("mktime result does not match localtime result\n");
  81. return 1;
  82. }
  83. }
  84. return 0;
  85. }
  86. #define TIMEOUT 1000
  87. #include "support/test-driver.c"