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