2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
8 * Taken from elsie.nci.nih.gov to replace the existing Solaris zdump,
9 * which was based on an earlier version of the elsie code.
11 * For zdump 7.24, the following changes were made to the elsie code:
12 * locale/textdomain/messages to match existing Solaris style.
13 * Solaris verbose mode is documented to display the current time first.
14 * cstyle cleaned code.
15 * removed old locale/textdomain code.
18 static char elsieid
[] = "@(#)zdump.c 7.74";
21 * This code has been made independent of the rest of the time
22 * conversion package to increase confidence in the verification it provides.
23 * You can use this code to help in verifying other implementations.
26 #include "stdio.h" /* for stdout, stderr, perror */
27 #include "string.h" /* for strcpy */
28 #include "sys/types.h" /* for time_t */
29 #include "time.h" /* for struct tm */
30 #include "stdlib.h" /* for exit, malloc, atoi */
31 #include "locale.h" /* for setlocale, textdomain */
34 #include "tzfile.h" /* for defines */
38 #define ZDUMP_LO_YEAR (-500)
39 #endif /* !defined ZDUMP_LO_YEAR */
42 #define ZDUMP_HI_YEAR 2500
43 #endif /* !defined ZDUMP_HI_YEAR */
45 #ifndef MAX_STRING_LENGTH
46 #define MAX_STRING_LENGTH 1024
47 #endif /* !defined MAX_STRING_LENGTH */
51 #endif /* !defined TRUE */
55 #endif /* !defined FALSE */
59 * See tzfile.h for details on isleap_sum.
61 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
62 #endif /* !defined isleap_sum */
65 #define SECSPERDAY ((long)SECSPERHOUR * HOURSPERDAY)
67 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
68 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
73 #else /* !defined lint */
76 #endif /* defined __GNUC__ */
77 #endif /* !defined lint */
78 #endif /* !defined GNUC_or_lint */
82 #define INITIALIZE(x) ((x) = 0)
83 #else /* !defined GNUC_or_lint */
85 #endif /* !defined GNUC_or_lint */
86 #endif /* !defined INITIALIZE */
88 static time_t absolute_min_time
;
89 static time_t absolute_max_time
;
90 static size_t longest
;
91 static char *progname
;
94 static char *abbr(struct tm
*);
95 static void abbrok(const char *, const char *);
96 static long delta(struct tm
*, struct tm
*);
97 static void dumptime(const struct tm
*);
98 static time_t hunt(char *, time_t, time_t);
99 static void setabsolutes(void);
100 static void show(char *, time_t, int);
101 static void usage(void);
102 static const char *tformat(void);
103 static time_t yeartot(long y
);
106 #define my_localtime localtime
107 #else /* !defined TYPECHECK */
112 register struct tm
*tmp
;
115 if (tp
!= NULL
&& tmp
!= NULL
) {
121 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
122 (void) fflush(stdout
);
123 (void) fprintf(stderr
, "\n%s: ", progname
);
124 (void) fprintf(stderr
, tformat(), *tp
);
125 (void) fprintf(stderr
, " ->");
126 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
127 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
128 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
129 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
130 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
131 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
132 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
133 (void) fprintf(stderr
, " -> ");
134 (void) fprintf(stderr
, tformat(), t
);
135 (void) fprintf(stderr
, "\n");
140 #endif /* !defined TYPECHECK */
143 abbrok(const char * const abbrp
, const char * const zone
)
145 register const char *cp
;
151 while (isalpha(*cp
) || isdigit(*cp
) || *cp
== '-' || *cp
== '+')
153 (void) fflush(stdout
);
154 if (cp
- abbrp
< 3) {
155 (void) fprintf(stderr
, gettext("%s: warning: zone \"%s\" "
156 "abbreviation \"%s\" has fewer than 3 alphabetics\n"),
157 progname
, zone
, abbrp
);
159 } else if (cp
- abbrp
> 6) {
160 (void) fprintf(stderr
, gettext("%s: warning: zone \"%s\" "
161 "abbreviation \"%s\" has more than 6 characters\n"),
162 progname
, zone
, abbrp
);
164 } else if (*cp
!= '\0') {
165 (void) fprintf(stderr
, gettext("%s: warning: zone \"%s\" "
166 "abbreviation \"%s\" has characters other than "
167 "alphanumerics\n"), progname
, zone
, abbrp
);
182 register char *cutarg
;
183 register long cutloyear
= ZDUMP_LO_YEAR
;
184 register long cuthiyear
= ZDUMP_HI_YEAR
;
185 register time_t cutlotime
;
186 register time_t cuthitime
;
192 register struct tm
*tmp
;
193 register struct tm
*newtmp
;
195 INITIALIZE(cutlotime
);
196 INITIALIZE(cuthitime
);
198 (void) setlocale(LC_ALL
, "");
199 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
200 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
202 (void) textdomain(TEXT_DOMAIN
);
205 for (i
= 1; i
< argc
; ++i
)
206 if (strcmp(argv
[i
], "--version") == 0) {
207 (void) printf("%s\n", elsieid
);
212 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
215 else cutarg
= optarg
;
217 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
222 if (cutarg
!= NULL
) {
227 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
229 } else if (sscanf(cutarg
, "%ld,%ld%c",
230 &lo
, &hi
, &dummy
) == 2) {
234 (void) fprintf(stderr
, gettext("%s: wild -c argument %s\n"),
240 cutlotime
= yeartot(cutloyear
);
241 cuthitime
= yeartot(cuthiyear
);
245 for (i
= optind
; i
< argc
; ++i
)
246 if (strlen(argv
[i
]) > longest
)
247 longest
= strlen(argv
[i
]);
249 for (i
= optind
; i
< argc
; ++i
) {
250 static char buf
[MAX_STRING_LENGTH
];
251 static char *tzp
= NULL
;
253 (void) unsetenv("TZ");
256 if ((tzp
= malloc(3 + strlen(argv
[i
]) + 1)) == NULL
) {
260 (void) strcpy(tzp
, "TZ=");
261 (void) strcat(tzp
, argv
[i
]);
262 if (putenv(tzp
) != 0) {
267 show(argv
[i
], now
, FALSE
);
273 * We show the current time first, probably because we froze
274 * the behavior of zdump some time ago and then it got
277 show(argv
[i
], now
, TRUE
);
280 t
= absolute_min_time
;
281 show(argv
[i
], t
, TRUE
);
282 t
+= SECSPERHOUR
* HOURSPERDAY
;
283 show(argv
[i
], t
, TRUE
);
286 tmp
= my_localtime(&t
);
289 (void) strncpy(buf
, abbr(&tm
), sizeof (buf
) - 1);
294 /* check if newt will overrun maximum time_t value */
295 if (t
> LONG_MAX
- (SECSPERHOUR
* 12))
297 newt
= t
+ SECSPERHOUR
* 12;
298 if (newt
>= cuthitime
)
300 newtmp
= localtime(&newt
);
303 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
304 (delta(&newtm
, &tm
) != (newt
- t
) ||
305 newtm
.tm_isdst
!= tm
.tm_isdst
||
306 strcmp(abbr(&newtm
), buf
) != 0)) {
307 newt
= hunt(argv
[i
], t
, newt
);
308 newtmp
= localtime(&newt
);
309 if (newtmp
!= NULL
) {
320 t
= absolute_max_time
;
322 show(argv
[i
], t
, TRUE
);
323 t
-= SECSPERHOUR
* HOURSPERDAY
;
324 show(argv
[i
], t
, TRUE
);
325 #else /* !defined(sun) */
326 t
-= SECSPERHOUR
* HOURSPERDAY
;
327 show(argv
[i
], t
, TRUE
);
328 t
+= SECSPERHOUR
* HOURSPERDAY
;
329 show(argv
[i
], t
, TRUE
);
330 #endif /* !defined(sun) */
332 if (fflush(stdout
) || ferror(stdout
)) {
333 (void) fprintf(stderr
, "%s: ", progname
);
334 (void) perror(gettext("Error writing standard output"));
337 return (EXIT_SUCCESS
);
344 absolute_min_time
= LONG_MIN
;
345 absolute_max_time
= LONG_MAX
;
347 if (0.5 == (time_t)0.5) {
349 * time_t is floating.
351 if (sizeof (time_t) == sizeof (float)) {
352 absolute_min_time
= (time_t)-FLT_MAX
;
353 absolute_max_time
= (time_t)FLT_MAX
;
354 } else if (sizeof (time_t) == sizeof (double)) {
355 absolute_min_time
= (time_t)-DBL_MAX
;
356 absolute_max_time
= (time_t)DBL_MAX
;
358 (void) fprintf(stderr
, gettext("%s: use of -v on "
359 "system with floating time_t other than float "
360 "or double\n"), progname
);
364 /*CONSTANTCONDITION*/
365 if (0 > (time_t)-1) {
369 register time_t hibit
;
371 for (hibit
= 1; (hibit
* 2) != 0; hibit
*= 2)
373 absolute_min_time
= hibit
;
374 absolute_max_time
= -(hibit
+ 1);
377 * time_t is unsigned.
379 absolute_min_time
= 0;
380 absolute_max_time
= absolute_min_time
- 1;
390 register long seconds
;
397 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
399 if (t
> absolute_max_time
- seconds
) {
400 t
= absolute_max_time
;
406 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
407 if (t
< absolute_min_time
+ seconds
) {
408 t
= absolute_min_time
;
426 register struct tm
*lotmp
;
428 register struct tm
*tmp
;
429 char loab
[MAX_STRING_LENGTH
];
431 lotmp
= my_localtime(&lot
);
434 (void) strncpy(loab
, abbr(&lotm
), sizeof (loab
) - 1);
437 diff
= (long)(hit
- lot
);
446 tmp
= my_localtime(&t
);
449 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
450 (delta(&tm
, &lotm
) == (t
- lot
) &&
451 tm
.tm_isdst
== lotm
.tm_isdst
&&
452 strcmp(abbr(&tm
), loab
) == 0)) {
458 show(name
, lot
, TRUE
);
459 show(name
, hit
, TRUE
);
464 * Thanks to Paul Eggert for logic used in delta.
472 register long result
;
475 if (newp
->tm_year
< oldp
->tm_year
)
476 return (-delta(oldp
, newp
));
478 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
479 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
480 result
+= newp
->tm_yday
- oldp
->tm_yday
;
481 result
*= HOURSPERDAY
;
482 result
+= newp
->tm_hour
- oldp
->tm_hour
;
483 result
*= MINSPERHOUR
;
484 result
+= newp
->tm_min
- oldp
->tm_min
;
485 result
*= SECSPERMIN
;
486 result
+= newp
->tm_sec
- oldp
->tm_sec
;
496 register struct tm
*tmp
;
498 (void) printf("%-*s ", (int)longest
, zone
);
502 (void) printf(tformat(), t
);
505 (void) printf(" UTC");
507 (void) printf(" = ");
509 tmp
= my_localtime(&t
);
512 if (*abbr(tmp
) != '\0')
513 (void) printf(" %s", abbr(tmp
));
515 (void) printf(" isdst=%d", tmp
->tm_isdst
);
517 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
518 #endif /* defined TM_GMTOFF */
522 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
523 abbrok(abbr(tmp
), zone
);
530 register char *result
;
533 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
535 result
= tzname
[tmp
->tm_isdst
];
536 return ((result
== NULL
) ? &nada
: result
);
540 * The code below can fail on certain theoretical systems;
541 * it works on all known real-world systems as of 2004-12-30.
548 /* time_t is signed long */
551 /*CONSTANTCONDITION*/
552 if (0.5 == (time_t)0.5) { /* floating */
553 /*CONSTANTCONDITION*/
554 if (sizeof (time_t) > sizeof (double))
558 /*CONSTANTCONDITION*/
559 if (0 > (time_t)-1) { /* signed */
560 /*CONSTANTCONDITION*/
561 if (sizeof (time_t) > sizeof (long))
563 /*CONSTANTCONDITION*/
564 if (sizeof (time_t) > sizeof (int))
568 /*CONSTANTCONDITION*/
569 if (sizeof (time_t) > sizeof (unsigned long))
571 /*CONSTANTCONDITION*/
572 if (sizeof (time_t) > sizeof (unsigned int))
580 register const struct tm
*timeptr
;
582 static const char wday_name
[][3] = {
583 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
585 static const char mon_name
[][3] = {
586 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
587 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
589 register const char *wn
;
590 register const char *mn
;
594 if (timeptr
== NULL
) {
595 (void) printf("NULL");
599 * The packaged versions of localtime and gmtime never put out-of-range
600 * values in tm_wday or tm_mon, but since this code might be compiled
601 * with other (perhaps experimental) versions, paranoia is in order.
603 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
604 (int)(sizeof (wday_name
) / sizeof (wday_name
[0])))
606 else wn
= wday_name
[timeptr
->tm_wday
];
607 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
608 (int)(sizeof (mon_name
) / sizeof (mon_name
[0])))
610 else mn
= mon_name
[timeptr
->tm_mon
];
611 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
613 timeptr
->tm_mday
, timeptr
->tm_hour
,
614 timeptr
->tm_min
, timeptr
->tm_sec
);
616 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
617 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
620 if (trail
< 0 && lead
> 0) {
623 } else if (lead
< 0 && trail
> 0) {
628 (void) printf("%d", trail
);
630 (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));
636 (void) fprintf(stderr
, gettext(
637 "%s: [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),