2.3.3-74
[glibc.git] / timezone / zdump.c
blob20bb916822b174394c207a59c0b7b1fc1e2eccaa
1 static char elsieid[] = "@(#)zdump.c 7.40";
3 /*
4 ** This code has been made independent of the rest of the time
5 ** conversion package to increase confidence in the verification it provides.
6 ** You can use this code to help in verifying other implementations.
7 */
9 #include "stdio.h" /* for stdout, stderr, perror */
10 #include "string.h" /* for strcpy */
11 #include "sys/types.h" /* for time_t */
12 #include "time.h" /* for struct tm */
13 #include "stdlib.h" /* for exit, malloc, atoi */
15 #ifndef MAX_STRING_LENGTH
16 #define MAX_STRING_LENGTH 1024
17 #endif /* !defined MAX_STRING_LENGTH */
19 #ifndef TRUE
20 #define TRUE 1
21 #endif /* !defined TRUE */
23 #ifndef FALSE
24 #define FALSE 0
25 #endif /* !defined FALSE */
27 #ifndef EXIT_SUCCESS
28 #define EXIT_SUCCESS 0
29 #endif /* !defined EXIT_SUCCESS */
31 #ifndef EXIT_FAILURE
32 #define EXIT_FAILURE 1
33 #endif /* !defined EXIT_FAILURE */
35 #ifndef SECSPERMIN
36 #define SECSPERMIN 60
37 #endif /* !defined SECSPERMIN */
39 #ifndef MINSPERHOUR
40 #define MINSPERHOUR 60
41 #endif /* !defined MINSPERHOUR */
43 #ifndef SECSPERHOUR
44 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
45 #endif /* !defined SECSPERHOUR */
47 #ifndef HOURSPERDAY
48 #define HOURSPERDAY 24
49 #endif /* !defined HOURSPERDAY */
51 #ifndef EPOCH_YEAR
52 #define EPOCH_YEAR 1970
53 #endif /* !defined EPOCH_YEAR */
55 #ifndef TM_YEAR_BASE
56 #define TM_YEAR_BASE 1900
57 #endif /* !defined TM_YEAR_BASE */
59 #ifndef DAYSPERNYEAR
60 #define DAYSPERNYEAR 365
61 #endif /* !defined DAYSPERNYEAR */
63 #ifndef isleap
64 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
65 #endif /* !defined isleap */
67 #if HAVE_GETTEXT
68 #include "locale.h" /* for setlocale */
69 #include "libintl.h"
70 #endif /* HAVE_GETTEXT */
72 #ifndef GNUC_or_lint
73 #ifdef lint
74 #define GNUC_or_lint
75 #else /* !defined lint */
76 #ifdef __GNUC__
77 #define GNUC_or_lint
78 #endif /* defined __GNUC__ */
79 #endif /* !defined lint */
80 #endif /* !defined GNUC_or_lint */
82 #ifndef INITIALIZE
83 #ifdef GNUC_or_lint
84 #define INITIALIZE(x) ((x) = 0)
85 #else /* !defined GNUC_or_lint */
86 #define INITIALIZE(x)
87 #endif /* !defined GNUC_or_lint */
88 #endif /* !defined INITIALIZE */
91 ** For the benefit of GNU folk...
92 ** `_(MSGID)' uses the current locale's message library string for MSGID.
93 ** The default is to use gettext if available, and use MSGID otherwise.
96 #ifndef _
97 #if HAVE_GETTEXT
98 #define _(msgid) gettext(msgid)
99 #else /* !HAVE_GETTEXT */
100 #define _(msgid) msgid
101 #endif /* !HAVE_GETTEXT */
102 #endif /* !defined _ */
104 #ifndef TZ_DOMAIN
105 #define TZ_DOMAIN "tz"
106 #endif /* !defined TZ_DOMAIN */
108 #ifndef P
109 #ifdef __STDC__
110 #define P(x) x
111 #else /* !defined __STDC__ */
112 #define P(x) ()
113 #endif /* !defined __STDC__ */
114 #endif /* !defined P */
116 extern char ** environ;
117 extern int getopt P((int argc, char * const argv[],
118 const char * options));
119 extern char * optarg;
120 extern int optind;
121 extern char * tzname[2];
123 static char * abbr P((struct tm * tmp));
124 static long delta P((struct tm * newp, struct tm * oldp));
125 static time_t hunt P((char * name, time_t lot, time_t hit));
126 static size_t longest;
127 static char * progname;
128 static void show P((char * zone, time_t t, int v));
129 static void dumptime P((const struct tm * tmp));
132 main(argc, argv)
133 int argc;
134 char * argv[];
136 register int i;
137 register int c;
138 register int vflag;
139 register char * cutoff;
140 register int cutyear;
141 register long cuttime;
142 char ** fakeenv;
143 time_t now;
144 time_t t;
145 time_t newt;
146 time_t hibit;
147 struct tm tm;
148 struct tm newtm;
150 INITIALIZE(cuttime);
151 #if HAVE_GETTEXT
152 (void) setlocale(LC_MESSAGES, "");
153 #ifdef TZ_DOMAINDIR
154 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
155 #endif /* defined TEXTDOMAINDIR */
156 (void) textdomain(TZ_DOMAIN);
157 #endif /* HAVE_GETTEXT */
158 progname = argv[0];
159 for (i = 1; i < argc; ++i)
160 if (strcmp(argv[i], "--version") == 0) {
161 (void) printf("%s\n", elsieid);
162 (void) exit(EXIT_SUCCESS);
164 vflag = 0;
165 cutoff = NULL;
166 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
167 if (c == 'v')
168 vflag = 1;
169 else cutoff = optarg;
170 if ((c != EOF && c != -1) ||
171 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
172 (void) fprintf(stderr,
173 _("%s: usage is %s [ --version ] [ -v ] [ -c cutoff ] zonename ...\n"),
174 argv[0], argv[0]);
175 (void) exit(EXIT_FAILURE);
177 if (cutoff != NULL) {
178 int y;
180 cutyear = atoi(cutoff);
181 cuttime = 0;
182 for (y = EPOCH_YEAR; y < cutyear; ++y)
183 cuttime += DAYSPERNYEAR + isleap(y);
184 cuttime *= SECSPERHOUR * HOURSPERDAY;
186 (void) time(&now);
187 longest = 0;
188 for (i = optind; i < argc; ++i)
189 if (strlen(argv[i]) > longest)
190 longest = strlen(argv[i]);
191 for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
192 continue;
194 register int from;
195 register int to;
197 for (i = 0; environ[i] != NULL; ++i)
198 continue;
199 fakeenv = (char **) malloc((size_t) ((i + 2) *
200 sizeof *fakeenv));
201 if (fakeenv == NULL ||
202 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
203 (void) perror(progname);
204 (void) exit(EXIT_FAILURE);
206 to = 0;
207 (void) strcpy(fakeenv[to++], "TZ=");
208 for (from = 0; environ[from] != NULL; ++from)
209 if (strncmp(environ[from], "TZ=", 3) != 0)
210 fakeenv[to++] = environ[from];
211 fakeenv[to] = NULL;
212 environ = fakeenv;
214 for (i = optind; i < argc; ++i) {
215 static char buf[MAX_STRING_LENGTH];
217 (void) strcpy(&fakeenv[0][3], argv[i]);
218 if (!vflag) {
219 show(argv[i], now, FALSE);
220 continue;
223 ** Get lowest value of t.
225 t = hibit;
226 if (t > 0) /* time_t is unsigned */
227 t = 0;
228 show(argv[i], t, TRUE);
229 t += SECSPERHOUR * HOURSPERDAY;
230 show(argv[i], t, TRUE);
231 tm = *localtime(&t);
232 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
233 for ( ; ; ) {
234 if (cutoff != NULL && t >= cuttime)
235 break;
236 newt = t + SECSPERHOUR * 12;
237 if (cutoff != NULL && newt >= cuttime)
238 break;
239 if (newt <= t)
240 break;
241 newtm = *localtime(&newt);
242 if (delta(&newtm, &tm) != (newt - t) ||
243 newtm.tm_isdst != tm.tm_isdst ||
244 strcmp(abbr(&newtm), buf) != 0) {
245 newt = hunt(argv[i], t, newt);
246 newtm = *localtime(&newt);
247 (void) strncpy(buf, abbr(&newtm),
248 (sizeof buf) - 1);
250 t = newt;
251 tm = newtm;
254 ** Get highest value of t.
256 t = ~((time_t) 0);
257 if (t < 0) /* time_t is signed */
258 t &= ~hibit;
259 t -= SECSPERHOUR * HOURSPERDAY;
260 show(argv[i], t, TRUE);
261 t += SECSPERHOUR * HOURSPERDAY;
262 show(argv[i], t, TRUE);
264 if (fflush(stdout) || ferror(stdout)) {
265 (void) fprintf(stderr, "%s: ", argv[0]);
266 (void) perror(_("Error writing standard output"));
267 (void) exit(EXIT_FAILURE);
269 exit(EXIT_SUCCESS);
271 /* gcc -Wall pacifier */
272 for ( ; ; )
273 continue;
276 static time_t
277 hunt(name, lot, hit)
278 char * name;
279 time_t lot;
280 time_t hit;
282 time_t t;
283 struct tm lotm;
284 struct tm tm;
285 static char loab[MAX_STRING_LENGTH];
287 lotm = *localtime(&lot);
288 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
289 while ((hit - lot) >= 2) {
290 t = lot / 2 + hit / 2;
291 if (t <= lot)
292 ++t;
293 else if (t >= hit)
294 --t;
295 tm = *localtime(&t);
296 if (delta(&tm, &lotm) == (t - lot) &&
297 tm.tm_isdst == lotm.tm_isdst &&
298 strcmp(abbr(&tm), loab) == 0) {
299 lot = t;
300 lotm = tm;
301 } else hit = t;
303 show(name, lot, TRUE);
304 show(name, hit, TRUE);
305 return hit;
309 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
312 static long
313 delta(newp, oldp)
314 struct tm * newp;
315 struct tm * oldp;
317 long result;
318 int tmy;
320 if (newp->tm_year < oldp->tm_year)
321 return -delta(oldp, newp);
322 result = 0;
323 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
324 result += DAYSPERNYEAR + isleap(tmy + (long) TM_YEAR_BASE);
325 result += newp->tm_yday - oldp->tm_yday;
326 result *= HOURSPERDAY;
327 result += newp->tm_hour - oldp->tm_hour;
328 result *= MINSPERHOUR;
329 result += newp->tm_min - oldp->tm_min;
330 result *= SECSPERMIN;
331 result += newp->tm_sec - oldp->tm_sec;
332 return result;
335 static void
336 show(zone, t, v)
337 char * zone;
338 time_t t;
339 int v;
341 struct tm * tmp;
343 (void) printf("%-*s ", (int) longest, zone);
344 if (v) {
345 dumptime(gmtime(&t));
346 (void) printf(" UTC = ");
348 tmp = localtime(&t);
349 dumptime(tmp);
350 if (*abbr(tmp) != '\0')
351 (void) printf(" %s", abbr(tmp));
352 if (v) {
353 (void) printf(" isdst=%d", tmp->tm_isdst);
354 #ifdef TM_GMTOFF
355 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
356 #endif /* defined TM_GMTOFF */
358 (void) printf("\n");
361 static char *
362 abbr(tmp)
363 struct tm * tmp;
365 register char * result;
366 static char nada;
368 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
369 return &nada;
370 result = tzname[tmp->tm_isdst];
371 return (result == NULL) ? &nada : result;
374 static void
375 dumptime(timeptr)
376 register const struct tm * timeptr;
378 static const char wday_name[][3] = {
379 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
381 static const char mon_name[][3] = {
382 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
383 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
385 register const char * wn;
386 register const char * mn;
389 ** The packaged versions of localtime and gmtime never put out-of-range
390 ** values in tm_wday or tm_mon, but since this code might be compiled
391 ** with other (perhaps experimental) versions, paranoia is in order.
393 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
394 (int) (sizeof wday_name / sizeof wday_name[0]))
395 wn = "???";
396 else wn = wday_name[timeptr->tm_wday];
397 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
398 (int) (sizeof mon_name / sizeof mon_name[0]))
399 mn = "???";
400 else mn = mon_name[timeptr->tm_mon];
401 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d %ld",
402 wn, mn,
403 timeptr->tm_mday, timeptr->tm_hour,
404 timeptr->tm_min, timeptr->tm_sec,
405 timeptr->tm_year + (long) TM_YEAR_BASE);