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 #endif /* defined __GNUC__ */
74 #endif /* !defined GNUC_or_lint */
78 #define INITIALIZE(x) ((x) = 0)
79 #else /* !defined GNUC_or_lint */
81 #endif /* !defined GNUC_or_lint */
82 #endif /* !defined INITIALIZE */
84 static time_t absolute_min_time
;
85 static time_t absolute_max_time
;
86 static size_t longest
;
87 static char *progname
;
90 static char *abbr(struct tm
*);
91 static void abbrok(const char *, const char *);
92 static long delta(struct tm
*, struct tm
*);
93 static void dumptime(const struct tm
*);
94 static time_t hunt(char *, time_t, time_t);
95 static void setabsolutes(void);
96 static void show(char *, time_t, int);
97 static void usage(void);
98 static const char *tformat(void);
99 static time_t yeartot(long y
);
102 #define my_localtime localtime
103 #else /* !defined TYPECHECK */
108 register struct tm
*tmp
;
111 if (tp
!= NULL
&& tmp
!= NULL
) {
117 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
118 (void) fflush(stdout
);
119 (void) fprintf(stderr
, "\n%s: ", progname
);
120 (void) fprintf(stderr
, tformat(), *tp
);
121 (void) fprintf(stderr
, " ->");
122 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
123 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
124 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
125 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
126 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
127 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
128 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
129 (void) fprintf(stderr
, " -> ");
130 (void) fprintf(stderr
, tformat(), t
);
131 (void) fprintf(stderr
, "\n");
136 #endif /* !defined TYPECHECK */
139 abbrok(const char * const abbrp
, const char * const zone
)
141 register const char *cp
;
147 while (isalpha(*cp
) || isdigit(*cp
) || *cp
== '-' || *cp
== '+')
149 (void) fflush(stdout
);
150 if (cp
- abbrp
< 3) {
151 (void) fprintf(stderr
, gettext("%s: warning: zone \"%s\" "
152 "abbreviation \"%s\" has fewer than 3 alphabetics\n"),
153 progname
, zone
, abbrp
);
155 } else if (cp
- abbrp
> 6) {
156 (void) fprintf(stderr
, gettext("%s: warning: zone \"%s\" "
157 "abbreviation \"%s\" has more than 6 characters\n"),
158 progname
, zone
, abbrp
);
160 } else if (*cp
!= '\0') {
161 (void) fprintf(stderr
, gettext("%s: warning: zone \"%s\" "
162 "abbreviation \"%s\" has characters other than "
163 "alphanumerics\n"), progname
, zone
, abbrp
);
178 register char *cutarg
;
179 register long cutloyear
= ZDUMP_LO_YEAR
;
180 register long cuthiyear
= ZDUMP_HI_YEAR
;
181 register time_t cutlotime
;
182 register time_t cuthitime
;
188 register struct tm
*tmp
;
189 register struct tm
*newtmp
;
191 INITIALIZE(cutlotime
);
192 INITIALIZE(cuthitime
);
194 (void) setlocale(LC_ALL
, "");
195 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
196 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
198 (void) textdomain(TEXT_DOMAIN
);
201 for (i
= 1; i
< argc
; ++i
)
202 if (strcmp(argv
[i
], "--version") == 0) {
203 (void) printf("%s\n", elsieid
);
208 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
211 else cutarg
= optarg
;
213 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
218 if (cutarg
!= NULL
) {
223 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
225 } else if (sscanf(cutarg
, "%ld,%ld%c",
226 &lo
, &hi
, &dummy
) == 2) {
230 (void) fprintf(stderr
, gettext("%s: wild -c argument %s\n"),
236 cutlotime
= yeartot(cutloyear
);
237 cuthitime
= yeartot(cuthiyear
);
241 for (i
= optind
; i
< argc
; ++i
)
242 if (strlen(argv
[i
]) > longest
)
243 longest
= strlen(argv
[i
]);
245 for (i
= optind
; i
< argc
; ++i
) {
246 static char buf
[MAX_STRING_LENGTH
];
247 static char *tzp
= NULL
;
249 (void) unsetenv("TZ");
252 if ((tzp
= malloc(3 + strlen(argv
[i
]) + 1)) == NULL
) {
256 (void) strcpy(tzp
, "TZ=");
257 (void) strcat(tzp
, argv
[i
]);
258 if (putenv(tzp
) != 0) {
263 show(argv
[i
], now
, FALSE
);
269 * We show the current time first, probably because we froze
270 * the behavior of zdump some time ago and then it got
273 show(argv
[i
], now
, TRUE
);
276 t
= absolute_min_time
;
277 show(argv
[i
], t
, TRUE
);
278 t
+= SECSPERHOUR
* HOURSPERDAY
;
279 show(argv
[i
], t
, TRUE
);
282 tmp
= my_localtime(&t
);
285 (void) strncpy(buf
, abbr(&tm
), sizeof (buf
) - 1);
290 /* check if newt will overrun maximum time_t value */
291 if (t
> LONG_MAX
- (SECSPERHOUR
* 12))
293 newt
= t
+ SECSPERHOUR
* 12;
294 if (newt
>= cuthitime
)
296 newtmp
= localtime(&newt
);
299 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
300 (delta(&newtm
, &tm
) != (newt
- t
) ||
301 newtm
.tm_isdst
!= tm
.tm_isdst
||
302 strcmp(abbr(&newtm
), buf
) != 0)) {
303 newt
= hunt(argv
[i
], t
, newt
);
304 newtmp
= localtime(&newt
);
305 if (newtmp
!= NULL
) {
316 t
= absolute_max_time
;
318 show(argv
[i
], t
, TRUE
);
319 t
-= SECSPERHOUR
* HOURSPERDAY
;
320 show(argv
[i
], t
, TRUE
);
321 #else /* !defined(sun) */
322 t
-= SECSPERHOUR
* HOURSPERDAY
;
323 show(argv
[i
], t
, TRUE
);
324 t
+= SECSPERHOUR
* HOURSPERDAY
;
325 show(argv
[i
], t
, TRUE
);
326 #endif /* !defined(sun) */
328 if (fflush(stdout
) || ferror(stdout
)) {
329 (void) fprintf(stderr
, "%s: ", progname
);
330 (void) perror(gettext("Error writing standard output"));
333 return (EXIT_SUCCESS
);
340 absolute_min_time
= LONG_MIN
;
341 absolute_max_time
= LONG_MAX
;
343 if (0.5 == (time_t)0.5) {
345 * time_t is floating.
347 if (sizeof (time_t) == sizeof (float)) {
348 absolute_min_time
= (time_t)-FLT_MAX
;
349 absolute_max_time
= (time_t)FLT_MAX
;
350 } else if (sizeof (time_t) == sizeof (double)) {
351 absolute_min_time
= (time_t)-DBL_MAX
;
352 absolute_max_time
= (time_t)DBL_MAX
;
354 (void) fprintf(stderr
, gettext("%s: use of -v on "
355 "system with floating time_t other than float "
356 "or double\n"), progname
);
360 /*CONSTANTCONDITION*/
361 if (0 > (time_t)-1) {
365 register time_t hibit
;
367 for (hibit
= 1; (hibit
* 2) != 0; hibit
*= 2)
369 absolute_min_time
= hibit
;
370 absolute_max_time
= -(hibit
+ 1);
373 * time_t is unsigned.
375 absolute_min_time
= 0;
376 absolute_max_time
= absolute_min_time
- 1;
386 register long seconds
;
393 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
395 if (t
> absolute_max_time
- seconds
) {
396 t
= absolute_max_time
;
402 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
403 if (t
< absolute_min_time
+ seconds
) {
404 t
= absolute_min_time
;
422 register struct tm
*lotmp
;
424 register struct tm
*tmp
;
425 char loab
[MAX_STRING_LENGTH
];
427 lotmp
= my_localtime(&lot
);
430 (void) strncpy(loab
, abbr(&lotm
), sizeof (loab
) - 1);
433 diff
= (long)(hit
- lot
);
442 tmp
= my_localtime(&t
);
445 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
446 (delta(&tm
, &lotm
) == (t
- lot
) &&
447 tm
.tm_isdst
== lotm
.tm_isdst
&&
448 strcmp(abbr(&tm
), loab
) == 0)) {
454 show(name
, lot
, TRUE
);
455 show(name
, hit
, TRUE
);
460 * Thanks to Paul Eggert for logic used in delta.
468 register long result
;
471 if (newp
->tm_year
< oldp
->tm_year
)
472 return (-delta(oldp
, newp
));
474 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
475 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
476 result
+= newp
->tm_yday
- oldp
->tm_yday
;
477 result
*= HOURSPERDAY
;
478 result
+= newp
->tm_hour
- oldp
->tm_hour
;
479 result
*= MINSPERHOUR
;
480 result
+= newp
->tm_min
- oldp
->tm_min
;
481 result
*= SECSPERMIN
;
482 result
+= newp
->tm_sec
- oldp
->tm_sec
;
492 register struct tm
*tmp
;
494 (void) printf("%-*s ", (int)longest
, zone
);
498 (void) printf(tformat(), t
);
501 (void) printf(" UTC");
503 (void) printf(" = ");
505 tmp
= my_localtime(&t
);
508 if (*abbr(tmp
) != '\0')
509 (void) printf(" %s", abbr(tmp
));
511 (void) printf(" isdst=%d", tmp
->tm_isdst
);
513 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
514 #endif /* defined TM_GMTOFF */
518 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
519 abbrok(abbr(tmp
), zone
);
526 register char *result
;
529 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
531 result
= tzname
[tmp
->tm_isdst
];
532 return ((result
== NULL
) ? &nada
: result
);
536 * The code below can fail on certain theoretical systems;
537 * it works on all known real-world systems as of 2004-12-30.
544 /* time_t is signed long */
547 /*CONSTANTCONDITION*/
548 if (0.5 == (time_t)0.5) { /* floating */
549 /*CONSTANTCONDITION*/
550 if (sizeof (time_t) > sizeof (double))
554 /*CONSTANTCONDITION*/
555 if (0 > (time_t)-1) { /* signed */
556 /*CONSTANTCONDITION*/
557 if (sizeof (time_t) > sizeof (long))
559 /*CONSTANTCONDITION*/
560 if (sizeof (time_t) > sizeof (int))
564 /*CONSTANTCONDITION*/
565 if (sizeof (time_t) > sizeof (unsigned long))
567 /*CONSTANTCONDITION*/
568 if (sizeof (time_t) > sizeof (unsigned int))
576 register const struct tm
*timeptr
;
578 static const char wday_name
[][3] = {
579 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
581 static const char mon_name
[][3] = {
582 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
583 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
585 register const char *wn
;
586 register const char *mn
;
590 if (timeptr
== NULL
) {
591 (void) printf("NULL");
595 * The packaged versions of localtime and gmtime never put out-of-range
596 * values in tm_wday or tm_mon, but since this code might be compiled
597 * with other (perhaps experimental) versions, paranoia is in order.
599 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
600 (int)(sizeof (wday_name
) / sizeof (wday_name
[0])))
602 else wn
= wday_name
[timeptr
->tm_wday
];
603 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
604 (int)(sizeof (mon_name
) / sizeof (mon_name
[0])))
606 else mn
= mon_name
[timeptr
->tm_mon
];
607 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
609 timeptr
->tm_mday
, timeptr
->tm_hour
,
610 timeptr
->tm_min
, timeptr
->tm_sec
);
612 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
613 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
616 if (trail
< 0 && lead
> 0) {
619 } else if (lead
< 0 && trail
> 0) {
624 (void) printf("%d", trail
);
626 (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));
632 (void) fprintf(stderr
, gettext(
633 "%s: [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),