source: trunk/src/libctests/glibc/time/tst-mktime2.c@ 2036

Last change on this file since 2036 was 2036, checked in by bird, 20 years ago

Initial revision

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