tst-mktime2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Test program from Paul Eggert and Tony Leneis. */
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. static time_t time_t_max;
  6. static time_t time_t_min;
  7. /* Values we'll use to set the TZ environment variable. */
  8. static const char *tz_strings[] =
  9. {
  10. (const char *) 0, "GMT0", "JST-9",
  11. "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
  12. };
  13. #define N_STRINGS ((int) (sizeof (tz_strings) / sizeof (tz_strings[0])))
  14. /* Fail if mktime fails to convert a date in the spring-forward gap.
  15. Based on a problem report from Andreas Jaeger. */
  16. static void
  17. spring_forward_gap (void)
  18. {
  19. /* glibc (up to about 1998-10-07) failed this test. */
  20. struct tm tm;
  21. /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
  22. instead of "TZ=America/Vancouver" in order to detect the bug even
  23. on systems that don't support the Olson extension, or don't have the
  24. full zoneinfo tables installed. */
  25. setenv ("TZ", "PST8PDT,M4.1.0,M10.5.0", 1);
  26. tm.tm_year = 98;
  27. tm.tm_mon = 3;
  28. tm.tm_mday = 5;
  29. tm.tm_hour = 2;
  30. tm.tm_min = 0;
  31. tm.tm_sec = 0;
  32. tm.tm_isdst = -1;
  33. if (mktime (&tm) == (time_t)-1)
  34. exit (1);
  35. }
  36. static void
  37. mktime_test1 (time_t now)
  38. {
  39. struct tm *lt = localtime (&now);
  40. if (lt && mktime (lt) != now)
  41. exit (2);
  42. }
  43. static void
  44. mktime_test (time_t now)
  45. {
  46. mktime_test1 (now);
  47. mktime_test1 ((time_t) (time_t_max - now));
  48. mktime_test1 ((time_t) (time_t_min + now));
  49. }
  50. static void
  51. irix_6_4_bug (void)
  52. {
  53. /* Based on code from Ariel Faigon. */
  54. struct tm tm;
  55. tm.tm_year = 96;
  56. tm.tm_mon = 3;
  57. tm.tm_mday = 0;
  58. tm.tm_hour = 0;
  59. tm.tm_min = 0;
  60. tm.tm_sec = 0;
  61. tm.tm_isdst = -1;
  62. mktime (&tm);
  63. if (tm.tm_mon != 2 || tm.tm_mday != 31)
  64. exit (3);
  65. }
  66. static void
  67. bigtime_test (int j)
  68. {
  69. struct tm tm;
  70. time_t now;
  71. tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
  72. tm.tm_isdst = -1;
  73. now = mktime (&tm);
  74. if (now != (time_t) -1)
  75. {
  76. struct tm *lt = localtime (&now);
  77. if (! (lt
  78. && lt->tm_year == tm.tm_year
  79. && lt->tm_mon == tm.tm_mon
  80. && lt->tm_mday == tm.tm_mday
  81. && lt->tm_hour == tm.tm_hour
  82. && lt->tm_min == tm.tm_min
  83. && lt->tm_sec == tm.tm_sec
  84. && lt->tm_yday == tm.tm_yday
  85. && lt->tm_wday == tm.tm_wday
  86. && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
  87. == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
  88. exit (4);
  89. }
  90. }
  91. static int
  92. do_test (void)
  93. {
  94. time_t t, delta;
  95. int i, j;
  96. setenv ("TZ", "America/Sao_Paulo", 1);
  97. /* This test makes some buggy mktime implementations loop.
  98. Give up after 60 seconds; a mktime slower than that
  99. isn't worth using anyway. */
  100. alarm (60);
  101. for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
  102. continue;
  103. time_t_max--;
  104. if ((time_t) -1 < 0)
  105. for (time_t_min = -1; (time_t) (time_t_min * 2) < 0; time_t_min *= 2)
  106. continue;
  107. delta = time_t_max / 997; /* a suitable prime number */
  108. for (i = 0; i < N_STRINGS; i++)
  109. {
  110. if (tz_strings[i])
  111. setenv ("TZ", tz_strings[i], 1);
  112. for (t = 0; t <= time_t_max - delta; t += delta)
  113. mktime_test (t);
  114. mktime_test ((time_t) 1);
  115. mktime_test ((time_t) (60 * 60));
  116. mktime_test ((time_t) (60 * 60 * 24));
  117. for (j = 1; 0 < j; j *= 2)
  118. bigtime_test (j);
  119. bigtime_test (j - 1);
  120. }
  121. irix_6_4_bug ();
  122. spring_forward_gap ();
  123. return 0;
  124. }
  125. #define TEST_FUNCTION do_test ()
  126. #include "../test-skeleton.c"