/time/tst-mktime2.c

https://gitlab.com/Namal/glibc · C · 158 lines · 122 code · 18 blank · 18 comment · 34 complexity · 947e9f92cb56fda8d0fa522f22dbfbe1 MD5 · raw file

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