2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "@(#)zdump.c 1.15 06/12/06 SMI"
10 * Taken from elsie.nci.nih.gov to replace the existing Solaris zdump,
11 * which was based on an earlier version of the elsie code.
13 * For zdump 7.24, the following changes were made to the elsie code:
14 * locale/textdomain/messages to match existing Solaris style.
15 * Solaris verbose mode is documented to display the current time first.
16 * cstyle cleaned code.
17 * removed old locale/textdomain code.
20 static char elsieid[] = "@(#)zdump.c 7.74";
23 * This code has been made independent of the rest of the time
24 * conversion package to increase confidence in the verification it provides.
25 * You can use this code to help in verifying other implementations.
28 #include "stdio.h" /* for stdout, stderr, perror */
29 #include "string.h" /* for strcpy */
30 #include "sys/types.h" /* for time_t */
31 #include "time.h" /* for struct tm */
32 #include "stdlib.h" /* for exit, malloc, atoi */
33 #include "locale.h" /* for setlocale, textdomain */
36 #include "tzfile.h" /* for defines */
40 #define ZDUMP_LO_YEAR (-500)
41 #endif /* !defined ZDUMP_LO_YEAR */
44 #define ZDUMP_HI_YEAR 2500
45 #endif /* !defined ZDUMP_HI_YEAR */
47 #ifndef MAX_STRING_LENGTH
48 #define MAX_STRING_LENGTH 1024
49 #endif /* !defined MAX_STRING_LENGTH */
53 #endif /* !defined TRUE */
57 #endif /* !defined FALSE */
61 * See tzfile.h for details on isleap_sum.
63 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
64 #endif /* !defined isleap_sum */
67 #define SECSPERDAY ((long)SECSPERHOUR * HOURSPERDAY)
69 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
70 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
75 #else /* !defined lint */
78 #endif /* defined __GNUC__ */
79 #endif /* !defined lint */
80 #endif /* !defined GNUC_or_lint */
84 #define INITIALIZE(x) ((x) = 0)
85 #else /* !defined GNUC_or_lint */
87 #endif /* !defined GNUC_or_lint */
88 #endif /* !defined INITIALIZE */
90 static time_t absolute_min_time;
91 static time_t absolute_max_time;
92 static size_t longest;
93 static char *progname;
96 static char *abbr(struct tm *);
97 static void abbrok(const char *, const char *);
98 static long delta(struct tm *, struct tm *);
99 static void dumptime(const struct tm *);
100 static time_t hunt(char *, time_t, time_t);
101 static void setabsolutes(void);
102 static void show(char *, time_t, int);
103 static void usage(void);
104 static const char *tformat(void);
105 static time_t yeartot(long y);
108 #define my_localtime localtime
109 #else /* !defined TYPECHECK */
114 register struct tm *tmp;
117 if (tp != NULL && tmp != NULL) {
123 if (t - *tp >= 1 || *tp - t >= 1) {
124 (void) fflush(stdout);
125 (void) fprintf(stderr, "\n%s: ", progname);
126 (void) fprintf(stderr, tformat(), *tp);
127 (void) fprintf(stderr, " ->");
128 (void) fprintf(stderr, " year=%d", tmp->tm_year);
129 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
130 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
131 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
132 (void) fprintf(stderr, " min=%d", tmp->tm_min);
133 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
134 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
135 (void) fprintf(stderr, " -> ");
136 (void) fprintf(stderr, tformat(), t);
137 (void) fprintf(stderr, "\n");
142 #endif /* !defined TYPECHECK */
146 const char * const abbrp;
147 const char * const zone;
149 register const char *cp;
155 while (isascii(*cp) && isalpha((unsigned char)*cp))
157 (void) fflush(stdout);
158 if (cp - abbrp == 0) {
161 * The first %s prints for the program name (zdump),
162 * the second %s prints the timezone name, the third
163 * %s prints the timezone abbreviation (tzname[0] or
166 (void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
167 "abbreviation \"%s\" lacks alphabetic at start\n"),
168 progname, zone, abbrp);
170 } else if (cp - abbrp < 3) {
171 (void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
172 "abbreviation \"%s\" has fewer than 3 alphabetics\n"),
173 progname, zone, abbrp);
175 } else if (cp - abbrp > 6) {
176 (void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
177 "abbreviation \"%s\" has more than 6 alphabetics\n"),
178 progname, zone, abbrp);
181 if (error == 0 && (*cp == '+' || *cp == '-')) {
183 if (isascii(*cp) && isdigit((unsigned char)*cp))
184 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
187 (void) fprintf(stderr, gettext("%s: warning: "
188 "zone \"%s\" abbreviation \"%s\" differs from "
189 "POSIX standard\n"), progname, zone, abbrp);
205 register char *cutarg;
206 register long cutloyear = ZDUMP_LO_YEAR;
207 register long cuthiyear = ZDUMP_HI_YEAR;
208 register time_t cutlotime;
209 register time_t cuthitime;
215 register struct tm *tmp;
216 register struct tm *newtmp;
218 INITIALIZE(cutlotime);
219 INITIALIZE(cuthitime);
221 (void) setlocale(LC_ALL, "");
222 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
223 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
225 (void) textdomain(TEXT_DOMAIN);
228 for (i = 1; i < argc; ++i)
229 if (strcmp(argv[i], "--version") == 0) {
230 (void) printf("%s\n", elsieid);
235 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
238 else cutarg = optarg;
240 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
245 if (cutarg != NULL) {
250 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
252 } else if (sscanf(cutarg, "%ld,%ld%c",
253 &lo, &hi, &dummy) == 2) {
257 (void) fprintf(stderr, gettext("%s: wild -c argument %s\n"),
263 cutlotime = yeartot(cutloyear);
264 cuthitime = yeartot(cuthiyear);
268 for (i = optind; i < argc; ++i)
269 if (strlen(argv[i]) > longest)
270 longest = strlen(argv[i]);
272 for (i = optind; i < argc; ++i) {
273 static char buf[MAX_STRING_LENGTH];
274 static char *tzp = NULL;
276 (void) unsetenv("TZ");
279 if ((tzp = malloc(3 + strlen(argv[i]) + 1)) == NULL) {
283 (void) strcpy(tzp, "TZ=");
284 (void) strcat(tzp, argv[i]);
285 if (putenv(tzp) != 0) {
290 show(argv[i], now, FALSE);
296 * We show the current time first, probably because we froze
297 * the behavior of zdump some time ago and then it got
300 show(argv[i], now, TRUE);
303 t = absolute_min_time;
304 show(argv[i], t, TRUE);
305 t += SECSPERHOUR * HOURSPERDAY;
306 show(argv[i], t, TRUE);
309 tmp = my_localtime(&t);
312 (void) strncpy(buf, abbr(&tm), sizeof (buf) - 1);
317 /* check if newt will overrun maximum time_t value */
318 if (t > LONG_MAX - (SECSPERHOUR * 12))
320 newt = t + SECSPERHOUR * 12;
321 if (newt >= cuthitime)
323 newtmp = localtime(&newt);
326 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
327 (delta(&newtm, &tm) != (newt - t) ||
328 newtm.tm_isdst != tm.tm_isdst ||
329 strcmp(abbr(&newtm), buf) != 0)) {
330 newt = hunt(argv[i], t, newt);
331 newtmp = localtime(&newt);
332 if (newtmp != NULL) {
343 t = absolute_max_time;
345 show(argv[i], t, TRUE);
346 t -= SECSPERHOUR * HOURSPERDAY;
347 show(argv[i], t, TRUE);
348 #else /* !defined(sun) */
349 t -= SECSPERHOUR * HOURSPERDAY;
350 show(argv[i], t, TRUE);
351 t += SECSPERHOUR * HOURSPERDAY;
352 show(argv[i], t, TRUE);
353 #endif /* !defined(sun) */
355 if (fflush(stdout) || ferror(stdout)) {
356 (void) fprintf(stderr, "%s: ", progname);
357 (void) perror(gettext("Error writing standard output"));
360 return (EXIT_SUCCESS);
367 absolute_min_time = LONG_MIN;
368 absolute_max_time = LONG_MAX;
370 if (0.5 == (time_t)0.5) {
372 * time_t is floating.
374 if (sizeof (time_t) == sizeof (float)) {
375 absolute_min_time = (time_t)-FLT_MAX;
376 absolute_max_time = (time_t)FLT_MAX;
377 } else if (sizeof (time_t) == sizeof (double)) {
378 absolute_min_time = (time_t)-DBL_MAX;
379 absolute_max_time = (time_t)DBL_MAX;
381 (void) fprintf(stderr, gettext("%s: use of -v on "
382 "system with floating time_t other than float "
383 "or double\n"), progname);
387 /*CONSTANTCONDITION*/
388 if (0 > (time_t)-1) {
392 register time_t hibit;
394 for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
396 absolute_min_time = hibit;
397 absolute_max_time = -(hibit + 1);
400 * time_t is unsigned.
402 absolute_min_time = 0;
403 absolute_max_time = absolute_min_time - 1;
413 register long seconds;
420 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
422 if (t > absolute_max_time - seconds) {
423 t = absolute_max_time;
429 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
430 if (t < absolute_min_time + seconds) {
431 t = absolute_min_time;
449 register struct tm *lotmp;
451 register struct tm *tmp;
452 char loab[MAX_STRING_LENGTH];
454 lotmp = my_localtime(&lot);
457 (void) strncpy(loab, abbr(&lotm), sizeof (loab) - 1);
460 diff = (long)(hit - lot);
469 tmp = my_localtime(&t);
472 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
473 (delta(&tm, &lotm) == (t - lot) &&
474 tm.tm_isdst == lotm.tm_isdst &&
475 strcmp(abbr(&tm), loab) == 0)) {
481 show(name, lot, TRUE);
482 show(name, hit, TRUE);
487 * Thanks to Paul Eggert for logic used in delta.
495 register long result;
498 if (newp->tm_year < oldp->tm_year)
499 return (-delta(oldp, newp));
501 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
502 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
503 result += newp->tm_yday - oldp->tm_yday;
504 result *= HOURSPERDAY;
505 result += newp->tm_hour - oldp->tm_hour;
506 result *= MINSPERHOUR;
507 result += newp->tm_min - oldp->tm_min;
508 result *= SECSPERMIN;
509 result += newp->tm_sec - oldp->tm_sec;
519 register struct tm *tmp;
521 (void) printf("%-*s ", (int)longest, zone);
525 (void) printf(tformat(), t);
528 (void) printf(" UTC");
530 (void) printf(" = ");
532 tmp = my_localtime(&t);
535 if (*abbr(tmp) != '\0')
536 (void) printf(" %s", abbr(tmp));
538 (void) printf(" isdst=%d", tmp->tm_isdst);
540 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
541 #endif /* defined TM_GMTOFF */
545 if (tmp != NULL && *abbr(tmp) != '\0')
546 abbrok(abbr(tmp), zone);
553 register char *result;
556 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
558 result = tzname[tmp->tm_isdst];
559 return ((result == NULL) ? &nada : result);
563 * The code below can fail on certain theoretical systems;
564 * it works on all known real-world systems as of 2004-12-30.
571 /* time_t is signed long */
574 /*CONSTANTCONDITION*/
575 if (0.5 == (time_t)0.5) { /* floating */
576 /*CONSTANTCONDITION*/
577 if (sizeof (time_t) > sizeof (double))
581 /*CONSTANTCONDITION*/
582 if (0 > (time_t)-1) { /* signed */
583 /*CONSTANTCONDITION*/
584 if (sizeof (time_t) > sizeof (long))
586 /*CONSTANTCONDITION*/
587 if (sizeof (time_t) > sizeof (int))
591 /*CONSTANTCONDITION*/
592 if (sizeof (time_t) > sizeof (unsigned long))
594 /*CONSTANTCONDITION*/
595 if (sizeof (time_t) > sizeof (unsigned int))
603 register const struct tm *timeptr;
605 static const char wday_name[][3] = {
606 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
608 static const char mon_name[][3] = {
609 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
610 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
612 register const char *wn;
613 register const char *mn;
617 if (timeptr == NULL) {
618 (void) printf("NULL");
622 * The packaged versions of localtime and gmtime never put out-of-range
623 * values in tm_wday or tm_mon, but since this code might be compiled
624 * with other (perhaps experimental) versions, paranoia is in order.
626 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
627 (int)(sizeof (wday_name) / sizeof (wday_name[0])))
629 else wn = wday_name[timeptr->tm_wday];
630 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
631 (int)(sizeof (mon_name) / sizeof (mon_name[0])))
633 else mn = mon_name[timeptr->tm_mon];
634 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
636 timeptr->tm_mday, timeptr->tm_hour,
637 timeptr->tm_min, timeptr->tm_sec);
639 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
640 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
643 if (trail < 0 && lead > 0) {
646 } else if (lead < 0 && trail > 0) {
651 (void) printf("%d", trail);
653 (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
659 (void) fprintf(stderr, gettext(
660 "%s: [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),