Updated to fedora-glibc-20050208T0948
[glibc.git] / timezone / zdump.c
blobbd7132698f8bc6959c721740bb0e00215855e4f4
1 static char elsieid[] = "@(#)zdump.c 7.61";
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 */
14 #include "float.h" /* for FLT_MAX and DBL_MAX */
16 #ifndef ZDUMP_LO_YEAR
17 #define ZDUMP_LO_YEAR (-500)
18 #endif /* !defined ZDUMP_LO_YEAR */
20 #ifndef ZDUMP_HI_YEAR
21 #define ZDUMP_HI_YEAR 2500
22 #endif /* !defined ZDUMP_HI_YEAR */
24 #ifndef MAX_STRING_LENGTH
25 #define MAX_STRING_LENGTH 1024
26 #endif /* !defined MAX_STRING_LENGTH */
28 #ifndef TRUE
29 #define TRUE 1
30 #endif /* !defined TRUE */
32 #ifndef FALSE
33 #define FALSE 0
34 #endif /* !defined FALSE */
36 #ifndef EXIT_SUCCESS
37 #define EXIT_SUCCESS 0
38 #endif /* !defined EXIT_SUCCESS */
40 #ifndef EXIT_FAILURE
41 #define EXIT_FAILURE 1
42 #endif /* !defined EXIT_FAILURE */
44 #ifndef SECSPERMIN
45 #define SECSPERMIN 60
46 #endif /* !defined SECSPERMIN */
48 #ifndef MINSPERHOUR
49 #define MINSPERHOUR 60
50 #endif /* !defined MINSPERHOUR */
52 #ifndef SECSPERHOUR
53 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
54 #endif /* !defined SECSPERHOUR */
56 #ifndef HOURSPERDAY
57 #define HOURSPERDAY 24
58 #endif /* !defined HOURSPERDAY */
60 #ifndef EPOCH_YEAR
61 #define EPOCH_YEAR 1970
62 #endif /* !defined EPOCH_YEAR */
64 #ifndef TM_YEAR_BASE
65 #define TM_YEAR_BASE 1900
66 #endif /* !defined TM_YEAR_BASE */
68 #ifndef DAYSPERNYEAR
69 #define DAYSPERNYEAR 365
70 #endif /* !defined DAYSPERNYEAR */
72 #ifndef isleap
73 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
74 #endif /* !defined isleap */
76 #ifndef isleap_sum
78 ** See tzfile.h for details on isleap_sum.
80 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
81 #endif /* !defined isleap_sum */
83 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
84 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
85 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
87 #if HAVE_GETTEXT
88 #include "locale.h" /* for setlocale */
89 #include "libintl.h"
90 #endif /* HAVE_GETTEXT */
92 #ifndef GNUC_or_lint
93 #ifdef lint
94 #define GNUC_or_lint
95 #else /* !defined lint */
96 #ifdef __GNUC__
97 #define GNUC_or_lint
98 #endif /* defined __GNUC__ */
99 #endif /* !defined lint */
100 #endif /* !defined GNUC_or_lint */
102 #ifndef INITIALIZE
103 #ifdef GNUC_or_lint
104 #define INITIALIZE(x) ((x) = 0)
105 #else /* !defined GNUC_or_lint */
106 #define INITIALIZE(x)
107 #endif /* !defined GNUC_or_lint */
108 #endif /* !defined INITIALIZE */
111 ** For the benefit of GNU folk...
112 ** `_(MSGID)' uses the current locale's message library string for MSGID.
113 ** The default is to use gettext if available, and use MSGID otherwise.
116 #ifndef _
117 #if HAVE_GETTEXT
118 #define _(msgid) gettext(msgid)
119 #else /* !HAVE_GETTEXT */
120 #define _(msgid) msgid
121 #endif /* !HAVE_GETTEXT */
122 #endif /* !defined _ */
124 #ifndef TZ_DOMAIN
125 #define TZ_DOMAIN "tz"
126 #endif /* !defined TZ_DOMAIN */
128 #ifndef P
129 #ifdef __STDC__
130 #define P(x) x
131 #else /* !defined __STDC__ */
132 #define P(x) ()
133 #endif /* !defined __STDC__ */
134 #endif /* !defined P */
136 extern char ** environ;
137 extern int getopt P((int argc, char * const argv[],
138 const char * options));
139 extern char * optarg;
140 extern int optind;
141 extern char * tzname[2];
143 static time_t absolute_min_time;
144 static time_t absolute_max_time;
145 static size_t longest;
146 static char * progname;
148 static char * abbr P((struct tm * tmp));
149 static long delta P((struct tm * newp, struct tm * oldp));
150 static void dumptime P((const struct tm * tmp));
151 static time_t hunt P((char * name, time_t lot, time_t hit));
152 static void setabsolutes P((void));
153 static void show P((char * zone, time_t t, int v));
154 static const char * tformat P((void));
155 static time_t yeartot P((long y));
157 #ifndef TYPECHECK
158 #define my_localtime localtime
159 #else /* !defined TYPECHECK */
160 static struct tm *
161 my_localtime(tp)
162 time_t * tp;
164 register struct tm * tmp;
166 tmp = localtime(tp);
167 if (tp != NULL && tmp != NULL) {
168 struct tm tm;
169 register time_t t;
171 tm = *tmp;
172 t = mktime(&tm);
173 if (t - *tp >= 1 || *tp - t >= 1) {
174 (void) fflush(stdout);
175 (void) fprintf(stderr, "\n%s: ", progname);
176 (void) fprintf(stderr, tformat(), *tp);
177 (void) fprintf(stderr, " ->");
178 (void) fprintf(stderr, " sec %d", tmp->tm_sec);
179 (void) fprintf(stderr, " min %d", tmp->tm_min);
180 (void) fprintf(stderr, " hour %d", tmp->tm_hour);
181 (void) fprintf(stderr, " mday %d", tmp->tm_mday);
182 (void) fprintf(stderr, " mon %d", tmp->tm_mon);
183 (void) fprintf(stderr, " year %d", tmp->tm_year);
184 (void) fprintf(stderr, " -> ");
185 (void) fprintf(stderr, tformat(), t);
186 (void) fprintf(stderr, "\n");
189 return tmp;
191 #endif /* !defined TYPECHECK */
194 main(argc, argv)
195 int argc;
196 char * argv[];
198 register int i;
199 register int c;
200 register int vflag;
201 register char * cutarg;
202 register long cutloyear = ZDUMP_LO_YEAR;
203 register long cuthiyear = ZDUMP_HI_YEAR;
204 register time_t cutlotime;
205 register time_t cuthitime;
206 register char ** fakeenv;
207 time_t now;
208 time_t t;
209 time_t newt;
210 struct tm tm;
211 struct tm newtm;
212 register struct tm * tmp;
213 register struct tm * newtmp;
215 INITIALIZE(cutlotime);
216 INITIALIZE(cuthitime);
217 #if HAVE_GETTEXT
218 (void) setlocale(LC_MESSAGES, "");
219 #ifdef TZ_DOMAINDIR
220 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
221 #endif /* defined TEXTDOMAINDIR */
222 (void) textdomain(TZ_DOMAIN);
223 #endif /* HAVE_GETTEXT */
224 progname = argv[0];
225 for (i = 1; i < argc; ++i)
226 if (strcmp(argv[i], "--version") == 0) {
227 (void) printf("%s\n", elsieid);
228 (void) exit(EXIT_SUCCESS);
230 vflag = 0;
231 cutarg = NULL;
232 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
233 if (c == 'v')
234 vflag = 1;
235 else cutarg = optarg;
236 if ((c != EOF && c != -1) ||
237 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
238 (void) fprintf(stderr,
239 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
240 progname, progname);
241 (void) exit(EXIT_FAILURE);
243 if (vflag) {
244 if (cutarg != NULL) {
245 long lo;
246 long hi;
247 char dummy;
249 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
250 cuthiyear = hi;
251 } else if (sscanf(cutarg, "%ld,%ld%c",
252 &lo, &hi, &dummy) == 2) {
253 cutloyear = lo;
254 cuthiyear = hi;
255 } else {
256 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
257 progname, cutarg);
258 (void) exit(EXIT_FAILURE);
261 setabsolutes();
262 cutlotime = yeartot(cutloyear);
263 cuthitime = yeartot(cuthiyear);
265 (void) time(&now);
266 longest = 0;
267 for (i = optind; i < argc; ++i)
268 if (strlen(argv[i]) > longest)
269 longest = strlen(argv[i]);
271 register int from;
272 register int to;
274 for (i = 0; environ[i] != NULL; ++i)
275 continue;
276 fakeenv = (char **) malloc((size_t) ((i + 2) *
277 sizeof *fakeenv));
278 if (fakeenv == NULL ||
279 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
280 (void) perror(progname);
281 (void) exit(EXIT_FAILURE);
283 to = 0;
284 (void) strcpy(fakeenv[to++], "TZ=");
285 for (from = 0; environ[from] != NULL; ++from)
286 if (strncmp(environ[from], "TZ=", 3) != 0)
287 fakeenv[to++] = environ[from];
288 fakeenv[to] = NULL;
289 environ = fakeenv;
291 for (i = optind; i < argc; ++i) {
292 static char buf[MAX_STRING_LENGTH];
294 (void) strcpy(&fakeenv[0][3], argv[i]);
295 if (!vflag) {
296 show(argv[i], now, FALSE);
297 continue;
299 t = absolute_min_time;
300 show(argv[i], t, TRUE);
301 t += SECSPERHOUR * HOURSPERDAY;
302 show(argv[i], t, TRUE);
303 if (t < cutlotime)
304 t = cutlotime;
305 tmp = my_localtime(&t);
306 if (tmp != NULL) {
307 tm = *tmp;
308 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
310 for ( ; ; ) {
311 if (t >= cuthitime)
312 break;
313 newt = t + SECSPERHOUR * 12;
314 if (newt >= cuthitime)
315 break;
316 if (newt <= t)
317 break;
318 newtmp = localtime(&newt);
319 if (newtmp != NULL)
320 newtm = *newtmp;
321 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
322 (delta(&newtm, &tm) != (newt - t) ||
323 newtm.tm_isdst != tm.tm_isdst ||
324 strcmp(abbr(&newtm), buf) != 0)) {
325 newt = hunt(argv[i], t, newt);
326 newtmp = localtime(&newt);
327 if (newtmp != NULL) {
328 newtm = *newtmp;
329 (void) strncpy(buf,
330 abbr(&newtm),
331 (sizeof buf) - 1);
334 t = newt;
335 tm = newtm;
336 tmp = newtmp;
338 t = absolute_max_time;
339 t -= SECSPERHOUR * HOURSPERDAY;
340 show(argv[i], t, TRUE);
341 t += SECSPERHOUR * HOURSPERDAY;
342 show(argv[i], t, TRUE);
344 if (fflush(stdout) || ferror(stdout)) {
345 (void) fprintf(stderr, "%s: ", progname);
346 (void) perror(_("Error writing standard output"));
347 (void) exit(EXIT_FAILURE);
349 exit(EXIT_SUCCESS);
350 /* If exit fails to exit... */
351 return EXIT_FAILURE;
354 static void
355 setabsolutes()
357 if (0.5 == (time_t) 0.5) {
359 ** time_t is floating.
361 if (sizeof (time_t) == sizeof (float)) {
362 absolute_min_time = (time_t) -FLT_MAX;
363 absolute_max_time = (time_t) FLT_MAX;
364 } else if (sizeof (time_t) == sizeof (double)) {
365 absolute_min_time = (time_t) -DBL_MAX;
366 absolute_max_time = (time_t) DBL_MAX;
367 } else {
368 (void) fprintf(stderr,
369 _("%s: use of -v on system with floating time_t other than float or double\n"),
370 progname);
371 (void) exit(EXIT_FAILURE);
373 } else if (0 > (time_t) -1) {
375 ** time_t is signed.
377 register time_t hibit;
379 for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
380 continue;
381 absolute_min_time = hibit;
382 absolute_max_time = -(hibit + 1);
383 } else {
385 ** time_t is unsigned.
387 absolute_min_time = 0;
388 absolute_max_time = absolute_min_time - 1;
392 static time_t
393 yeartot(y)
394 const long y;
396 register long myy;
397 register long seconds;
398 register time_t t;
400 myy = EPOCH_YEAR;
401 t = 0;
402 while (myy != y) {
403 if (myy < y) {
404 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
405 ++myy;
406 if (t > absolute_max_time - seconds) {
407 t = absolute_max_time;
408 break;
410 t += seconds;
411 } else {
412 --myy;
413 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
414 if (t < absolute_min_time + seconds) {
415 t = absolute_min_time;
416 break;
418 t -= seconds;
421 return t;
424 static time_t
425 hunt(name, lot, hit)
426 char * name;
427 time_t lot;
428 time_t hit;
430 time_t t;
431 long diff;
432 struct tm lotm;
433 register struct tm * lotmp;
434 struct tm tm;
435 register struct tm * tmp;
436 char loab[MAX_STRING_LENGTH];
438 lotmp = my_localtime(&lot);
439 if (lotmp != NULL) {
440 lotm = *lotmp;
441 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
443 for ( ; ; ) {
444 diff = (long) (hit - lot);
445 if (diff < 2)
446 break;
447 t = lot;
448 t += diff / 2;
449 if (t <= lot)
450 ++t;
451 else if (t >= hit)
452 --t;
453 tmp = my_localtime(&t);
454 if (tmp != NULL)
455 tm = *tmp;
456 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
457 (delta(&tm, &lotm) == (t - lot) &&
458 tm.tm_isdst == lotm.tm_isdst &&
459 strcmp(abbr(&tm), loab) == 0)) {
460 lot = t;
461 lotm = tm;
462 lotmp = tmp;
463 } else hit = t;
465 show(name, lot, TRUE);
466 show(name, hit, TRUE);
467 return hit;
471 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
474 static long
475 delta(newp, oldp)
476 struct tm * newp;
477 struct tm * oldp;
479 register long result;
480 register int tmy;
482 if (newp->tm_year < oldp->tm_year)
483 return -delta(oldp, newp);
484 result = 0;
485 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
486 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
487 result += newp->tm_yday - oldp->tm_yday;
488 result *= HOURSPERDAY;
489 result += newp->tm_hour - oldp->tm_hour;
490 result *= MINSPERHOUR;
491 result += newp->tm_min - oldp->tm_min;
492 result *= SECSPERMIN;
493 result += newp->tm_sec - oldp->tm_sec;
494 return result;
497 static void
498 show(zone, t, v)
499 char * zone;
500 time_t t;
501 int v;
503 register struct tm * tmp;
505 (void) printf("%-*s ", (int) longest, zone);
506 if (v) {
507 tmp = gmtime(&t);
508 if (tmp == NULL) {
509 (void) printf(tformat(), t);
510 } else {
511 dumptime(tmp);
512 (void) printf(" UTC");
514 (void) printf(" = ");
516 tmp = my_localtime(&t);
517 dumptime(tmp);
518 if (tmp != NULL) {
519 if (*abbr(tmp) != '\0')
520 (void) printf(" %s", abbr(tmp));
521 if (v) {
522 (void) printf(" isdst=%d", tmp->tm_isdst);
523 #ifdef TM_GMTOFF
524 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
525 #endif /* defined TM_GMTOFF */
528 (void) printf("\n");
531 static char *
532 abbr(tmp)
533 struct tm * tmp;
535 register char * result;
536 static char nada;
538 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
539 return &nada;
540 result = tzname[tmp->tm_isdst];
541 return (result == NULL) ? &nada : result;
545 ** The code below can fail on certain theoretical systems;
546 ** it works on all known real-world systems as of 2004-12-30.
549 static const char *
550 tformat()
552 if (0.5 == (time_t) 0.5) { /* floating */
553 if (sizeof (time_t) > sizeof (double))
554 return "%Lg";
555 return "%g";
557 if (0 > (time_t) -1) { /* signed */
558 if (sizeof (time_t) > sizeof (long))
559 return "%lld";
560 if (sizeof (time_t) > sizeof (int))
561 return "%ld";
562 return "%d";
564 if (sizeof (time_t) > sizeof (unsigned long))
565 return "%llu";
566 if (sizeof (time_t) > sizeof (unsigned int))
567 return "%lu";
568 return "%u";
571 static void
572 dumptime(timeptr)
573 register const struct tm * timeptr;
575 static const char wday_name[][3] = {
576 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
578 static const char mon_name[][3] = {
579 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
580 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
582 register const char * wn;
583 register const char * mn;
584 register int lead;
585 register int trail;
587 if (timeptr == NULL) {
588 (void) printf("NULL");
589 return;
592 ** The packaged versions of localtime and gmtime never put out-of-range
593 ** values in tm_wday or tm_mon, but since this code might be compiled
594 ** with other (perhaps experimental) versions, paranoia is in order.
596 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
597 (int) (sizeof wday_name / sizeof wday_name[0]))
598 wn = "???";
599 else wn = wday_name[timeptr->tm_wday];
600 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
601 (int) (sizeof mon_name / sizeof mon_name[0]))
602 mn = "???";
603 else mn = mon_name[timeptr->tm_mon];
604 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
605 wn, mn,
606 timeptr->tm_mday, timeptr->tm_hour,
607 timeptr->tm_min, timeptr->tm_sec);
608 #define DIVISOR 10
609 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
610 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
611 trail / DIVISOR;
612 trail %= DIVISOR;
613 if (trail < 0 && lead > 0) {
614 trail += DIVISOR;
615 --lead;
616 } else if (lead < 0 && trail > 0) {
617 trail -= DIVISOR;
618 ++lead;
620 if (lead == 0)
621 (void) printf("%d", trail);
622 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));