1 static char elsieid
[] = "@(#)zdump.c 7.66";
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 isascii, isalpha, isdigit */
18 #define ZDUMP_LO_YEAR (-500)
19 #endif /* !defined ZDUMP_LO_YEAR */
22 #define ZDUMP_HI_YEAR 2500
23 #endif /* !defined ZDUMP_HI_YEAR */
25 #ifndef MAX_STRING_LENGTH
26 #define MAX_STRING_LENGTH 1024
27 #endif /* !defined MAX_STRING_LENGTH */
31 #endif /* !defined TRUE */
35 #endif /* !defined FALSE */
38 #define EXIT_SUCCESS 0
39 #endif /* !defined EXIT_SUCCESS */
42 #define EXIT_FAILURE 1
43 #endif /* !defined EXIT_FAILURE */
47 #endif /* !defined SECSPERMIN */
50 #define MINSPERHOUR 60
51 #endif /* !defined MINSPERHOUR */
54 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
55 #endif /* !defined SECSPERHOUR */
58 #define HOURSPERDAY 24
59 #endif /* !defined HOURSPERDAY */
62 #define EPOCH_YEAR 1970
63 #endif /* !defined EPOCH_YEAR */
66 #define TM_YEAR_BASE 1900
67 #endif /* !defined TM_YEAR_BASE */
70 #define DAYSPERNYEAR 365
71 #endif /* !defined DAYSPERNYEAR */
74 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
75 #endif /* !defined isleap */
79 ** See tzfile.h for details on isleap_sum.
81 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
82 #endif /* !defined isleap_sum */
84 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
85 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
86 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
89 #include "locale.h" /* for setlocale */
91 #endif /* HAVE_GETTEXT */
96 #else /* !defined lint */
99 #endif /* defined __GNUC__ */
100 #endif /* !defined lint */
101 #endif /* !defined GNUC_or_lint */
105 #define INITIALIZE(x) ((x) = 0)
106 #else /* !defined GNUC_or_lint */
107 #define INITIALIZE(x)
108 #endif /* !defined GNUC_or_lint */
109 #endif /* !defined INITIALIZE */
112 ** For the benefit of GNU folk...
113 ** `_(MSGID)' uses the current locale's message library string for MSGID.
114 ** The default is to use gettext if available, and use MSGID otherwise.
119 #define _(msgid) gettext(msgid)
120 #else /* !HAVE_GETTEXT */
121 #define _(msgid) msgid
122 #endif /* !HAVE_GETTEXT */
123 #endif /* !defined _ */
126 #define TZ_DOMAIN "tz"
127 #endif /* !defined TZ_DOMAIN */
132 #else /* !defined __STDC__ */
134 #endif /* !defined __STDC__ */
135 #endif /* !defined P */
137 extern char ** environ
;
138 extern int getopt
P((int argc
, char * const argv
[],
139 const char * options
));
140 extern char * optarg
;
142 extern char * tzname
[2];
144 static time_t absolute_min_time
;
145 static time_t absolute_max_time
;
146 static size_t longest
;
147 static char * progname
;
150 static char * abbr
P((struct tm
* tmp
));
151 static void abbrok
P((const char * abbr
, const char * zone
));
152 static long delta
P((struct tm
* newp
, struct tm
* oldp
));
153 static void dumptime
P((const struct tm
* tmp
));
154 static time_t hunt
P((char * name
, time_t lot
, time_t hit
));
155 static void setabsolutes
P((void));
156 static void show
P((char * zone
, time_t t
, int v
));
157 static const char * tformat
P((void));
158 static time_t yeartot
P((long y
));
161 #define my_localtime localtime
162 #else /* !defined TYPECHECK */
167 register struct tm
* tmp
;
170 if (tp
!= NULL
&& tmp
!= NULL
) {
176 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
177 (void) fflush(stdout
);
178 (void) fprintf(stderr
, "\n%s: ", progname
);
179 (void) fprintf(stderr
, tformat(), *tp
);
180 (void) fprintf(stderr
, " ->");
181 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
182 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
183 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
184 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
185 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
186 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
187 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
188 (void) fprintf(stderr
, " -> ");
189 (void) fprintf(stderr
, tformat(), t
);
190 (void) fprintf(stderr
, "\n");
195 #endif /* !defined TYPECHECK */
199 const char * const abbr
;
200 const char * const zone
;
202 register const char * cp
;
209 while (isascii(*cp
) && isalpha(*cp
))
212 wp
= _("lacks alphabetic at start");
214 wp
= _("has fewer than 3 alphabetics");
216 wp
= _("has more than 6 alphabetics");
217 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
219 if (isascii(*cp
) && isdigit(*cp
))
220 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
224 wp
= _("differs from POSIX standard");
227 (void) fflush(stdout
);
228 (void) fprintf(stderr
,
229 "%s: warning: zone \"%s\" abbreviation \"%s\" %s\n",
230 progname
, zone
, abbr
, wp
);
242 register char * cutarg
;
243 register long cutloyear
= ZDUMP_LO_YEAR
;
244 register long cuthiyear
= ZDUMP_HI_YEAR
;
245 register time_t cutlotime
;
246 register time_t cuthitime
;
247 register char ** fakeenv
;
253 register struct tm
* tmp
;
254 register struct tm
* newtmp
;
256 INITIALIZE(cutlotime
);
257 INITIALIZE(cuthitime
);
259 (void) setlocale(LC_ALL
, "");
261 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
262 #endif /* defined TEXTDOMAINDIR */
263 (void) textdomain(TZ_DOMAIN
);
264 #endif /* HAVE_GETTEXT */
266 for (i
= 1; i
< argc
; ++i
)
267 if (strcmp(argv
[i
], "--version") == 0) {
268 (void) printf("%s\n", elsieid
);
269 (void) exit(EXIT_SUCCESS
);
273 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
276 else cutarg
= optarg
;
277 if ((c
!= EOF
&& c
!= -1) ||
278 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
279 (void) fprintf(stderr
,
280 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
282 (void) exit(EXIT_FAILURE
);
285 if (cutarg
!= NULL
) {
290 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
292 } else if (sscanf(cutarg
, "%ld,%ld%c",
293 &lo
, &hi
, &dummy
) == 2) {
297 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
299 (void) exit(EXIT_FAILURE
);
303 cutlotime
= yeartot(cutloyear
);
304 cuthitime
= yeartot(cuthiyear
);
308 for (i
= optind
; i
< argc
; ++i
)
309 if (strlen(argv
[i
]) > longest
)
310 longest
= strlen(argv
[i
]);
315 for (i
= 0; environ
[i
] != NULL
; ++i
)
317 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
319 if (fakeenv
== NULL
||
320 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
321 (void) perror(progname
);
322 (void) exit(EXIT_FAILURE
);
325 (void) strcpy(fakeenv
[to
++], "TZ=");
326 for (from
= 0; environ
[from
] != NULL
; ++from
)
327 if (strncmp(environ
[from
], "TZ=", 3) != 0)
328 fakeenv
[to
++] = environ
[from
];
332 for (i
= optind
; i
< argc
; ++i
) {
333 static char buf
[MAX_STRING_LENGTH
];
335 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
337 show(argv
[i
], now
, FALSE
);
341 t
= absolute_min_time
;
342 show(argv
[i
], t
, TRUE
);
343 t
+= SECSPERHOUR
* HOURSPERDAY
;
344 show(argv
[i
], t
, TRUE
);
347 tmp
= my_localtime(&t
);
350 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
355 newt
= t
+ SECSPERHOUR
* 12;
356 if (newt
>= cuthitime
)
360 newtmp
= localtime(&newt
);
363 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
364 (delta(&newtm
, &tm
) != (newt
- t
) ||
365 newtm
.tm_isdst
!= tm
.tm_isdst
||
366 strcmp(abbr(&newtm
), buf
) != 0)) {
367 newt
= hunt(argv
[i
], t
, newt
);
368 newtmp
= localtime(&newt
);
369 if (newtmp
!= NULL
) {
380 t
= absolute_max_time
;
381 t
-= SECSPERHOUR
* HOURSPERDAY
;
382 show(argv
[i
], t
, TRUE
);
383 t
+= SECSPERHOUR
* HOURSPERDAY
;
384 show(argv
[i
], t
, TRUE
);
386 if (fflush(stdout
) || ferror(stdout
)) {
387 (void) fprintf(stderr
, "%s: ", progname
);
388 (void) perror(_("Error writing standard output"));
389 (void) exit(EXIT_FAILURE
);
392 /* If exit fails to exit... */
399 if (0.5 == (time_t) 0.5) {
401 ** time_t is floating.
403 if (sizeof (time_t) == sizeof (float)) {
404 absolute_min_time
= (time_t) -FLT_MAX
;
405 absolute_max_time
= (time_t) FLT_MAX
;
406 } else if (sizeof (time_t) == sizeof (double)) {
407 absolute_min_time
= (time_t) -DBL_MAX
;
408 absolute_max_time
= (time_t) DBL_MAX
;
410 (void) fprintf(stderr
,
411 _("%s: use of -v on system with floating time_t other than float or double\n"),
413 (void) exit(EXIT_FAILURE
);
415 } else if (0 > (time_t) -1) {
419 register time_t hibit
;
421 for (hibit
= 1; (hibit
* 2) != 0; hibit
*= 2)
423 absolute_min_time
= hibit
;
424 absolute_max_time
= -(hibit
+ 1);
427 ** time_t is unsigned.
429 absolute_min_time
= 0;
430 absolute_max_time
= absolute_min_time
- 1;
439 register long seconds
;
446 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
448 if (t
> absolute_max_time
- seconds
) {
449 t
= absolute_max_time
;
455 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
456 if (t
< absolute_min_time
+ seconds
) {
457 t
= absolute_min_time
;
475 register struct tm
* lotmp
;
477 register struct tm
* tmp
;
478 char loab
[MAX_STRING_LENGTH
];
480 lotmp
= my_localtime(&lot
);
483 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
486 diff
= (long) (hit
- lot
);
495 tmp
= my_localtime(&t
);
498 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
499 (delta(&tm
, &lotm
) == (t
- lot
) &&
500 tm
.tm_isdst
== lotm
.tm_isdst
&&
501 strcmp(abbr(&tm
), loab
) == 0)) {
507 show(name
, lot
, TRUE
);
508 show(name
, hit
, TRUE
);
513 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
521 register long result
;
524 if (newp
->tm_year
< oldp
->tm_year
)
525 return -delta(oldp
, newp
);
527 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
528 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
529 result
+= newp
->tm_yday
- oldp
->tm_yday
;
530 result
*= HOURSPERDAY
;
531 result
+= newp
->tm_hour
- oldp
->tm_hour
;
532 result
*= MINSPERHOUR
;
533 result
+= newp
->tm_min
- oldp
->tm_min
;
534 result
*= SECSPERMIN
;
535 result
+= newp
->tm_sec
- oldp
->tm_sec
;
545 register struct tm
* tmp
;
547 (void) printf("%-*s ", (int) longest
, zone
);
551 (void) printf(tformat(), t
);
554 (void) printf(" UTC");
556 (void) printf(" = ");
558 tmp
= my_localtime(&t
);
561 if (*abbr(tmp
) != '\0')
562 (void) printf(" %s", abbr(tmp
));
564 (void) printf(" isdst=%d", tmp
->tm_isdst
);
566 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
567 #endif /* defined TM_GMTOFF */
571 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
572 abbrok(abbr(tmp
), zone
);
579 register char * result
;
582 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
584 result
= tzname
[tmp
->tm_isdst
];
585 return (result
== NULL
) ? &nada
: result
;
589 ** The code below can fail on certain theoretical systems;
590 ** it works on all known real-world systems as of 2004-12-30.
596 if (0.5 == (time_t) 0.5) { /* floating */
597 if (sizeof (time_t) > sizeof (double))
601 if (0 > (time_t) -1) { /* signed */
602 if (sizeof (time_t) > sizeof (long))
604 if (sizeof (time_t) > sizeof (int))
608 if (sizeof (time_t) > sizeof (unsigned long))
610 if (sizeof (time_t) > sizeof (unsigned int))
617 register const struct tm
* timeptr
;
619 static const char wday_name
[][3] = {
620 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
622 static const char mon_name
[][3] = {
623 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
624 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
626 register const char * wn
;
627 register const char * mn
;
631 if (timeptr
== NULL
) {
632 (void) printf("NULL");
636 ** The packaged versions of localtime and gmtime never put out-of-range
637 ** values in tm_wday or tm_mon, but since this code might be compiled
638 ** with other (perhaps experimental) versions, paranoia is in order.
640 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
641 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
643 else wn
= wday_name
[timeptr
->tm_wday
];
644 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
645 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
647 else mn
= mon_name
[timeptr
->tm_mon
];
648 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
650 timeptr
->tm_mday
, timeptr
->tm_hour
,
651 timeptr
->tm_min
, timeptr
->tm_sec
);
653 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
654 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
657 if (trail
< 0 && lead
> 0) {
660 } else if (lead
< 0 && trail
> 0) {
665 (void) printf("%d", trail
);
666 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));