1 static char elsieid[] = "@(#)zdump.c 8.8";
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.
10 * ICU note: Mr. Arthur David Olson (olsona@dc37a.nci.nih.gov) stated that
11 * "zdump.c is indeed in the public domain" in e-mail on Feb 22, 2007.
12 * This version of zdump.c is modified by ICU team to change output format
13 * and some additional options.
17 #include "stdio.h" /* for stdout, stderr, perror */
18 #include "string.h" /* for strcpy */
19 #include "sys/types.h" /* for time_t */
20 #include "time.h" /* for struct tm */
21 #include "stdlib.h" /* for exit, malloc, atoi */
22 #include "float.h" /* for FLT_MAX and DBL_MAX */
23 #include "ctype.h" /* for isalpha et al. */
25 /* Enable extensions and modifications for ICU. */
34 #endif /* !defined isascii */
37 #define ZDUMP_LO_YEAR (-500)
38 #endif /* !defined ZDUMP_LO_YEAR */
41 #define ZDUMP_HI_YEAR 2500
42 #endif /* !defined ZDUMP_HI_YEAR */
44 #ifndef MAX_STRING_LENGTH
45 #define MAX_STRING_LENGTH 1024
46 #endif /* !defined MAX_STRING_LENGTH */
50 #endif /* !defined TRUE */
54 #endif /* !defined FALSE */
57 #define EXIT_SUCCESS 0
58 #endif /* !defined EXIT_SUCCESS */
61 #define EXIT_FAILURE 1
62 #endif /* !defined EXIT_FAILURE */
66 #endif /* !defined SECSPERMIN */
69 #define MINSPERHOUR 60
70 #endif /* !defined MINSPERHOUR */
73 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
74 #endif /* !defined SECSPERHOUR */
77 #define HOURSPERDAY 24
78 #endif /* !defined HOURSPERDAY */
81 #define EPOCH_YEAR 1970
82 #endif /* !defined EPOCH_YEAR */
85 #define TM_YEAR_BASE 1900
86 #endif /* !defined TM_YEAR_BASE */
89 #define DAYSPERNYEAR 365
90 #endif /* !defined DAYSPERNYEAR */
93 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
94 #endif /* !defined isleap */
98 ** See tzfile.h for details on isleap_sum.
100 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
101 #endif /* !defined isleap_sum */
103 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
104 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
105 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
108 #define HAVE_GETTEXT 0
111 #include "locale.h" /* for setlocale */
113 #endif /* HAVE_GETTEXT */
118 #else /* !defined lint */
121 #endif /* defined __GNUC__ */
122 #endif /* !defined lint */
123 #endif /* !defined GNUC_or_lint */
127 #define INITIALIZE(x) ((x) = 0)
128 #else /* !defined GNUC_or_lint */
129 #define INITIALIZE(x)
130 #endif /* !defined GNUC_or_lint */
131 #endif /* !defined INITIALIZE */
134 ** For the benefit of GNU folk...
135 ** `_(MSGID)' uses the current locale's message library string for MSGID.
136 ** The default is to use gettext if available, and use MSGID otherwise.
141 #define _(msgid) gettext(msgid)
142 #else /* !HAVE_GETTEXT */
143 #define _(msgid) msgid
144 #endif /* !HAVE_GETTEXT */
145 #endif /* !defined _ */
148 #define TZ_DOMAIN "tz"
149 #endif /* !defined TZ_DOMAIN */
151 extern char ** environ;
152 extern int getopt(int argc, char * const argv[],
153 const char * options);
154 extern char * optarg;
156 extern char * tzname[2];
158 static time_t absolute_min_time;
159 static time_t absolute_max_time;
160 static size_t longest;
161 static char * progname;
164 static char * abbr(struct tm * tmp);
165 static void abbrok(const char * abbrp, const char * zone);
166 static long delta(struct tm * newp, struct tm * oldp);
167 static void dumptime(const struct tm * tmp);
168 static time_t hunt(char * name, time_t lot, time_t hit);
169 static void setabsolutes(void);
170 static void show(char * zone, time_t t, int v);
171 static const char * tformat(void);
172 static time_t yeartot(long y);
174 typedef struct listentry {
176 struct listentry * next;
179 static time_t huntICU(char * name, time_t lot, time_t hit, FILE *fp);
180 static void dumptimeICU(FILE * fp, time_t t);
181 static void showICU(FILE * fp, char * zone, time_t t1, time_t t2);
182 static int getall(struct listentry ** namelist);
183 static void getzones(char * basedir, char * subdir, struct listentry ** last, int * count);
187 #define my_localtime localtime
188 #else /* !defined TYPECHECK */
193 register struct tm * tmp;
196 if (tp != NULL && tmp != NULL) {
202 if (t - *tp >= 1 || *tp - t >= 1) {
203 (void) fflush(stdout);
204 (void) fprintf(stderr, "\n%s: ", progname);
205 (void) fprintf(stderr, tformat(), *tp);
206 (void) fprintf(stderr, " ->");
207 (void) fprintf(stderr, " year=%d", tmp->tm_year);
208 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
209 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
210 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
211 (void) fprintf(stderr, " min=%d", tmp->tm_min);
212 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
213 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
214 (void) fprintf(stderr, " -> ");
215 (void) fprintf(stderr, tformat(), t);
216 (void) fprintf(stderr, "\n");
221 #endif /* !defined TYPECHECK */
225 const char * const abbrp;
226 const char * const zone;
228 register const char * cp;
235 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
238 wp = _("lacks alphabetic at start");
239 else if (cp - abbrp < 3)
240 wp = _("has fewer than 3 alphabetics");
241 else if (cp - abbrp > 6)
242 wp = _("has more than 6 alphabetics");
243 if (wp == NULL && (*cp == '+' || *cp == '-')) {
245 if (isascii((unsigned char) *cp) &&
246 isdigit((unsigned char) *cp))
247 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
250 wp = _("differs from POSIX standard");
254 (void) fflush(stdout);
255 (void) fprintf(stderr,
256 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
257 progname, zone, abbrp, wp);
262 usage(const char *progname, FILE *stream, int status)
264 (void) fprintf(stream,
265 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
267 Report bugs to tz@elsie.nci.nih.gov.\n"),
280 register char * cutarg;
281 register long cutloyear = ZDUMP_LO_YEAR;
282 register long cuthiyear = ZDUMP_HI_YEAR;
283 register time_t cutlotime;
284 register time_t cuthitime;
285 register char ** fakeenv;
291 register struct tm * tmp;
292 register struct tm * newtmp;
298 listentry * namelist = NULL;
302 INITIALIZE(cutlotime);
303 INITIALIZE(cuthitime);
305 (void) setlocale(LC_ALL, "");
307 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
308 #endif /* defined TEXTDOMAINDIR */
309 (void) textdomain(TZ_DOMAIN);
310 #endif /* HAVE_GETTEXT */
312 for (i = 1; i < argc; ++i)
313 if (strcmp(argv[i], "--version") == 0) {
314 (void) printf("%s\n", elsieid);
316 } else if (strcmp(argv[i], "--help") == 0) {
317 usage(progname, stdout, EXIT_SUCCESS);
327 c = getopt(argc, argv, "ac:d:iv");
349 if ((c != EOF && c != -1) ||
350 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
351 (void) fprintf(stderr,
352 _("%s: usage is %s [ --version ] [ -a ] [ -v ] [ -i ] [ -c [loyear,]hiyear ] [ -d dir ] [ zonename ... ]\n"),
357 if (dirarg != NULL) {
359 /* create the output directory */
361 if ((dp = opendir(dirarg)) == NULL) {
362 fprintf(stderr, "cannot create the target directory");
368 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
371 else cutarg = optarg;
372 if ((c != EOF && c != -1) ||
373 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
374 usage(progname, stderr, EXIT_FAILURE);
378 if (cutarg != NULL) {
383 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
385 } else if (sscanf(cutarg, "%ld,%ld%c",
386 &lo, &hi, &dummy) == 2) {
390 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
396 cutlotime = yeartot(cutloyear);
397 cuthitime = yeartot(cuthiyear);
402 /* get all available zones */
407 count = getall(&namelist);
409 fakeargv = (char **) malloc((size_t) (argc + count) * sizeof *argv);
411 if ((fakeargv = (char **) malloc((size_t) (argc + count) * sizeof *argv)) == NULL) {
415 for (i = 0; i < argc; i++) {
416 fakeargv[i] = argv[i];
418 for (i = 0; i < count; i++) {
419 fakeargv[i + argc] = namelist->name;
420 namelist = namelist->next;
428 for (i = optind; i < argc; ++i)
429 if (strlen(argv[i]) > longest)
430 longest = strlen(argv[i]);
435 for (i = 0; environ[i] != NULL; ++i)
437 fakeenv = (char **) malloc((size_t) ((i + 2) *
439 if (fakeenv == NULL ||
440 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
441 (void) perror(progname);
445 (void) strcpy(fakeenv[to++], "TZ=");
446 for (from = 0; environ[from] != NULL; ++from)
447 if (strncmp(environ[from], "TZ=", 3) != 0)
448 fakeenv[to++] = environ[from];
452 for (i = optind; i < argc; ++i) {
453 static char buf[MAX_STRING_LENGTH];
455 (void) strcpy(&fakeenv[0][3], argv[i]);
457 show(argv[i], now, FALSE);
463 if (dirarg == NULL) {
464 /* we want to display a zone name here */
468 printf("ZONE: %s\n", argv[i]);
471 char path[FILENAME_MAX + 1];
472 strcpy(path, dirarg);
474 zstart = strlen(path);
475 strcat(path, argv[i]);
476 /* replace '/' with '-' */
477 while(path[++zstart] != 0) {
478 if (path[zstart] == '/') {
482 if ((fp = fopen(path, "w")) == NULL) {
483 fprintf(stderr, "cannot create output file %s\n", path);
490 t = absolute_min_time;
492 /* skip displaying info for the lowest time, which is actually not
493 * a transition when -i option is set */
496 show(argv[i], t, TRUE);
497 t += SECSPERHOUR * HOURSPERDAY;
498 show(argv[i], t, TRUE);
504 tmp = my_localtime(&t);
507 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
510 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
512 newt = t + SECSPERHOUR * 12;
513 newtmp = localtime(&newt);
518 /* We do not want to capture transitions just for
519 * abbreviated zone name changes */
520 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
521 (delta(&newtm, &tm) != (newt - t) ||
522 newtm.tm_isdst != tm.tm_isdst)) {
523 newt = huntICU(argv[i], t, newt, fp);
524 newtmp = localtime(&newt);
525 if (newtmp != NULL) {
534 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
535 (delta(&newtm, &tm) != (newt - t) ||
536 newtm.tm_isdst != tm.tm_isdst ||
537 strcmp(abbr(&newtm), buf) != 0)) {
538 newt = hunt(argv[i], t, newt);
539 newtmp = localtime(&newt);
540 if (newtmp != NULL) {
556 /* skip displaying info for the highest time, which is actually not
557 * a transition when -i option is used*/
559 t = absolute_max_time;
560 t -= SECSPERHOUR * HOURSPERDAY;
561 show(argv[i], t, TRUE);
562 t += SECSPERHOUR * HOURSPERDAY;
563 show(argv[i], t, TRUE);
573 if (fflush(stdout) || ferror(stdout)) {
574 (void) fprintf(stderr, "%s: ", progname);
575 (void) perror(_("Error writing to standard output"));
580 struct listentry * entry = namelist;
581 struct listentry * next;
582 while (entry != NULL) {
591 /* If exit fails to exit... */
598 if (0.5 == (time_t) 0.5) {
600 ** time_t is floating.
602 if (sizeof (time_t) == sizeof (float)) {
603 absolute_min_time = (time_t) -FLT_MAX;
604 absolute_max_time = (time_t) FLT_MAX;
605 } else if (sizeof (time_t) == sizeof (double)) {
606 absolute_min_time = (time_t) -DBL_MAX;
607 absolute_max_time = (time_t) DBL_MAX;
609 (void) fprintf(stderr,
610 _("%s: use of -v on system with floating time_t other than float or double\n"),
614 } else if (0 > (time_t) -1) {
616 ** time_t is signed. Assume overflow wraps around.
626 absolute_max_time = t;
628 absolute_min_time = t - 1;
629 if (t < absolute_min_time)
630 absolute_min_time = t;
633 ** time_t is unsigned.
635 absolute_min_time = 0;
636 absolute_max_time = absolute_min_time - 1;
645 register long seconds;
652 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
654 if (t > absolute_max_time - seconds) {
655 t = absolute_max_time;
661 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
662 if (t < absolute_min_time + seconds) {
663 t = absolute_min_time;
673 hunt(char *name, time_t lot, time_t hit)
678 register struct tm * lotmp;
680 register struct tm * tmp;
681 char loab[MAX_STRING_LENGTH];
683 lotmp = my_localtime(&lot);
686 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
689 diff = (long) (hit - lot);
698 tmp = my_localtime(&t);
701 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
702 (delta(&tm, &lotm) == (t - lot) &&
703 tm.tm_isdst == lotm.tm_isdst &&
704 strcmp(abbr(&tm), loab) == 0)) {
710 show(name, lot, TRUE);
711 show(name, hit, TRUE);
716 ** Thanks to Paul Eggert for logic used in delta.
724 register long result;
727 if (newp->tm_year < oldp->tm_year)
728 return -delta(oldp, newp);
730 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
731 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
732 result += newp->tm_yday - oldp->tm_yday;
733 result *= HOURSPERDAY;
734 result += newp->tm_hour - oldp->tm_hour;
735 result *= MINSPERHOUR;
736 result += newp->tm_min - oldp->tm_min;
737 result *= SECSPERMIN;
738 result += newp->tm_sec - oldp->tm_sec;
743 show(char *zone, time_t t, int v)
745 register struct tm * tmp;
747 (void) printf("%-*s ", (int) longest, zone);
751 (void) printf(tformat(), t);
754 (void) printf(" UTC");
756 (void) printf(" = ");
758 tmp = my_localtime(&t);
761 if (*abbr(tmp) != '\0')
762 (void) printf(" %s", abbr(tmp));
764 (void) printf(" isdst=%d", tmp->tm_isdst);
766 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
767 #endif /* defined TM_GMTOFF */
771 if (tmp != NULL && *abbr(tmp) != '\0')
772 abbrok(abbr(tmp), zone);
779 register char * result;
782 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
784 result = tzname[tmp->tm_isdst];
785 return (result == NULL) ? &nada : result;
789 ** The code below can fail on certain theoretical systems;
790 ** it works on all known real-world systems as of 2004-12-30.
796 if (0.5 == (time_t) 0.5) { /* floating */
797 if (sizeof (time_t) > sizeof (double))
801 if (0 > (time_t) -1) { /* signed */
802 if (sizeof (time_t) > sizeof (long))
804 if (sizeof (time_t) > sizeof (int))
808 if (sizeof (time_t) > sizeof (unsigned long))
810 if (sizeof (time_t) > sizeof (unsigned int))
817 register const struct tm * timeptr;
819 static const char wday_name[][3] = {
820 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
822 static const char mon_name[][3] = {
823 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
824 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
826 register const char * wn;
827 register const char * mn;
831 if (timeptr == NULL) {
832 (void) printf("NULL");
836 ** The packaged versions of localtime and gmtime never put out-of-range
837 ** values in tm_wday or tm_mon, but since this code might be compiled
838 ** with other (perhaps experimental) versions, paranoia is in order.
840 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
841 (int) (sizeof wday_name / sizeof wday_name[0]))
843 else wn = wday_name[timeptr->tm_wday];
844 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
845 (int) (sizeof mon_name / sizeof mon_name[0]))
847 else mn = mon_name[timeptr->tm_mon];
848 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
850 timeptr->tm_mday, timeptr->tm_hour,
851 timeptr->tm_min, timeptr->tm_sec);
853 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
854 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
857 if (trail < 0 && lead > 0) {
860 } else if (lead < 0 && trail > 0) {
865 (void) printf("%d", trail);
866 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
871 huntICU(char *name, time_t lot, time_t hit, FILE * fp)
876 register struct tm * lotmp;
878 register struct tm * tmp;
879 char loab[MAX_STRING_LENGTH];
881 lotmp = my_localtime(&lot);
884 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
887 diff = (long) (hit - lot);
896 tmp = my_localtime(&t);
899 /* We do not want to capture transitions just for
900 * abbreviated zone name changes */
901 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
902 (delta(&tm, &lotm) == (t - lot) &&
903 tm.tm_isdst == lotm.tm_isdst)) {
909 showICU(fp, name, lot, hit);
913 static void showICU(FILE * fp, char *zone, time_t t1, time_t t2)
924 static void dumptimeICU(FILE * fp, time_t t)
926 static const char wday_name[][3] = {
927 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
936 loc = *my_localtime(&t);
938 trail = loc.tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
939 lead = loc.tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + trail / DIVISOR;
941 if (trail < 0 && lead > 0) {
944 } else if (lead < 0 && trail > 0) {
949 fprintf(fp, "%04d-%02d-%02d", lead * DIVISOR + trail, loc.tm_mon + 1, loc.tm_mday);
950 fprintf(fp, " %.3s ", wday_name[loc.tm_wday]);
951 fprintf(fp, "%02d:%02d:%02d", loc.tm_hour, loc.tm_min, loc.tm_sec);
954 offset = delta(&loc, &gmt);
963 offset = (offset - sec) / 60;
967 fprintf(fp, "%02d", hour);
968 fprintf(fp, "%02d", min);
969 fprintf(fp, "%02d", sec);
970 fprintf(fp, "[DST=%d]", loc.tm_isdst);
973 static int getall(struct listentry ** namelist) {
975 struct listentry dummyentry;
976 struct listentry * last = &dummyentry;
978 getzones(TZDIR, NULL, &last, &count);
980 *namelist = dummyentry.next;
986 static void getzones(char * basedir, char * relpath, struct listentry ** last, int * count) {
987 char path[FILENAME_MAX + 1];
991 strcpy(path, basedir);
992 if (relpath != NULL) {
994 strcat(path, relpath);
997 if ((dp = opendir(path)) == NULL) {
999 if (strstr(relpath, ".tab") == NULL && strcmp(relpath, "Etc/Unknown") != 0) {
1003 if ((pzonename = malloc(strlen(relpath) + 1)) == NULL) {
1006 strcpy(pzonename, relpath);
1008 if ((pentry = malloc(sizeof(listentry))) == NULL) {
1012 pentry->name = pzonename;
1013 pentry->next = NULL;
1014 (*last)->next = pentry;
1020 while ((dir = readdir(dp)) != NULL) {
1021 char subpath[FILENAME_MAX + 1];
1023 if (strcmp(dir->d_name, ".") == 0
1024 || strcmp(dir->d_name, "..") == 0) {
1027 if (relpath != NULL) {
1028 strcpy(subpath, relpath);
1029 strcat(subpath, "/");
1030 strcat(subpath, dir->d_name);
1032 strcpy(subpath, dir->d_name);
1034 getzones(basedir, subpath, last, count);