Revert:
[glibc.git] / timezone / zdump.c
blobd767d75fb749389d637b5452d8d03964d53a86c3
1 static char elsieid[] = "@(#)zdump.c 7.66";
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 */
15 #include <ctype.h> /* for isascii, isalpha, isdigit */
17 #ifndef ZDUMP_LO_YEAR
18 #define ZDUMP_LO_YEAR (-500)
19 #endif /* !defined ZDUMP_LO_YEAR */
21 #ifndef ZDUMP_HI_YEAR
22 #define ZDUMP_HI_YEAR 2500
23 #endif /* !defined ZDUMP_HI_YEAR */
25 #ifndef MAX_STRING_LENGTH
26 #define MAX_STRING_LENGTH 1024
27 #endif /* !defined MAX_STRING_LENGTH */
29 #ifndef TRUE
30 #define TRUE 1
31 #endif /* !defined TRUE */
33 #ifndef FALSE
34 #define FALSE 0
35 #endif /* !defined FALSE */
37 #ifndef EXIT_SUCCESS
38 #define EXIT_SUCCESS 0
39 #endif /* !defined EXIT_SUCCESS */
41 #ifndef EXIT_FAILURE
42 #define EXIT_FAILURE 1
43 #endif /* !defined EXIT_FAILURE */
45 #ifndef SECSPERMIN
46 #define SECSPERMIN 60
47 #endif /* !defined SECSPERMIN */
49 #ifndef MINSPERHOUR
50 #define MINSPERHOUR 60
51 #endif /* !defined MINSPERHOUR */
53 #ifndef SECSPERHOUR
54 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
55 #endif /* !defined SECSPERHOUR */
57 #ifndef HOURSPERDAY
58 #define HOURSPERDAY 24
59 #endif /* !defined HOURSPERDAY */
61 #ifndef EPOCH_YEAR
62 #define EPOCH_YEAR 1970
63 #endif /* !defined EPOCH_YEAR */
65 #ifndef TM_YEAR_BASE
66 #define TM_YEAR_BASE 1900
67 #endif /* !defined TM_YEAR_BASE */
69 #ifndef DAYSPERNYEAR
70 #define DAYSPERNYEAR 365
71 #endif /* !defined DAYSPERNYEAR */
73 #ifndef isleap
74 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
75 #endif /* !defined isleap */
77 #ifndef isleap_sum
79 ** See tzfile.h for details on isleap_sum.
81 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
82 #endif /* !defined isleap_sum */
84 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
85 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
86 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
88 #if HAVE_GETTEXT
89 #include "locale.h" /* for setlocale */
90 #include "libintl.h"
91 #endif /* HAVE_GETTEXT */
93 #ifndef GNUC_or_lint
94 #ifdef lint
95 #define GNUC_or_lint
96 #else /* !defined lint */
97 #ifdef __GNUC__
98 #define GNUC_or_lint
99 #endif /* defined __GNUC__ */
100 #endif /* !defined lint */
101 #endif /* !defined GNUC_or_lint */
103 #ifndef INITIALIZE
104 #ifdef GNUC_or_lint
105 #define INITIALIZE(x) ((x) = 0)
106 #else /* !defined GNUC_or_lint */
107 #define INITIALIZE(x)
108 #endif /* !defined GNUC_or_lint */
109 #endif /* !defined INITIALIZE */
112 ** For the benefit of GNU folk...
113 ** `_(MSGID)' uses the current locale's message library string for MSGID.
114 ** The default is to use gettext if available, and use MSGID otherwise.
117 #ifndef _
118 #if HAVE_GETTEXT
119 #define _(msgid) gettext(msgid)
120 #else /* !HAVE_GETTEXT */
121 #define _(msgid) msgid
122 #endif /* !HAVE_GETTEXT */
123 #endif /* !defined _ */
125 #ifndef TZ_DOMAIN
126 #define TZ_DOMAIN "tz"
127 #endif /* !defined TZ_DOMAIN */
129 #ifndef P
130 #ifdef __STDC__
131 #define P(x) x
132 #else /* !defined __STDC__ */
133 #define P(x) ()
134 #endif /* !defined __STDC__ */
135 #endif /* !defined P */
137 extern char ** environ;
138 extern int getopt P((int argc, char * const argv[],
139 const char * options));
140 extern char * optarg;
141 extern int optind;
142 extern char * tzname[2];
144 static time_t absolute_min_time;
145 static time_t absolute_max_time;
146 static size_t longest;
147 static char * progname;
148 static int warned;
150 static char * abbr P((struct tm * tmp));
151 static void abbrok P((const char * abbr, const char * zone));
152 static long delta P((struct tm * newp, struct tm * oldp));
153 static void dumptime P((const struct tm * tmp));
154 static time_t hunt P((char * name, time_t lot, time_t hit));
155 static void setabsolutes P((void));
156 static void show P((char * zone, time_t t, int v));
157 static const char * tformat P((void));
158 static time_t yeartot P((long y));
160 #ifndef TYPECHECK
161 #define my_localtime localtime
162 #else /* !defined TYPECHECK */
163 static struct tm *
164 my_localtime(tp)
165 time_t * tp;
167 register struct tm * tmp;
169 tmp = localtime(tp);
170 if (tp != NULL && tmp != NULL) {
171 struct tm tm;
172 register time_t t;
174 tm = *tmp;
175 t = mktime(&tm);
176 if (t - *tp >= 1 || *tp - t >= 1) {
177 (void) fflush(stdout);
178 (void) fprintf(stderr, "\n%s: ", progname);
179 (void) fprintf(stderr, tformat(), *tp);
180 (void) fprintf(stderr, " ->");
181 (void) fprintf(stderr, " year=%d", tmp->tm_year);
182 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
183 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
184 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
185 (void) fprintf(stderr, " min=%d", tmp->tm_min);
186 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
187 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
188 (void) fprintf(stderr, " -> ");
189 (void) fprintf(stderr, tformat(), t);
190 (void) fprintf(stderr, "\n");
193 return tmp;
195 #endif /* !defined TYPECHECK */
197 static void
198 abbrok(abbr, zone)
199 const char * const abbr;
200 const char * const zone;
202 register const char * cp;
203 register char * wp;
205 if (warned)
206 return;
207 cp = abbr;
208 wp = NULL;
209 while (isascii(*cp) && isalpha(*cp))
210 ++cp;
211 if (cp - abbr == 0)
212 wp = _("lacks alphabetic at start");
213 if (cp - abbr < 3)
214 wp = _("has fewer than 3 alphabetics");
215 if (cp - abbr > 6)
216 wp = _("has more than 6 alphabetics");
217 if (wp == NULL && (*cp == '+' || *cp == '-')) {
218 ++cp;
219 if (isascii(*cp) && isdigit(*cp))
220 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
221 ++cp;
223 if (*cp != '\0')
224 wp = _("differs from POSIX standard");
225 if (wp == NULL)
226 return;
227 (void) fflush(stdout);
228 (void) fprintf(stderr,
229 "%s: warning: zone \"%s\" abbreviation \"%s\" %s\n",
230 progname, zone, abbr, wp);
231 warned = TRUE;
235 main(argc, argv)
236 int argc;
237 char * argv[];
239 register int i;
240 register int c;
241 register int vflag;
242 register char * cutarg;
243 register long cutloyear = ZDUMP_LO_YEAR;
244 register long cuthiyear = ZDUMP_HI_YEAR;
245 register time_t cutlotime;
246 register time_t cuthitime;
247 register char ** fakeenv;
248 time_t now;
249 time_t t;
250 time_t newt;
251 struct tm tm;
252 struct tm newtm;
253 register struct tm * tmp;
254 register struct tm * newtmp;
256 INITIALIZE(cutlotime);
257 INITIALIZE(cuthitime);
258 #if HAVE_GETTEXT
259 (void) setlocale(LC_ALL, "");
260 #ifdef TZ_DOMAINDIR
261 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
262 #endif /* defined TEXTDOMAINDIR */
263 (void) textdomain(TZ_DOMAIN);
264 #endif /* HAVE_GETTEXT */
265 progname = argv[0];
266 for (i = 1; i < argc; ++i)
267 if (strcmp(argv[i], "--version") == 0) {
268 (void) printf("%s\n", elsieid);
269 (void) exit(EXIT_SUCCESS);
271 vflag = 0;
272 cutarg = NULL;
273 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
274 if (c == 'v')
275 vflag = 1;
276 else cutarg = optarg;
277 if ((c != EOF && c != -1) ||
278 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
279 (void) fprintf(stderr,
280 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
281 progname, progname);
282 (void) exit(EXIT_FAILURE);
284 if (vflag) {
285 if (cutarg != NULL) {
286 long lo;
287 long hi;
288 char dummy;
290 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
291 cuthiyear = hi;
292 } else if (sscanf(cutarg, "%ld,%ld%c",
293 &lo, &hi, &dummy) == 2) {
294 cutloyear = lo;
295 cuthiyear = hi;
296 } else {
297 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
298 progname, cutarg);
299 (void) exit(EXIT_FAILURE);
302 setabsolutes();
303 cutlotime = yeartot(cutloyear);
304 cuthitime = yeartot(cuthiyear);
306 (void) time(&now);
307 longest = 0;
308 for (i = optind; i < argc; ++i)
309 if (strlen(argv[i]) > longest)
310 longest = strlen(argv[i]);
312 register int from;
313 register int to;
315 for (i = 0; environ[i] != NULL; ++i)
316 continue;
317 fakeenv = (char **) malloc((size_t) ((i + 2) *
318 sizeof *fakeenv));
319 if (fakeenv == NULL ||
320 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
321 (void) perror(progname);
322 (void) exit(EXIT_FAILURE);
324 to = 0;
325 (void) strcpy(fakeenv[to++], "TZ=");
326 for (from = 0; environ[from] != NULL; ++from)
327 if (strncmp(environ[from], "TZ=", 3) != 0)
328 fakeenv[to++] = environ[from];
329 fakeenv[to] = NULL;
330 environ = fakeenv;
332 for (i = optind; i < argc; ++i) {
333 static char buf[MAX_STRING_LENGTH];
335 (void) strcpy(&fakeenv[0][3], argv[i]);
336 if (!vflag) {
337 show(argv[i], now, FALSE);
338 continue;
340 warned = FALSE;
341 t = absolute_min_time;
342 show(argv[i], t, TRUE);
343 t += SECSPERHOUR * HOURSPERDAY;
344 show(argv[i], t, TRUE);
345 if (t < cutlotime)
346 t = cutlotime;
347 tmp = my_localtime(&t);
348 if (tmp != NULL) {
349 tm = *tmp;
350 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
352 for ( ; ; ) {
353 if (t >= cuthitime)
354 break;
355 newt = t + SECSPERHOUR * 12;
356 if (newt >= cuthitime)
357 break;
358 if (newt <= t)
359 break;
360 newtmp = localtime(&newt);
361 if (newtmp != NULL)
362 newtm = *newtmp;
363 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
364 (delta(&newtm, &tm) != (newt - t) ||
365 newtm.tm_isdst != tm.tm_isdst ||
366 strcmp(abbr(&newtm), buf) != 0)) {
367 newt = hunt(argv[i], t, newt);
368 newtmp = localtime(&newt);
369 if (newtmp != NULL) {
370 newtm = *newtmp;
371 (void) strncpy(buf,
372 abbr(&newtm),
373 (sizeof buf) - 1);
376 t = newt;
377 tm = newtm;
378 tmp = newtmp;
380 t = absolute_max_time;
381 t -= SECSPERHOUR * HOURSPERDAY;
382 show(argv[i], t, TRUE);
383 t += SECSPERHOUR * HOURSPERDAY;
384 show(argv[i], t, TRUE);
386 if (fflush(stdout) || ferror(stdout)) {
387 (void) fprintf(stderr, "%s: ", progname);
388 (void) perror(_("Error writing standard output"));
389 (void) exit(EXIT_FAILURE);
391 exit(EXIT_SUCCESS);
392 /* If exit fails to exit... */
393 return EXIT_FAILURE;
396 static void
397 setabsolutes()
399 if (0.5 == (time_t) 0.5) {
401 ** time_t is floating.
403 if (sizeof (time_t) == sizeof (float)) {
404 absolute_min_time = (time_t) -FLT_MAX;
405 absolute_max_time = (time_t) FLT_MAX;
406 } else if (sizeof (time_t) == sizeof (double)) {
407 absolute_min_time = (time_t) -DBL_MAX;
408 absolute_max_time = (time_t) DBL_MAX;
409 } else {
410 (void) fprintf(stderr,
411 _("%s: use of -v on system with floating time_t other than float or double\n"),
412 progname);
413 (void) exit(EXIT_FAILURE);
415 } else if (0 > (time_t) -1) {
417 ** time_t is signed.
419 register time_t hibit;
421 for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
422 continue;
423 absolute_min_time = hibit;
424 absolute_max_time = -(hibit + 1);
425 } else {
427 ** time_t is unsigned.
429 absolute_min_time = 0;
430 absolute_max_time = absolute_min_time - 1;
434 static time_t
435 yeartot(y)
436 const long y;
438 register long myy;
439 register long seconds;
440 register time_t t;
442 myy = EPOCH_YEAR;
443 t = 0;
444 while (myy != y) {
445 if (myy < y) {
446 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
447 ++myy;
448 if (t > absolute_max_time - seconds) {
449 t = absolute_max_time;
450 break;
452 t += seconds;
453 } else {
454 --myy;
455 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
456 if (t < absolute_min_time + seconds) {
457 t = absolute_min_time;
458 break;
460 t -= seconds;
463 return t;
466 static time_t
467 hunt(name, lot, hit)
468 char * name;
469 time_t lot;
470 time_t hit;
472 time_t t;
473 long diff;
474 struct tm lotm;
475 register struct tm * lotmp;
476 struct tm tm;
477 register struct tm * tmp;
478 char loab[MAX_STRING_LENGTH];
480 lotmp = my_localtime(&lot);
481 if (lotmp != NULL) {
482 lotm = *lotmp;
483 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
485 for ( ; ; ) {
486 diff = (long) (hit - lot);
487 if (diff < 2)
488 break;
489 t = lot;
490 t += diff / 2;
491 if (t <= lot)
492 ++t;
493 else if (t >= hit)
494 --t;
495 tmp = my_localtime(&t);
496 if (tmp != NULL)
497 tm = *tmp;
498 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
499 (delta(&tm, &lotm) == (t - lot) &&
500 tm.tm_isdst == lotm.tm_isdst &&
501 strcmp(abbr(&tm), loab) == 0)) {
502 lot = t;
503 lotm = tm;
504 lotmp = tmp;
505 } else hit = t;
507 show(name, lot, TRUE);
508 show(name, hit, TRUE);
509 return hit;
513 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
516 static long
517 delta(newp, oldp)
518 struct tm * newp;
519 struct tm * oldp;
521 register long result;
522 register int tmy;
524 if (newp->tm_year < oldp->tm_year)
525 return -delta(oldp, newp);
526 result = 0;
527 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
528 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
529 result += newp->tm_yday - oldp->tm_yday;
530 result *= HOURSPERDAY;
531 result += newp->tm_hour - oldp->tm_hour;
532 result *= MINSPERHOUR;
533 result += newp->tm_min - oldp->tm_min;
534 result *= SECSPERMIN;
535 result += newp->tm_sec - oldp->tm_sec;
536 return result;
539 static void
540 show(zone, t, v)
541 char * zone;
542 time_t t;
543 int v;
545 register struct tm * tmp;
547 (void) printf("%-*s ", (int) longest, zone);
548 if (v) {
549 tmp = gmtime(&t);
550 if (tmp == NULL) {
551 (void) printf(tformat(), t);
552 } else {
553 dumptime(tmp);
554 (void) printf(" UTC");
556 (void) printf(" = ");
558 tmp = my_localtime(&t);
559 dumptime(tmp);
560 if (tmp != NULL) {
561 if (*abbr(tmp) != '\0')
562 (void) printf(" %s", abbr(tmp));
563 if (v) {
564 (void) printf(" isdst=%d", tmp->tm_isdst);
565 #ifdef TM_GMTOFF
566 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
567 #endif /* defined TM_GMTOFF */
570 (void) printf("\n");
571 if (tmp != NULL && *abbr(tmp) != '\0')
572 abbrok(abbr(tmp), zone);
575 static char *
576 abbr(tmp)
577 struct tm * tmp;
579 register char * result;
580 static char nada;
582 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
583 return &nada;
584 result = tzname[tmp->tm_isdst];
585 return (result == NULL) ? &nada : result;
589 ** The code below can fail on certain theoretical systems;
590 ** it works on all known real-world systems as of 2004-12-30.
593 static const char *
594 tformat()
596 if (0.5 == (time_t) 0.5) { /* floating */
597 if (sizeof (time_t) > sizeof (double))
598 return "%Lg";
599 return "%g";
601 if (0 > (time_t) -1) { /* signed */
602 if (sizeof (time_t) > sizeof (long))
603 return "%lld";
604 if (sizeof (time_t) > sizeof (int))
605 return "%ld";
606 return "%d";
608 if (sizeof (time_t) > sizeof (unsigned long))
609 return "%llu";
610 if (sizeof (time_t) > sizeof (unsigned int))
611 return "%lu";
612 return "%u";
615 static void
616 dumptime(timeptr)
617 register const struct tm * timeptr;
619 static const char wday_name[][3] = {
620 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
622 static const char mon_name[][3] = {
623 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
624 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
626 register const char * wn;
627 register const char * mn;
628 register int lead;
629 register int trail;
631 if (timeptr == NULL) {
632 (void) printf("NULL");
633 return;
636 ** The packaged versions of localtime and gmtime never put out-of-range
637 ** values in tm_wday or tm_mon, but since this code might be compiled
638 ** with other (perhaps experimental) versions, paranoia is in order.
640 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
641 (int) (sizeof wday_name / sizeof wday_name[0]))
642 wn = "???";
643 else wn = wday_name[timeptr->tm_wday];
644 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
645 (int) (sizeof mon_name / sizeof mon_name[0]))
646 mn = "???";
647 else mn = mon_name[timeptr->tm_mon];
648 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
649 wn, mn,
650 timeptr->tm_mday, timeptr->tm_hour,
651 timeptr->tm_min, timeptr->tm_sec);
652 #define DIVISOR 10
653 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
654 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
655 trail / DIVISOR;
656 trail %= DIVISOR;
657 if (trail < 0 && lead > 0) {
658 trail += DIVISOR;
659 --lead;
660 } else if (lead < 0 && trail > 0) {
661 trail -= DIVISOR;
662 ++lead;
664 if (lead == 0)
665 (void) printf("%d", trail);
666 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));