1 static char elsieid[] = "@(#)zdump.c 7.74";
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.
9 #include "stdio.h" /* for stdout, stderr, perror */
10 #include "string.h" /* for strcpy */
11 #include "sys/types.h" /* for time_t */
12 #include "time.h" /* for struct tm */
13 #include "stdlib.h" /* for exit, malloc, atoi */
14 #include "float.h" /* for FLT_MAX and DBL_MAX */
15 #include "ctype.h" /* for isalpha et al. */
21 #define ZDUMP_LO_YEAR (-500)
22 #endif /* !defined ZDUMP_LO_YEAR */
25 #define ZDUMP_HI_YEAR 2500
26 #endif /* !defined ZDUMP_HI_YEAR */
28 #ifndef MAX_STRING_LENGTH
29 #define MAX_STRING_LENGTH 1024
30 #endif /* !defined MAX_STRING_LENGTH */
34 #endif /* !defined TRUE */
38 #endif /* !defined FALSE */
41 #define EXIT_SUCCESS 0
42 #endif /* !defined EXIT_SUCCESS */
45 #define EXIT_FAILURE 1
46 #endif /* !defined EXIT_FAILURE */
50 #endif /* !defined SECSPERMIN */
53 #define MINSPERHOUR 60
54 #endif /* !defined MINSPERHOUR */
57 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
58 #endif /* !defined SECSPERHOUR */
61 #define HOURSPERDAY 24
62 #endif /* !defined HOURSPERDAY */
65 #define EPOCH_YEAR 1970
66 #endif /* !defined EPOCH_YEAR */
69 #define TM_YEAR_BASE 1900
70 #endif /* !defined TM_YEAR_BASE */
73 #define DAYSPERNYEAR 365
74 #endif /* !defined DAYSPERNYEAR */
77 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
78 #endif /* !defined isleap */
82 ** See tzfile.h for details on isleap_sum.
84 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
85 #endif /* !defined isleap_sum */
87 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
88 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
89 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
92 #include "locale.h" /* for setlocale */
94 #endif /* HAVE_GETTEXT */
99 #else /* !defined lint */
102 #endif /* defined __GNUC__ */
103 #endif /* !defined lint */
104 #endif /* !defined GNUC_or_lint */
108 #define INITIALIZE(x) ((x) = 0)
109 #else /* !defined GNUC_or_lint */
110 #define INITIALIZE(x)
111 #endif /* !defined GNUC_or_lint */
112 #endif /* !defined INITIALIZE */
115 ** For the benefit of GNU folk...
116 ** `_(MSGID)' uses the current locale's message library string for MSGID.
117 ** The default is to use gettext if available, and use MSGID otherwise.
122 #define _(msgid) gettext(msgid)
123 #else /* !HAVE_GETTEXT */
124 #define _(msgid) msgid
125 #endif /* !HAVE_GETTEXT */
126 #endif /* !defined _ */
129 #define TZ_DOMAIN "tz"
130 #endif /* !defined TZ_DOMAIN */
135 #else /* !defined __STDC__ */
137 #endif /* !defined __STDC__ */
138 #endif /* !defined P */
140 extern char ** environ;
141 extern int getopt P((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 P((struct tm * tmp));
154 static void abbrok P((const char * abbrp, const char * zone));
155 static long delta P((struct tm * newp, struct tm * oldp));
156 static void dumptime P((const struct tm * tmp));
157 static time_t hunt P((char * name, time_t lot, time_t hit));
158 static void setabsolutes P((void));
159 static void show P((char * zone, time_t t, int v));
160 static const char * tformat P((void));
161 static time_t yeartot P((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);
246 register char * cutarg;
247 register long cutloyear = ZDUMP_LO_YEAR;
248 register long cuthiyear = ZDUMP_HI_YEAR;
249 register time_t cutlotime;
250 register time_t cuthitime;
251 register char ** fakeenv;
257 register struct tm * tmp;
258 register struct tm * newtmp;
260 INITIALIZE(cutlotime);
261 INITIALIZE(cuthitime);
263 (void) setlocale(LC_ALL, "");
265 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
266 #endif /* defined TEXTDOMAINDIR */
267 (void) textdomain(TZ_DOMAIN);
268 #endif /* HAVE_GETTEXT */
270 for (i = 1; i < argc; ++i)
271 if (strcmp(argv[i], "--version") == 0) {
272 (void) printf("%s\n", elsieid);
277 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
280 else cutarg = optarg;
281 if ((c != EOF && c != -1) ||
282 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
283 (void) fprintf(stderr,
284 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
289 if (cutarg != NULL) {
294 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
296 } else if (sscanf(cutarg, "%ld,%ld%c",
297 &lo, &hi, &dummy) == 2) {
301 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
307 cutlotime = yeartot(cutloyear);
308 cuthitime = yeartot(cuthiyear);
312 for (i = optind; i < argc; ++i)
313 if (strlen(argv[i]) > longest)
314 longest = strlen(argv[i]);
319 for (i = 0; environ[i] != NULL; ++i)
321 fakeenv = (char **) malloc((size_t) ((i + 2) *
323 if (fakeenv == NULL ||
324 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
325 (void) perror(progname);
329 (void) strcpy(fakeenv[to++], "TZ=");
330 for (from = 0; environ[from] != NULL; ++from)
331 if (strncmp(environ[from], "TZ=", 3) != 0)
332 fakeenv[to++] = environ[from];
336 for (i = optind; i < argc; ++i) {
337 static char buf[MAX_STRING_LENGTH];
339 (void) strcpy(&fakeenv[0][3], argv[i]);
341 show(argv[i], now, FALSE);
345 t = absolute_min_time;
346 show(argv[i], t, TRUE);
347 t += SECSPERHOUR * HOURSPERDAY;
348 show(argv[i], t, TRUE);
351 tmp = my_localtime(&t);
354 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
359 newt = t + SECSPERHOUR * 12;
360 if (newt >= cuthitime)
364 newtmp = localtime(&newt);
367 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
368 (delta(&newtm, &tm) != (newt - t) ||
369 newtm.tm_isdst != tm.tm_isdst ||
370 strcmp(abbr(&newtm), buf) != 0)) {
371 newt = hunt(argv[i], t, newt);
372 newtmp = localtime(&newt);
373 if (newtmp != NULL) {
384 t = absolute_max_time;
385 t -= SECSPERHOUR * HOURSPERDAY;
386 show(argv[i], t, TRUE);
387 t += SECSPERHOUR * HOURSPERDAY;
388 show(argv[i], t, TRUE);
390 if (fflush(stdout) || ferror(stdout)) {
391 (void) fprintf(stderr, "%s: ", progname);
392 (void) perror(_("Error writing standard output"));
396 /* If exit fails to exit... */
403 if (0.5 == (time_t) 0.5) {
405 ** time_t is floating.
407 if (sizeof (time_t) == sizeof (float)) {
408 absolute_min_time = (time_t) -FLT_MAX;
409 absolute_max_time = (time_t) FLT_MAX;
410 } else if (sizeof (time_t) == sizeof (double)) {
411 absolute_min_time = (time_t) -DBL_MAX;
412 absolute_max_time = (time_t) DBL_MAX;
414 (void) fprintf(stderr,
415 _("%s: use of -v on system with floating time_t other than float or double\n"),
419 } else if (0 > (time_t) -1) {
423 register time_t hibit;
425 for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
427 absolute_min_time = hibit;
428 absolute_max_time = -(hibit + 1);
431 ** time_t is unsigned.
433 absolute_min_time = 0;
434 absolute_max_time = absolute_min_time - 1;
443 register long seconds;
450 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
452 if (t > absolute_max_time - seconds) {
453 t = absolute_max_time;
459 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
460 if (t < absolute_min_time + seconds) {
461 t = absolute_min_time;
479 register struct tm * lotmp;
481 register struct tm * tmp;
482 char loab[MAX_STRING_LENGTH];
484 lotmp = my_localtime(&lot);
487 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
490 diff = (long) (hit - lot);
499 tmp = my_localtime(&t);
502 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
503 (delta(&tm, &lotm) == (t - lot) &&
504 tm.tm_isdst == lotm.tm_isdst &&
505 strcmp(abbr(&tm), loab) == 0)) {
511 show(name, lot, TRUE);
512 show(name, hit, TRUE);
517 ** Thanks to Paul Eggert for logic used in delta.
525 register long result;
528 if (newp->tm_year < oldp->tm_year)
529 return -delta(oldp, newp);
531 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
532 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
533 result += newp->tm_yday - oldp->tm_yday;
534 result *= HOURSPERDAY;
535 result += newp->tm_hour - oldp->tm_hour;
536 result *= MINSPERHOUR;
537 result += newp->tm_min - oldp->tm_min;
538 result *= SECSPERMIN;
539 result += newp->tm_sec - oldp->tm_sec;
549 register struct tm * tmp;
551 (void) printf("%-*s ", (int) longest, zone);
555 (void) printf(tformat(), t);
558 (void) printf(" UTC");
560 (void) printf(" = ");
562 tmp = my_localtime(&t);
565 if (*abbr(tmp) != '\0')
566 (void) printf(" %s", abbr(tmp));
568 (void) printf(" isdst=%d", tmp->tm_isdst);
570 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
571 #endif /* defined TM_GMTOFF */
575 if (tmp != NULL && *abbr(tmp) != '\0')
576 abbrok(abbr(tmp), zone);
583 register char * result;
586 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
588 result = tzname[tmp->tm_isdst];
589 return (result == NULL) ? &nada : result;
593 ** The code below can fail on certain theoretical systems;
594 ** it works on all known real-world systems as of 2004-12-30.
600 if (0.5 == (time_t) 0.5) { /* floating */
601 if (sizeof (time_t) > sizeof (double))
605 if (0 > (time_t) -1) { /* signed */
606 if (sizeof (time_t) > sizeof (long))
608 if (sizeof (time_t) > sizeof (int))
612 if (sizeof (time_t) > sizeof (unsigned long))
614 if (sizeof (time_t) > sizeof (unsigned int))
621 register const struct tm * timeptr;
623 static const char wday_name[][3] = {
624 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
626 static const char mon_name[][3] = {
627 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
628 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
630 register const char * wn;
631 register const char * mn;
635 if (timeptr == NULL) {
636 (void) printf("NULL");
640 ** The packaged versions of localtime and gmtime never put out-of-range
641 ** values in tm_wday or tm_mon, but since this code might be compiled
642 ** with other (perhaps experimental) versions, paranoia is in order.
644 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
645 (int) (sizeof wday_name / sizeof wday_name[0]))
647 else wn = wday_name[timeptr->tm_wday];
648 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
649 (int) (sizeof mon_name / sizeof mon_name[0]))
651 else mn = mon_name[timeptr->tm_mon];
652 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
654 timeptr->tm_mday, timeptr->tm_hour,
655 timeptr->tm_min, timeptr->tm_sec);
657 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
658 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
661 if (trail < 0 && lead > 0) {
664 } else if (lead < 0 && trail > 0) {
669 (void) printf("%d", trail);
670 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));