Update from tzcode2005k.
[kopensolaris-gnu/glibc.git] / timezone / zdump.c
1 static char     elsieid[] = "@(#)zdump.c        7.65";
2
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 */
8
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
16 #ifndef ZDUMP_LO_YEAR
17 #define ZDUMP_LO_YEAR   (-500)
18 #endif /* !defined ZDUMP_LO_YEAR */
19
20 #ifndef ZDUMP_HI_YEAR
21 #define ZDUMP_HI_YEAR   2500
22 #endif /* !defined ZDUMP_HI_YEAR */
23
24 #ifndef MAX_STRING_LENGTH
25 #define MAX_STRING_LENGTH       1024
26 #endif /* !defined MAX_STRING_LENGTH */
27
28 #ifndef TRUE
29 #define TRUE            1
30 #endif /* !defined TRUE */
31
32 #ifndef FALSE
33 #define FALSE           0
34 #endif /* !defined FALSE */
35
36 #ifndef EXIT_SUCCESS
37 #define EXIT_SUCCESS    0
38 #endif /* !defined EXIT_SUCCESS */
39
40 #ifndef EXIT_FAILURE
41 #define EXIT_FAILURE    1
42 #endif /* !defined EXIT_FAILURE */
43
44 #ifndef SECSPERMIN
45 #define SECSPERMIN      60
46 #endif /* !defined SECSPERMIN */
47
48 #ifndef MINSPERHOUR
49 #define MINSPERHOUR     60
50 #endif /* !defined MINSPERHOUR */
51
52 #ifndef SECSPERHOUR
53 #define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
54 #endif /* !defined SECSPERHOUR */
55
56 #ifndef HOURSPERDAY
57 #define HOURSPERDAY     24
58 #endif /* !defined HOURSPERDAY */
59
60 #ifndef EPOCH_YEAR
61 #define EPOCH_YEAR      1970
62 #endif /* !defined EPOCH_YEAR */
63
64 #ifndef TM_YEAR_BASE
65 #define TM_YEAR_BASE    1900
66 #endif /* !defined TM_YEAR_BASE */
67
68 #ifndef DAYSPERNYEAR
69 #define DAYSPERNYEAR    365
70 #endif /* !defined DAYSPERNYEAR */
71
72 #ifndef isleap
73 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
74 #endif /* !defined isleap */
75
76 #ifndef isleap_sum
77 /*
78 ** See tzfile.h for details on isleap_sum.
79 */
80 #define isleap_sum(a, b)        isleap((a) % 400 + (b) % 400)
81 #endif /* !defined isleap_sum */
82
83 #define SECSPERDAY      ((long) SECSPERHOUR * HOURSPERDAY)
84 #define SECSPERNYEAR    (SECSPERDAY * DAYSPERNYEAR)
85 #define SECSPERLYEAR    (SECSPERNYEAR + SECSPERDAY)
86
87 #if HAVE_GETTEXT
88 #include "locale.h"     /* for setlocale */
89 #include "libintl.h"
90 #endif /* HAVE_GETTEXT */
91
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 */
101
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 */
109
110 /*
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.
114 */
115
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 _ */
123
124 #ifndef TZ_DOMAIN
125 #define TZ_DOMAIN "tz"
126 #endif /* !defined TZ_DOMAIN */
127
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 */
135
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];
142
143 static time_t   absolute_min_time;
144 static time_t   absolute_max_time;
145 static size_t   longest;
146 static char *   progname;
147 static int      warned;
148
149 static char *   abbr P((struct tm * tmp));
150 static void     abbrok P((const char * abbr, const char * zone));
151 static long     delta P((struct tm * newp, struct tm * oldp));
152 static void     dumptime P((const struct tm * tmp));
153 static time_t   hunt P((char * name, time_t lot, time_t hit));
154 static void     setabsolutes P((void));
155 static void     show P((char * zone, time_t t, int v));
156 static const char *     tformat P((void));
157 static time_t   yeartot P((long y));
158
159 #ifndef TYPECHECK
160 #define my_localtime    localtime
161 #else /* !defined TYPECHECK */
162 static struct tm *
163 my_localtime(tp)
164 time_t *        tp;
165 {
166         register struct tm *    tmp;
167
168         tmp = localtime(tp);
169         if (tp != NULL && tmp != NULL) {
170                 struct tm       tm;
171                 register time_t t;
172
173                 tm = *tmp;
174                 t = mktime(&tm);
175                 if (t - *tp >= 1 || *tp - t >= 1) {
176                         (void) fflush(stdout);
177                         (void) fprintf(stderr, "\n%s: ", progname);
178                         (void) fprintf(stderr, tformat(), *tp);
179                         (void) fprintf(stderr, " ->");
180                         (void) fprintf(stderr, " year=%d", tmp->tm_year);
181                         (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
182                         (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
183                         (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
184                         (void) fprintf(stderr, " min=%d", tmp->tm_min);
185                         (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
186                         (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
187                         (void) fprintf(stderr, " -> ");
188                         (void) fprintf(stderr, tformat(), t);
189                         (void) fprintf(stderr, "\n");
190                 }
191         }
192         return tmp;
193 }
194 #endif /* !defined TYPECHECK */
195
196 static void
197 abbrok(abbr, zone)
198 const char * const      abbr;
199 const char * const      zone;
200 {
201         register int            i;
202         register const char *   cp;
203         register char *         wp;
204
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;
222         }
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;
232 }
233
234 int
235 main(argc, argv)
236 int     argc;
237 char *  argv[];
238 {
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;
255
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);
270                 }
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);
283         }
284         if (vflag) {
285                 if (cutarg != NULL) {
286                         long    lo;
287                         long    hi;
288                         char    dummy;
289
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);
300                         }
301                 }
302                 setabsolutes();
303                 cutlotime = yeartot(cutloyear);
304                 cuthitime = yeartot(cuthiyear);
305         }
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]);
311         {
312                 register int    from;
313                 register int    to;
314
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);
323                 }
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;
331         }
332         for (i = optind; i < argc; ++i) {
333                 static char     buf[MAX_STRING_LENGTH];
334
335                 (void) strcpy(&fakeenv[0][3], argv[i]);
336                 if (!vflag) {
337                         show(argv[i], now, FALSE);
338                         continue;
339                 }
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);
351                 }
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);
374                                         }
375                         }
376                         t = newt;
377                         tm = newtm;
378                         tmp = newtmp;
379                 }
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);
385         }
386         if (fflush(stdout) || ferror(stdout)) {
387                 (void) fprintf(stderr, "%s: ", progname);
388                 (void) perror(_("Error writing standard output"));
389                 (void) exit(EXIT_FAILURE);
390         }
391         exit(EXIT_SUCCESS);
392         /* If exit fails to exit... */
393         return EXIT_FAILURE;
394 }
395
396 static void
397 setabsolutes()
398 {
399         if (0.5 == (time_t) 0.5) {
400                 /*
401                 ** time_t is floating.
402                 */
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);
414                 }
415         } else if (0 > (time_t) -1) {
416                 /*
417                 ** time_t is signed.
418                 */
419                 register time_t hibit;
420
421                 for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
422                         continue;
423                 absolute_min_time = hibit;
424                 absolute_max_time = -(hibit + 1);
425         } else {
426                 /*
427                 ** time_t is unsigned.
428                 */
429                 absolute_min_time = 0;
430                 absolute_max_time = absolute_min_time - 1;
431         }
432 }
433
434 static time_t
435 yeartot(y)
436 const long      y;
437 {
438         register long   myy;
439         register long   seconds;
440         register time_t t;
441
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;
451                         }
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;
459                         }
460                         t -= seconds;
461                 }
462         }
463         return t;
464 }
465
466 static time_t
467 hunt(name, lot, hit)
468 char *  name;
469 time_t  lot;
470 time_t  hit;
471 {
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];
479
480         lotmp = my_localtime(&lot);
481         if (lotmp != NULL) {
482                 lotm = *lotmp;
483                 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
484         }
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;
506         }
507         show(name, lot, TRUE);
508         show(name, hit, TRUE);
509         return hit;
510 }
511
512 /*
513 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
514 */
515
516 static long
517 delta(newp, oldp)
518 struct tm *     newp;
519 struct tm *     oldp;
520 {
521         register long   result;
522         register int    tmy;
523
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;
537 }
538
539 static void
540 show(zone, t, v)
541 char *  zone;
542 time_t  t;
543 int     v;
544 {
545         register struct tm *    tmp;
546
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");
555                 }
556                 (void) printf(" = ");
557         }
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 */
568                 }
569         }
570         (void) printf("\n");
571         if (tmp != NULL && *abbr(tmp) != '\0')
572                 abbrok(abbr(tmp), zone);
573 }
574
575 static char *
576 abbr(tmp)
577 struct tm *     tmp;
578 {
579         register char * result;
580         static char     nada;
581
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;
586 }
587
588 /*
589 ** The code below can fail on certain theoretical systems;
590 ** it works on all known real-world systems as of 2004-12-30.
591 */
592
593 static const char *
594 tformat()
595 {
596         if (0.5 == (time_t) 0.5) {      /* floating */
597                 if (sizeof (time_t) > sizeof (double))
598                         return "%Lg";
599                 return "%g";
600         }
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";
607         }
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";
613 }
614
615 static void
616 dumptime(timeptr)
617 register const struct tm *      timeptr;
618 {
619         static const char       wday_name[][3] = {
620                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
621         };
622         static const char       mon_name[][3] = {
623                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
624                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
625         };
626         register const char *   wn;
627         register const char *   mn;
628         register int            lead;
629         register int            trail;
630
631         if (timeptr == NULL) {
632                 (void) printf("NULL");
633                 return;
634         }
635         /*
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.
639         */
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;
663         }
664         if (lead == 0)
665                 (void) printf("%d", trail);
666         else    (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
667 }