1 /* Test program from Paul Eggert and Tony Leneis. */
3 #include <array_length.h>
8 #include <support/check.h>
12 /* True if the arithmetic type T is signed. */
13 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
15 /* The maximum and minimum values for the integer type T. These
16 macros have undefined behavior if T is signed and has padding bits.
17 If this is a problem for you, please let us know how to fix it for
19 #define TYPE_MINIMUM(t) \
20 ((t) (! TYPE_SIGNED (t) \
22 : ~ TYPE_MAXIMUM (t)))
23 #define TYPE_MAXIMUM(t) \
24 ((t) (! TYPE_SIGNED (t) \
26 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
29 # define TIME_T_MIN TYPE_MINIMUM (time_t)
32 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
35 /* Values we'll use to set the TZ environment variable. */
36 static const char *tz_strings[] =
38 (const char *) 0, "GMT0", "JST-9",
39 "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
43 set_timezone (const char *tz)
45 printf ("info: setting TZ=%s\n", tz);
46 if (setenv ("TZ", tz, 1) != 0)
47 FAIL_EXIT1 ("setenv: %m");
50 /* Fail if mktime fails to convert a date in the spring-forward gap.
51 Based on a problem report from Andreas Jaeger. */
53 spring_forward_gap (void)
55 /* glibc (up to about 1998-10-07) failed this test. */
58 /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
59 instead of "TZ=America/Vancouver" in order to detect the bug even
60 on systems that don't support the Olson extension, or don't have the
61 full zoneinfo tables installed. */
62 set_timezone ("PST8PDT,M4.1.0,M10.5.0");
71 if (mktime (&tm) == (time_t)-1)
72 FAIL_EXIT1 ("mktime: %m");
76 mktime_test1 (time_t now)
78 struct tm *lt = localtime (&now);
81 /* For extreme input values, it is expected that localtime fails
83 printf ("info: localtime (%lld) failed: %m\n", (long long int) now);
84 TEST_COMPARE (errno, EOVERFLOW);
87 TEST_COMPARE (mktime (lt), now);
91 mktime_test (time_t now)
94 mktime_test1 ((time_t) (TIME_T_MAX - now));
95 mktime_test1 ((time_t) (TIME_T_MIN + now));
101 /* Based on code from Ariel Faigon. */
111 TEST_COMPARE (tm.tm_mon, 2);
112 TEST_COMPARE (tm.tm_mday, 31);
120 tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
123 if (now != (time_t) -1)
125 struct tm *lt = localtime (&now);
126 TEST_COMPARE (lt->tm_year, tm.tm_year);
127 TEST_COMPARE (lt->tm_mon, tm.tm_mon);
128 TEST_COMPARE (lt->tm_mday, tm.tm_mday);
129 TEST_COMPARE (lt->tm_hour, tm.tm_hour);
130 TEST_COMPARE (lt->tm_min, tm.tm_min);
131 TEST_COMPARE (lt->tm_sec, tm.tm_sec);
132 TEST_COMPARE (lt->tm_yday, tm.tm_yday);
133 TEST_COMPARE (lt->tm_wday, tm.tm_wday);
134 TEST_COMPARE (lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst,
135 tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst);
146 set_timezone ("America/Sao_Paulo");
148 delta = TIME_T_MAX / 997; /* a suitable prime number */
149 for (i = 0; i < array_length (tz_strings); i++)
151 if (tz_strings[i] != NULL)
152 set_timezone (tz_strings[i]);
154 for (t = 0; t <= TIME_T_MAX - delta; t += delta)
156 mktime_test ((time_t) 1);
157 mktime_test ((time_t) (60 * 60));
158 mktime_test ((time_t) (60 * 60 * 24));
160 for (j = 1; j <= INT_MAX; j *= 2)
162 bigtime_test (j - 1);
165 spring_forward_gap ();
169 #include <support/test-driver.c>