]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/tzcode/zic/zdump.c
Merge r307360 from stable/9:
[FreeBSD/releng/9.3.git] / contrib / tzcode / zic / 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 #ifndef lint
7 static const char rcsid[] =
8   "$FreeBSD$";
9 static char     elsieid[] = "@(#)zdump.c        8.10";
10 #endif /* not lint */
11
12 /*
13 ** This code has been made independent of the rest of the time
14 ** conversion package to increase confidence in the verification it provides.
15 ** You can use this code to help in verifying other implementations.
16 */
17
18 #include <err.h>
19 #include <stdio.h>      /* for stdout, stderr */
20 #include <stdlib.h>     /* for exit, malloc, atoi */
21 #include <string.h>     /* for strcpy */
22 #include <sys/types.h>  /* for time_t */
23 #include <time.h>       /* for struct tm */
24 #include <unistd.h>
25 #include <float.h>      /* for FLT_MAX and DBL_MAX */
26 #include <ctype.h>      /* for isalpha et al. */
27 #ifndef isascii
28 #define isascii(x) 1
29 #endif /* !defined isascii */
30
31 #ifndef ZDUMP_LO_YEAR
32 #define ZDUMP_LO_YEAR   (-500)
33 #endif /* !defined ZDUMP_LO_YEAR */
34
35 #ifndef ZDUMP_HI_YEAR
36 #define ZDUMP_HI_YEAR   2500
37 #endif /* !defined ZDUMP_HI_YEAR */
38
39 #ifndef MAX_STRING_LENGTH
40 #define MAX_STRING_LENGTH       1024
41 #endif /* !defined MAX_STRING_LENGTH */
42
43 #ifndef TRUE
44 #define TRUE            1
45 #endif /* !defined TRUE */
46
47 #ifndef FALSE
48 #define FALSE           0
49 #endif /* !defined FALSE */
50
51 #ifndef EXIT_SUCCESS
52 #define EXIT_SUCCESS    0
53 #endif /* !defined EXIT_SUCCESS */
54
55 #ifndef EXIT_FAILURE
56 #define EXIT_FAILURE    1
57 #endif /* !defined EXIT_FAILURE */
58
59 #ifndef SECSPERMIN
60 #define SECSPERMIN      60
61 #endif /* !defined SECSPERMIN */
62
63 #ifndef MINSPERHOUR
64 #define MINSPERHOUR     60
65 #endif /* !defined MINSPERHOUR */
66
67 #ifndef SECSPERHOUR
68 #define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
69 #endif /* !defined SECSPERHOUR */
70
71 #ifndef HOURSPERDAY
72 #define HOURSPERDAY     24
73 #endif /* !defined HOURSPERDAY */
74
75 #ifndef EPOCH_YEAR
76 #define EPOCH_YEAR      1970
77 #endif /* !defined EPOCH_YEAR */
78
79 #ifndef TM_YEAR_BASE
80 #define TM_YEAR_BASE    1900
81 #endif /* !defined TM_YEAR_BASE */
82
83 #ifndef DAYSPERNYEAR
84 #define DAYSPERNYEAR    365
85 #endif /* !defined DAYSPERNYEAR */
86
87 #ifndef isleap
88 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
89 #endif /* !defined isleap */
90
91 #ifndef isleap_sum
92 /*
93 ** See tzfile.h for details on isleap_sum.
94 */
95 #define isleap_sum(a, b)        isleap((a) % 400 + (b) % 400)
96 #endif /* !defined isleap_sum */
97
98 #define SECSPERDAY      ((long) SECSPERHOUR * HOURSPERDAY)
99 #define SECSPERNYEAR    (SECSPERDAY * DAYSPERNYEAR)
100 #define SECSPERLYEAR    (SECSPERNYEAR + SECSPERDAY)
101
102 #ifndef HAVE_GETTEXT
103 #define HAVE_GETTEXT 0
104 #endif
105 #if HAVE_GETTEXT
106 #include "locale.h"     /* for setlocale */
107 #include "libintl.h"
108 #endif /* HAVE_GETTEXT */
109
110 #ifndef GNUC_or_lint
111 #ifdef lint
112 #define GNUC_or_lint
113 #else /* !defined lint */
114 #ifdef __GNUC__
115 #define GNUC_or_lint
116 #endif /* defined __GNUC__ */
117 #endif /* !defined lint */
118 #endif /* !defined GNUC_or_lint */
119
120 #ifndef INITIALIZE
121 #ifdef GNUC_or_lint
122 #define INITIALIZE(x)   ((x) = 0)
123 #else /* !defined GNUC_or_lint */
124 #define INITIALIZE(x)
125 #endif /* !defined GNUC_or_lint */
126 #endif /* !defined INITIALIZE */
127
128 /*
129 ** For the benefit of GNU folk...
130 ** `_(MSGID)' uses the current locale's message library string for MSGID.
131 ** The default is to use gettext if available, and use MSGID otherwise.
132 */
133
134 #ifndef _
135 #if HAVE_GETTEXT
136 #define _(msgid) gettext(msgid)
137 #else /* !(HAVE_GETTEXT) */
138 #define _(msgid) msgid
139 #endif /* !(HAVE_GETTEXT) */
140 #endif /* !defined _ */
141
142 #ifndef TZ_DOMAIN
143 #define TZ_DOMAIN "tz"
144 #endif /* !defined TZ_DOMAIN */
145
146 extern char **  environ;
147 extern char *   tzname[2];
148
149 static time_t   absolute_min_time;
150 static time_t   absolute_max_time;
151 static size_t   longest;
152 static char *   progname;
153 static int      warned;
154
155 static void     usage(FILE *stream, int status);
156 static char *   abbr(struct tm * tmp);
157 static void     abbrok(const char * abbrp, const char * zone);
158 static long     delta(struct tm * newp, struct tm * oldp);
159 static void     dumptime(const struct tm * tmp);
160 static time_t   hunt(char * name, time_t lot, time_t    hit);
161 static void     setabsolutes(void);
162 static void     show(char * zone, time_t t, int v);
163 static const char *     tformat(void);
164 static time_t   yeartot(long y);
165
166 #ifndef TYPECHECK
167 #define my_localtime    localtime
168 #else /* !defined TYPECHECK */
169 static struct tm *
170 my_localtime(tp)
171 time_t *        tp;
172 {
173         register struct tm *    tmp;
174
175         tmp = localtime(tp);
176         if (tp != NULL && tmp != NULL) {
177                 struct tm       tm;
178                 register time_t t;
179
180                 tm = *tmp;
181                 t = mktime(&tm);
182                 if (t - *tp >= 1 || *tp - t >= 1) {
183                         (void) fflush(stdout);
184                         (void) fprintf(stderr, "\n%s: ", progname);
185                         (void) fprintf(stderr, tformat(), *tp);
186                         (void) fprintf(stderr, " ->");
187                         (void) fprintf(stderr, " year=%d", tmp->tm_year);
188                         (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
189                         (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
190                         (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
191                         (void) fprintf(stderr, " min=%d", tmp->tm_min);
192                         (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
193                         (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
194                         (void) fprintf(stderr, " -> ");
195                         (void) fprintf(stderr, tformat(), t);
196                         (void) fprintf(stderr, "\n");
197                 }
198         }
199         return tmp;
200 }
201 #endif /* !defined TYPECHECK */
202
203 static void
204 abbrok(abbrp, zone)
205 const char * const      abbrp;
206 const char * const      zone;
207 {
208         register const char *   cp;
209         register char *         wp;
210
211         if (warned)
212                 return;
213         cp = abbrp;
214         wp = NULL;
215         while (isascii((unsigned char) *cp) &&
216                 (isalnum((unsigned char)*cp) || *cp == '-' || *cp == '+'))
217                 ++cp;
218         if (cp - abbrp < 3)
219                 wp = _("has fewer than 3 characters");
220         else if (cp - abbrp > 6)
221                 wp = _("has more than 6 characters");
222         else if (*cp)
223                 wp = "has characters other than ASCII alphanumerics, '-' or '+'";
224         else
225                 return;
226         (void) fflush(stdout);
227         (void) fprintf(stderr,
228                 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
229                 progname, zone, abbrp, wp);
230         warned = TRUE;
231 }
232
233 int
234 main(argc, argv)
235 int     argc;
236 char *  argv[];
237 {
238         register int            i;
239         register int            c;
240         register int            vflag;
241         register char *         cutarg;
242         register long           cutloyear = ZDUMP_LO_YEAR;
243         register long           cuthiyear = ZDUMP_HI_YEAR;
244         register time_t         cutlotime;
245         register time_t         cuthitime;
246         register char **        fakeenv;
247         time_t                  now;
248         time_t                  t;
249         time_t                  newt;
250         struct tm               tm;
251         struct tm               newtm;
252         register struct tm *    tmp;
253         register struct tm *    newtmp;
254
255         INITIALIZE(cutlotime);
256         INITIALIZE(cuthitime);
257 #if HAVE_GETTEXT
258         (void) setlocale(LC_MESSAGES, "");
259 #ifdef TZ_DOMAINDIR
260         (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
261 #endif /* TEXTDOMAINDIR */
262         (void) textdomain(TZ_DOMAIN);
263 #endif /* HAVE_GETTEXT */
264         for (i = 1; i < argc; ++i)
265                 if (strcmp(argv[i], "--version") == 0) {
266                         errx(EXIT_SUCCESS, "%s", elsieid);
267                 } else if (strcmp(argv[i], "--help") == 0) {
268                         usage(stdout, EXIT_SUCCESS);
269                 }
270         vflag = 0;
271         cutarg = NULL;
272         while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
273                 if (c == 'v')
274                         vflag = 1;
275                 else    cutarg = optarg;
276         if ((c != -1) ||
277                 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
278                         usage(stderr, EXIT_FAILURE);
279         }
280         if (vflag) {
281                 if (cutarg != NULL) {
282                         long    lo;
283                         long    hi;
284                         char    dummy;
285
286                         if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
287                                 cuthiyear = hi;
288                         } else if (sscanf(cutarg, "%ld,%ld%c",
289                                 &lo, &hi, &dummy) == 2) {
290                                         cutloyear = lo;
291                                         cuthiyear = hi;
292                         } else {
293 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
294                                         progname, cutarg);
295                                 exit(EXIT_FAILURE);
296                         }
297                 }
298                 setabsolutes();
299                 cutlotime = yeartot(cutloyear);
300                 cuthitime = yeartot(cuthiyear);
301         }
302         (void) time(&now);
303         longest = 0;
304         for (i = optind; i < argc; ++i)
305                 if (strlen(argv[i]) > longest)
306                         longest = strlen(argv[i]);
307         {
308                 register int    from;
309                 register int    to;
310
311                 for (i = 0;  environ[i] != NULL; ++i)
312                         continue;
313                 fakeenv = (char **) malloc((size_t) ((i + 2) *
314                         sizeof *fakeenv));
315                 if (fakeenv == NULL ||
316                         (fakeenv[0] = (char *) malloc((size_t) (longest +
317                                 4))) == NULL)
318                                         errx(EXIT_FAILURE,
319                                              _("malloc() failed"));
320                 to = 0;
321                 (void) strcpy(fakeenv[to++], "TZ=");
322                 for (from = 0; environ[from] != NULL; ++from)
323                         if (strncmp(environ[from], "TZ=", 3) != 0)
324                                 fakeenv[to++] = environ[from];
325                 fakeenv[to] = NULL;
326                 environ = fakeenv;
327         }
328         for (i = optind; i < argc; ++i) {
329                 static char     buf[MAX_STRING_LENGTH];
330
331                 (void) strcpy(&fakeenv[0][3], argv[i]);
332                 if (!vflag) {
333                         show(argv[i], now, FALSE);
334                         continue;
335                 }
336                 warned = FALSE;
337                 t = absolute_min_time;
338                 show(argv[i], t, TRUE);
339                 t += SECSPERHOUR * HOURSPERDAY;
340                 show(argv[i], t, TRUE);
341                 if (t < cutlotime)
342                         t = cutlotime;
343                 tmp = my_localtime(&t);
344                 if (tmp != NULL) {
345                         tm = *tmp;
346                         (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
347                 }
348                 for ( ; ; ) {
349                         if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
350                                 break;
351                         newt = t + SECSPERHOUR * 12;
352                         newtmp = localtime(&newt);
353                         if (newtmp != NULL)
354                                 newtm = *newtmp;
355                         if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
356                                 (delta(&newtm, &tm) != (newt - t) ||
357                                 newtm.tm_isdst != tm.tm_isdst ||
358                                 strcmp(abbr(&newtm), buf) != 0)) {
359                                         newt = hunt(argv[i], t, newt);
360                                         newtmp = localtime(&newt);
361                                         if (newtmp != NULL) {
362                                                 newtm = *newtmp;
363                                                 (void) strncpy(buf,
364                                                         abbr(&newtm),
365                                                         (sizeof buf) - 1);
366                                         }
367                         }
368                         t = newt;
369                         tm = newtm;
370                         tmp = newtmp;
371                 }
372                 t = absolute_max_time;
373                 t -= SECSPERHOUR * HOURSPERDAY;
374                 show(argv[i], t, TRUE);
375                 t += SECSPERHOUR * HOURSPERDAY;
376                 show(argv[i], t, TRUE);
377         }
378         if (fflush(stdout) || ferror(stdout))
379                 errx(EXIT_FAILURE, _("error writing standard output"));
380         exit(EXIT_SUCCESS);
381         /* If exit fails to exit... */
382         return(EXIT_FAILURE);
383 }
384
385 static void
386 setabsolutes(void)
387 {
388         if (0.5 == (time_t) 0.5) {
389                 /*
390                 ** time_t is floating.
391                 */
392                 if (sizeof (time_t) == sizeof (float)) {
393                         absolute_min_time = (time_t) -FLT_MAX;
394                         absolute_max_time = (time_t) FLT_MAX;
395                 } else if (sizeof (time_t) == sizeof (double)) {
396                         absolute_min_time = (time_t) -DBL_MAX;
397                         absolute_max_time = (time_t) DBL_MAX;
398                 } else {
399                         (void) fprintf(stderr,
400 _("%s: use of -v on system with floating time_t other than float or double\n"),
401                                 progname);
402                         exit(EXIT_FAILURE);
403                 }
404         } else if (0 > (time_t) -1) {
405                 /*
406                 ** time_t is signed.  Assume overflow wraps around.
407                 */
408                 time_t t = 0;
409                 time_t t1 = 1;
410
411                 while (t < t1) {
412                         t = t1;
413                         t1 = 2 * t1 + 1;
414                 }
415
416                 absolute_max_time = t;
417                 t = -t;
418                 absolute_min_time = t - 1;
419                 if (t < absolute_min_time)
420                         absolute_min_time = t;
421         } else {
422                 /*
423                 ** time_t is unsigned.
424                 */
425                 absolute_min_time = 0;
426                 absolute_max_time = absolute_min_time - 1;
427         }
428 }
429
430 static time_t
431 yeartot(y)
432 const long      y;
433 {
434         register long   myy;
435         register long   seconds;
436         register time_t t;
437
438         myy = EPOCH_YEAR;
439         t = 0;
440         while (myy != y) {
441                 if (myy < y) {
442                         seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
443                         ++myy;
444                         if (t > absolute_max_time - seconds) {
445                                 t = absolute_max_time;
446                                 break;
447                         }
448                         t += seconds;
449                 } else {
450                         --myy;
451                         seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
452                         if (t < absolute_min_time + seconds) {
453                                 t = absolute_min_time;
454                                 break;
455                         }
456                         t -= seconds;
457                 }
458         }
459         return t;
460 }
461
462 static void
463 usage(FILE *stream, int status)
464 {
465         fprintf(stream,
466 _("usage: %s [--version] [-v] [--help] [-c [loyear,]hiyear] zonename ...\n\
467 \n\
468 Report bugs to tz@elsie.nci.nih.gov.\n"), progname);
469         exit(status);
470 }
471
472 static time_t
473 hunt(char *name, time_t lot, time_t hit)
474 {
475         time_t                  t;
476         long                    diff;
477         struct tm               lotm;
478         register struct tm *    lotmp;
479         struct tm               tm;
480         register struct tm *    tmp;
481         char                    loab[MAX_STRING_LENGTH];
482
483         lotmp = my_localtime(&lot);
484         if (lotmp != NULL) {
485                 lotm = *lotmp;
486                 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
487         }
488         for ( ; ; ) {
489                 diff = (long) (hit - lot);
490                 if (diff < 2)
491                         break;
492                 t = lot;
493                 t += diff / 2;
494                 if (t <= lot)
495                         ++t;
496                 else if (t >= hit)
497                         --t;
498                 tmp = my_localtime(&t);
499                 if (tmp != NULL)
500                         tm = *tmp;
501                 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
502                         (delta(&tm, &lotm) == (t - lot) &&
503                         tm.tm_isdst == lotm.tm_isdst &&
504                         strcmp(abbr(&tm), loab) == 0)) {
505                                 lot = t;
506                                 lotm = tm;
507                                 lotmp = tmp;
508                 } else  hit = t;
509         }
510         show(name, lot, TRUE);
511         show(name, hit, TRUE);
512         return hit;
513 }
514
515 /*
516 ** Thanks to Paul Eggert for logic used in delta.
517 */
518
519 static long
520 delta(newp, oldp)
521 struct tm *     newp;
522 struct tm *     oldp;
523 {
524         register long   result;
525         register int    tmy;
526
527         if (newp->tm_year < oldp->tm_year)
528                 return -delta(oldp, newp);
529         result = 0;
530         for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
531                 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
532         result += newp->tm_yday - oldp->tm_yday;
533         result *= HOURSPERDAY;
534         result += newp->tm_hour - oldp->tm_hour;
535         result *= MINSPERHOUR;
536         result += newp->tm_min - oldp->tm_min;
537         result *= SECSPERMIN;
538         result += newp->tm_sec - oldp->tm_sec;
539         return result;
540 }
541
542 static void
543 show(char *zone, time_t t, 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(void)
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 }