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