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