2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
6 static char elsieid[] = "@(#)zdump.c 8.10";
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.
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. */
23 #endif /* !defined isascii */
26 #define ZDUMP_LO_YEAR (-500)
27 #endif /* !defined ZDUMP_LO_YEAR */
30 #define ZDUMP_HI_YEAR 2500
31 #endif /* !defined ZDUMP_HI_YEAR */
33 #ifndef MAX_STRING_LENGTH
34 #define MAX_STRING_LENGTH 1024
35 #endif /* !defined MAX_STRING_LENGTH */
39 #endif /* !defined TRUE */
43 #endif /* !defined FALSE */
46 #define EXIT_SUCCESS 0
47 #endif /* !defined EXIT_SUCCESS */
50 #define EXIT_FAILURE 1
51 #endif /* !defined EXIT_FAILURE */
55 #endif /* !defined SECSPERMIN */
58 #define MINSPERHOUR 60
59 #endif /* !defined MINSPERHOUR */
62 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
63 #endif /* !defined SECSPERHOUR */
66 #define HOURSPERDAY 24
67 #endif /* !defined HOURSPERDAY */
70 #define EPOCH_YEAR 1970
71 #endif /* !defined EPOCH_YEAR */
74 #define TM_YEAR_BASE 1900
75 #endif /* !defined TM_YEAR_BASE */
78 #define DAYSPERNYEAR 365
79 #endif /* !defined DAYSPERNYEAR */
82 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
83 #endif /* !defined isleap */
87 ** See tzfile.h for details on isleap_sum.
89 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
90 #endif /* !defined isleap_sum */
92 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
93 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
94 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
97 #define HAVE_GETTEXT 0
100 #include "locale.h" /* for setlocale */
102 #endif /* HAVE_GETTEXT */
107 #else /* !defined lint */
110 #endif /* defined __GNUC__ */
111 #endif /* !defined lint */
112 #endif /* !defined 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 */
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.
130 #define _(msgid) gettext(msgid)
131 #else /* !HAVE_GETTEXT */
132 #define _(msgid) msgid
133 #endif /* !HAVE_GETTEXT */
134 #endif /* !defined _ */
137 #define TZ_DOMAIN "tz"
138 #endif /* !defined TZ_DOMAIN */
140 extern char ** environ;
141 extern int getopt(int argc, char * const argv[],
142 const char * options);
143 extern char * optarg;
145 extern char * tzname[2];
147 static time_t absolute_min_time;
148 static time_t absolute_max_time;
149 static size_t longest;
150 static char * progname;
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);
164 #define my_localtime localtime
165 #else /* !defined TYPECHECK */
170 register struct tm * tmp;
173 if (tp != NULL && tmp != NULL) {
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");
198 #endif /* !defined TYPECHECK */
202 const char * const abbrp;
203 const char * const zone;
205 register const char * cp;
212 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
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 == '-')) {
222 if (isascii((unsigned char) *cp) &&
223 isdigit((unsigned char) *cp))
224 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
227 wp = _("differs from POSIX standard");
231 (void) fflush(stdout);
232 (void) fprintf(stderr,
233 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
234 progname, zone, abbrp, wp);
239 usage(stream, status)
243 (void) fprintf(stream,
244 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
246 Report bugs to tz@elsie.nci.nih.gov.\n"),
259 register char * cutarg;
260 register long cutloyear = ZDUMP_LO_YEAR;
261 register long cuthiyear = ZDUMP_HI_YEAR;
262 register time_t cutlotime;
263 register time_t cuthitime;
264 register char ** fakeenv;
270 register struct tm * tmp;
271 register struct tm * newtmp;
273 INITIALIZE(cutlotime);
274 INITIALIZE(cuthitime);
276 (void) setlocale(LC_ALL, "");
278 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
279 #endif /* defined TEXTDOMAINDIR */
280 (void) textdomain(TZ_DOMAIN);
281 #endif /* HAVE_GETTEXT */
283 for (i = 1; i < argc; ++i)
284 if (strcmp(argv[i], "--version") == 0) {
285 (void) printf("%s\n", elsieid);
287 } else if (strcmp(argv[i], "--help") == 0) {
288 usage(stdout, EXIT_SUCCESS);
292 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
295 else cutarg = optarg;
296 if ((c != EOF && c != -1) ||
297 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
298 usage(stderr, EXIT_FAILURE);
301 if (cutarg != NULL) {
306 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
308 } else if (sscanf(cutarg, "%ld,%ld%c",
309 &lo, &hi, &dummy) == 2) {
313 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
319 cutlotime = yeartot(cutloyear);
320 cuthitime = yeartot(cuthiyear);
324 for (i = optind; i < argc; ++i)
325 if (strlen(argv[i]) > longest)
326 longest = strlen(argv[i]);
331 for (i = 0; environ[i] != NULL; ++i)
333 fakeenv = (char **) malloc((size_t) ((i + 2) *
335 if (fakeenv == NULL ||
336 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
337 (void) perror(progname);
341 (void) strcpy(fakeenv[to++], "TZ=");
342 for (from = 0; environ[from] != NULL; ++from)
343 if (strncmp(environ[from], "TZ=", 3) != 0)
344 fakeenv[to++] = environ[from];
348 for (i = optind; i < argc; ++i) {
349 static char buf[MAX_STRING_LENGTH];
351 (void) strcpy(&fakeenv[0][3], argv[i]);
353 show(argv[i], now, FALSE);
357 t = absolute_min_time;
358 show(argv[i], t, TRUE);
359 t += SECSPERHOUR * HOURSPERDAY;
360 show(argv[i], t, TRUE);
363 tmp = my_localtime(&t);
366 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
369 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
371 newt = t + SECSPERHOUR * 12;
372 newtmp = localtime(&newt);
375 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
376 (delta(&newtm, &tm) != (newt - t) ||
377 newtm.tm_isdst != tm.tm_isdst ||
378 strcmp(abbr(&newtm), buf) != 0)) {
379 newt = hunt(argv[i], t, newt);
380 newtmp = localtime(&newt);
381 if (newtmp != NULL) {
392 t = absolute_max_time;
393 t -= SECSPERHOUR * HOURSPERDAY;
394 show(argv[i], t, TRUE);
395 t += SECSPERHOUR * HOURSPERDAY;
396 show(argv[i], t, TRUE);
398 if (fflush(stdout) || ferror(stdout)) {
399 (void) fprintf(stderr, "%s: ", progname);
400 (void) perror(_("Error writing to standard output"));
404 /* If exit fails to exit... */
411 if (0.5 == (time_t) 0.5) {
413 ** time_t is floating.
415 if (sizeof (time_t) == sizeof (float)) {
416 absolute_min_time = (time_t) -FLT_MAX;
417 absolute_max_time = (time_t) FLT_MAX;
418 } else if (sizeof (time_t) == sizeof (double)) {
419 absolute_min_time = (time_t) -DBL_MAX;
420 absolute_max_time = (time_t) DBL_MAX;
422 (void) fprintf(stderr,
423 _("%s: use of -v on system with floating time_t other than float or double\n"),
427 } else if (0 > (time_t) -1) {
429 ** time_t is signed. Assume overflow wraps around.
439 absolute_max_time = t;
441 absolute_min_time = t - 1;
442 if (t < absolute_min_time)
443 absolute_min_time = t;
446 ** time_t is unsigned.
448 absolute_min_time = 0;
449 absolute_max_time = absolute_min_time - 1;
458 register long seconds;
465 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
467 if (t > absolute_max_time - seconds) {
468 t = absolute_max_time;
474 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
475 if (t < absolute_min_time + seconds) {
476 t = absolute_min_time;
486 hunt(char *name, time_t lot, time_t hit)
491 register struct tm * lotmp;
493 register struct tm * tmp;
494 char loab[MAX_STRING_LENGTH];
496 lotmp = my_localtime(&lot);
499 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
502 diff = (long) (hit - lot);
511 tmp = my_localtime(&t);
514 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
515 (delta(&tm, &lotm) == (t - lot) &&
516 tm.tm_isdst == lotm.tm_isdst &&
517 strcmp(abbr(&tm), loab) == 0)) {
523 show(name, lot, TRUE);
524 show(name, hit, TRUE);
529 ** Thanks to Paul Eggert for logic used in delta.
537 register long result;
540 if (newp->tm_year < oldp->tm_year)
541 return -delta(oldp, newp);
543 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
544 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
545 result += newp->tm_yday - oldp->tm_yday;
546 result *= HOURSPERDAY;
547 result += newp->tm_hour - oldp->tm_hour;
548 result *= MINSPERHOUR;
549 result += newp->tm_min - oldp->tm_min;
550 result *= SECSPERMIN;
551 result += newp->tm_sec - oldp->tm_sec;
556 show(char *zone, time_t t, int v)
558 register struct tm * tmp;
560 (void) printf("%-*s ", (int) longest, zone);
564 (void) printf(tformat(), t);
567 (void) printf(" UTC");
569 (void) printf(" = ");
571 tmp = my_localtime(&t);
574 if (*abbr(tmp) != '\0')
575 (void) printf(" %s", abbr(tmp));
577 (void) printf(" isdst=%d", tmp->tm_isdst);
579 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
580 #endif /* defined TM_GMTOFF */
584 if (tmp != NULL && *abbr(tmp) != '\0')
585 abbrok(abbr(tmp), zone);
592 register char * result;
595 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
597 result = tzname[tmp->tm_isdst];
598 return (result == NULL) ? &nada : result;
602 ** The code below can fail on certain theoretical systems;
603 ** it works on all known real-world systems as of 2004-12-30.
609 if (0.5 == (time_t) 0.5) { /* floating */
610 if (sizeof (time_t) > sizeof (double))
614 if (0 > (time_t) -1) { /* signed */
615 if (sizeof (time_t) > sizeof (long))
617 if (sizeof (time_t) > sizeof (int))
621 if (sizeof (time_t) > sizeof (unsigned long))
623 if (sizeof (time_t) > sizeof (unsigned int))
630 register const struct tm * timeptr;
632 static const char wday_name[][3] = {
633 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
635 static const char mon_name[][3] = {
636 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
637 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
639 register const char * wn;
640 register const char * mn;
644 if (timeptr == NULL) {
645 (void) printf("NULL");
649 ** The packaged versions of localtime and gmtime never put out-of-range
650 ** values in tm_wday or tm_mon, but since this code might be compiled
651 ** with other (perhaps experimental) versions, paranoia is in order.
653 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
654 (int) (sizeof wday_name / sizeof wday_name[0]))
656 else wn = wday_name[timeptr->tm_wday];
657 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
658 (int) (sizeof mon_name / sizeof mon_name[0]))
660 else mn = mon_name[timeptr->tm_mon];
661 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
663 timeptr->tm_mday, timeptr->tm_hour,
664 timeptr->tm_min, timeptr->tm_sec);
666 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
667 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
670 if (trail < 0 && lead > 0) {
673 } else if (lead < 0 && trail > 0) {
678 (void) printf("%d", trail);
679 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));