2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
7 static const char rcsid[] =
9 static char elsieid[] = "@(#)zdump.c 8.10";
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.
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 */
25 #include <float.h> /* for FLT_MAX and DBL_MAX */
26 #include <ctype.h> /* for isalpha et al. */
29 #endif /* !defined isascii */
32 #define ZDUMP_LO_YEAR (-500)
33 #endif /* !defined ZDUMP_LO_YEAR */
36 #define ZDUMP_HI_YEAR 2500
37 #endif /* !defined ZDUMP_HI_YEAR */
39 #ifndef MAX_STRING_LENGTH
40 #define MAX_STRING_LENGTH 1024
41 #endif /* !defined MAX_STRING_LENGTH */
45 #endif /* !defined TRUE */
49 #endif /* !defined FALSE */
52 #define EXIT_SUCCESS 0
53 #endif /* !defined EXIT_SUCCESS */
56 #define EXIT_FAILURE 1
57 #endif /* !defined EXIT_FAILURE */
61 #endif /* !defined SECSPERMIN */
64 #define MINSPERHOUR 60
65 #endif /* !defined MINSPERHOUR */
68 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
69 #endif /* !defined SECSPERHOUR */
72 #define HOURSPERDAY 24
73 #endif /* !defined HOURSPERDAY */
76 #define EPOCH_YEAR 1970
77 #endif /* !defined EPOCH_YEAR */
80 #define TM_YEAR_BASE 1900
81 #endif /* !defined TM_YEAR_BASE */
84 #define DAYSPERNYEAR 365
85 #endif /* !defined DAYSPERNYEAR */
88 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
89 #endif /* !defined isleap */
93 ** See tzfile.h for details on isleap_sum.
95 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
96 #endif /* !defined isleap_sum */
98 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
99 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
100 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
103 #define HAVE_GETTEXT 0
106 #include "locale.h" /* for setlocale */
108 #endif /* HAVE_GETTEXT */
113 #else /* !defined lint */
116 #endif /* defined __GNUC__ */
117 #endif /* !defined lint */
118 #endif /* !defined 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 */
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.
136 #define _(msgid) gettext(msgid)
137 #else /* !(HAVE_GETTEXT) */
138 #define _(msgid) msgid
139 #endif /* !(HAVE_GETTEXT) */
140 #endif /* !defined _ */
143 #define TZ_DOMAIN "tz"
144 #endif /* !defined TZ_DOMAIN */
146 extern char ** environ;
147 extern char * tzname[2];
149 static time_t absolute_min_time;
150 static time_t absolute_max_time;
151 static size_t longest;
152 static char * progname;
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);
167 #define my_localtime localtime
168 #else /* !defined TYPECHECK */
173 register struct tm * tmp;
176 if (tp != NULL && tmp != NULL) {
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");
201 #endif /* !defined TYPECHECK */
205 const char * const abbrp;
206 const char * const zone;
208 register const char * cp;
215 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
218 wp = _("lacks alphabetic at start");
219 else if (cp - abbrp < 3)
220 wp = _("has fewer than 3 alphabetics");
221 else if (cp - abbrp > 6)
222 wp = _("has more than 6 alphabetics");
223 if (wp == NULL && (*cp == '+' || *cp == '-')) {
225 if (isascii((unsigned char) *cp) &&
226 isdigit((unsigned char) *cp))
227 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
230 wp = _("differs from POSIX standard");
234 (void) fflush(stdout);
235 (void) fprintf(stderr,
236 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
237 progname, zone, abbrp, wp);
249 register char * cutarg;
250 register long cutloyear = ZDUMP_LO_YEAR;
251 register long cuthiyear = ZDUMP_HI_YEAR;
252 register time_t cutlotime;
253 register time_t cuthitime;
254 register char ** fakeenv;
260 register struct tm * tmp;
261 register struct tm * newtmp;
264 INITIALIZE(cutlotime);
265 INITIALIZE(cuthitime);
267 (void) setlocale(LC_MESSAGES, "");
269 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
270 #endif /* TEXTDOMAINDIR */
271 (void) textdomain(TZ_DOMAIN);
272 #endif /* HAVE_GETTEXT */
273 for (i = 1; i < argc; ++i)
274 if (strcmp(argv[i], "--version") == 0) {
275 errx(EXIT_SUCCESS, "%s", elsieid);
276 } else if (strcmp(argv[i], "--help") == 0) {
277 usage(stdout, EXIT_SUCCESS);
281 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
284 else cutarg = optarg;
286 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
287 usage(stderr, EXIT_FAILURE);
290 if (cutarg != NULL) {
295 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
297 } else if (sscanf(cutarg, "%ld,%ld%c",
298 &lo, &hi, &dummy) == 2) {
302 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
308 cutlotime = yeartot(cutloyear);
309 cuthitime = yeartot(cuthiyear);
313 for (i = optind; i < argc; ++i)
314 if (strlen(argv[i]) > longest)
315 longest = strlen(argv[i]);
320 for (i = 0; environ[i] != NULL; ++i)
322 fakeenv = (char **) malloc((size_t) ((i + 2) *
324 if (fakeenv == NULL ||
325 (fakeenv[0] = (char *) malloc((size_t) (longest +
328 _("malloc() failed"));
330 (void) strcpy(fakeenv[to++], "TZ=");
331 for (from = 0; environ[from] != NULL; ++from)
332 if (strncmp(environ[from], "TZ=", 3) != 0)
333 fakeenv[to++] = environ[from];
337 for (i = optind; i < argc; ++i) {
338 static char buf[MAX_STRING_LENGTH];
340 (void) strcpy(&fakeenv[0][3], argv[i]);
342 show(argv[i], now, FALSE);
346 t = absolute_min_time;
347 show(argv[i], t, TRUE);
348 t += SECSPERHOUR * HOURSPERDAY;
349 show(argv[i], t, TRUE);
352 tmp = my_localtime(&t);
355 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
358 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
360 newt = t + SECSPERHOUR * 12;
361 newtmp = localtime(&newt);
364 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
365 (delta(&newtm, &tm) != (newt - t) ||
366 newtm.tm_isdst != tm.tm_isdst ||
367 strcmp(abbr(&newtm), buf) != 0)) {
368 newt = hunt(argv[i], t, newt);
369 newtmp = localtime(&newt);
370 if (newtmp != NULL) {
381 t = absolute_max_time;
382 t -= SECSPERHOUR * HOURSPERDAY;
383 show(argv[i], t, TRUE);
384 t += SECSPERHOUR * HOURSPERDAY;
385 show(argv[i], t, TRUE);
387 if (fflush(stdout) || ferror(stdout))
388 errx(EXIT_FAILURE, _("error writing standard output"));
390 /* If exit fails to exit... */
391 return(EXIT_FAILURE);
397 if (0.5 == (time_t) 0.5) {
399 ** time_t is floating.
401 if (sizeof (time_t) == sizeof (float)) {
402 absolute_min_time = (time_t) -FLT_MAX;
403 absolute_max_time = (time_t) FLT_MAX;
404 } else if (sizeof (time_t) == sizeof (double)) {
405 absolute_min_time = (time_t) -DBL_MAX;
406 absolute_max_time = (time_t) DBL_MAX;
408 (void) fprintf(stderr,
409 _("%s: use of -v on system with floating time_t other than float or double\n"),
413 } else if (0 > (time_t) -1) {
415 ** time_t is signed. Assume overflow wraps around.
425 absolute_max_time = t;
427 absolute_min_time = t - 1;
428 if (t < absolute_min_time)
429 absolute_min_time = t;
432 ** time_t is unsigned.
434 absolute_min_time = 0;
435 absolute_max_time = absolute_min_time - 1;
444 register long seconds;
451 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
453 if (t > absolute_max_time - seconds) {
454 t = absolute_max_time;
460 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
461 if (t < absolute_min_time + seconds) {
462 t = absolute_min_time;
472 usage(FILE *stream, int status)
475 _("usage: %s [--version] [-v] [--help] [-c [loyear,]hiyear] zonename ...\n\
477 Report bugs to tz@elsie.nci.nih.gov.\n"), progname);
482 hunt(char *name, time_t lot, time_t hit)
487 register struct tm * lotmp;
489 register struct tm * tmp;
490 char loab[MAX_STRING_LENGTH];
492 lotmp = my_localtime(&lot);
495 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
498 diff = (long) (hit - lot);
507 tmp = my_localtime(&t);
510 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
511 (delta(&tm, &lotm) == (t - lot) &&
512 tm.tm_isdst == lotm.tm_isdst &&
513 strcmp(abbr(&tm), loab) == 0)) {
519 show(name, lot, TRUE);
520 show(name, hit, TRUE);
525 ** Thanks to Paul Eggert for logic used in delta.
533 register long result;
536 if (newp->tm_year < oldp->tm_year)
537 return -delta(oldp, newp);
539 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
540 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
541 result += newp->tm_yday - oldp->tm_yday;
542 result *= HOURSPERDAY;
543 result += newp->tm_hour - oldp->tm_hour;
544 result *= MINSPERHOUR;
545 result += newp->tm_min - oldp->tm_min;
546 result *= SECSPERMIN;
547 result += newp->tm_sec - oldp->tm_sec;
552 show(char *zone, time_t t, int v)
554 register struct tm * tmp;
556 (void) printf("%-*s ", (int) longest, zone);
560 (void) printf(tformat(), t);
563 (void) printf(" UTC");
565 (void) printf(" = ");
567 tmp = my_localtime(&t);
570 if (*abbr(tmp) != '\0')
571 (void) printf(" %s", abbr(tmp));
573 (void) printf(" isdst=%d", tmp->tm_isdst);
575 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
576 #endif /* defined TM_GMTOFF */
580 if (tmp != NULL && *abbr(tmp) != '\0')
581 abbrok(abbr(tmp), zone);
588 register char * result;
591 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
593 result = tzname[tmp->tm_isdst];
594 return (result == NULL) ? &nada : result;
598 ** The code below can fail on certain theoretical systems;
599 ** it works on all known real-world systems as of 2004-12-30.
605 if (0.5 == (time_t) 0.5) { /* floating */
606 if (sizeof (time_t) > sizeof (double))
610 if (0 > (time_t) -1) { /* signed */
611 if (sizeof (time_t) > sizeof (long))
613 if (sizeof (time_t) > sizeof (int))
617 if (sizeof (time_t) > sizeof (unsigned long))
619 if (sizeof (time_t) > sizeof (unsigned int))
626 register const struct tm * timeptr;
628 static const char wday_name[][3] = {
629 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
631 static const char mon_name[][3] = {
632 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
633 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
635 register const char * wn;
636 register const char * mn;
640 if (timeptr == NULL) {
641 (void) printf("NULL");
645 ** The packaged versions of localtime and gmtime never put out-of-range
646 ** values in tm_wday or tm_mon, but since this code might be compiled
647 ** with other (perhaps experimental) versions, paranoia is in order.
649 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
650 (int) (sizeof wday_name / sizeof wday_name[0]))
652 else wn = wday_name[timeptr->tm_wday];
653 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
654 (int) (sizeof mon_name / sizeof mon_name[0]))
656 else mn = mon_name[timeptr->tm_mon];
657 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
659 timeptr->tm_mday, timeptr->tm_hour,
660 timeptr->tm_min, timeptr->tm_sec);
662 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
663 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
666 if (trail < 0 && lead > 0) {
669 } else if (lead < 0 && trail > 0) {
674 (void) printf("%d", trail);
675 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));