1 static char elsieid
[] = "@(#)zdump.c 7.77";
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. */
18 #endif /* !defined isascii */
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 /* !__STDC__ */
137 #endif /* !__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
);
239 main( int argc
, char * argv
[])
244 register char * cutarg
;
245 register long cutloyear
= ZDUMP_LO_YEAR
;
246 register long cuthiyear
= ZDUMP_HI_YEAR
;
247 register time_t cutlotime
;
248 register time_t cuthitime
;
249 register char ** fakeenv
;
255 register struct tm
* tmp
;
256 register struct tm
* newtmp
;
258 INITIALIZE(cutlotime
);
259 INITIALIZE(cuthitime
);
261 (void) setlocale(LC_ALL
, "");
263 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
264 #endif /* defined TEXTDOMAINDIR */
265 (void) textdomain(TZ_DOMAIN
);
266 #endif /* HAVE_GETTEXT */
268 for (i
= 1; i
< argc
; ++i
)
269 if (strcmp(argv
[i
], "--version") == 0) {
270 (void) printf("%s\n", elsieid
);
275 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
278 else cutarg
= optarg
;
279 if ((c
!= EOF
&& c
!= -1) ||
280 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
281 (void) fprintf(stderr
,
282 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
287 if (cutarg
!= NULL
) {
292 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
294 } else if (sscanf(cutarg
, "%ld,%ld%c",
295 &lo
, &hi
, &dummy
) == 2) {
299 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
305 cutlotime
= yeartot(cutloyear
);
306 cuthitime
= yeartot(cuthiyear
);
310 for (i
= optind
; i
< argc
; ++i
)
311 if (strlen(argv
[i
]) > longest
)
312 longest
= strlen(argv
[i
]);
317 for (i
= 0; environ
[i
] != NULL
; ++i
)
319 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
321 if (fakeenv
== NULL
||
322 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
323 (void) perror(progname
);
327 (void) strcpy(fakeenv
[to
++], "TZ=");
328 for (from
= 0; environ
[from
] != NULL
; ++from
)
329 if (strncmp(environ
[from
], "TZ=", 3) != 0)
330 fakeenv
[to
++] = environ
[from
];
334 for (i
= optind
; i
< argc
; ++i
) {
335 static char buf
[MAX_STRING_LENGTH
];
337 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
339 show(argv
[i
], now
, FALSE
);
343 t
= absolute_min_time
;
344 show(argv
[i
], t
, TRUE
);
345 t
+= SECSPERHOUR
* HOURSPERDAY
;
346 show(argv
[i
], t
, TRUE
);
349 tmp
= my_localtime(&t
);
352 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
357 newt
= t
+ SECSPERHOUR
* 12;
358 if (newt
>= cuthitime
)
362 newtmp
= localtime(&newt
);
365 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
366 (delta(&newtm
, &tm
) != (newt
- t
) ||
367 newtm
.tm_isdst
!= tm
.tm_isdst
||
368 strcmp(abbr(&newtm
), buf
) != 0)) {
369 newt
= hunt(argv
[i
], t
, newt
);
370 newtmp
= localtime(&newt
);
371 if (newtmp
!= NULL
) {
382 t
= absolute_max_time
;
383 t
-= SECSPERHOUR
* HOURSPERDAY
;
384 show(argv
[i
], t
, TRUE
);
385 t
+= SECSPERHOUR
* HOURSPERDAY
;
386 show(argv
[i
], t
, TRUE
);
388 if (fflush(stdout
) || ferror(stdout
)) {
389 (void) fprintf(stderr
, "%s: ", progname
);
390 (void) perror(_("Error writing standard output"));
394 /* If exit fails to exit... */
402 if (0.5 == (time_t) 0.5) {
404 ** time_t is floating.
406 if (sizeof (time_t) == sizeof (float)) {
407 absolute_min_time
= (time_t) -FLT_MAX
;
408 absolute_max_time
= (time_t) FLT_MAX
;
409 } else if (sizeof (time_t) == sizeof (double)) {
410 absolute_min_time
= (time_t) -DBL_MAX
;
411 absolute_max_time
= (time_t) DBL_MAX
;
413 (void) fprintf(stderr
,
414 _("%s: use of -v on system with floating time_t other than float or double\n"),
420 if (0 > (time_t) -1) {
422 ** time_t is signed. Assume overflow wraps around.
432 absolute_max_time
= t
;
434 absolute_min_time
= t
- 1;
435 if (t
< absolute_min_time
)
436 absolute_min_time
= t
;
439 ** time_t is unsigned.
441 absolute_min_time
= 0;
442 absolute_max_time
= absolute_min_time
- 1;
451 register long seconds
;
458 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
460 if (t
> absolute_max_time
- seconds
) {
461 t
= absolute_max_time
;
467 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
468 if (t
< absolute_min_time
+ seconds
) {
469 t
= absolute_min_time
;
480 hunt(char *name
, time_t lot
, time_t hit
)
481 #else /* !__STDC__ */
486 #endif /* !__STDC__ */
491 register struct tm
* lotmp
;
493 register struct tm
* tmp
;
494 char loab
[MAX_STRING_LENGTH
];
496 lotmp
= my_localtime(&lot
);
499 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
502 diff
= (long) (hit
- lot
);
511 tmp
= my_localtime(&t
);
514 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
515 (delta(&tm
, &lotm
) == (t
- lot
) &&
516 tm
.tm_isdst
== lotm
.tm_isdst
&&
517 strcmp(abbr(&tm
), loab
) == 0)) {
523 show(name
, lot
, TRUE
);
524 show(name
, hit
, TRUE
);
529 ** Thanks to Paul Eggert for logic used in delta.
537 register long result
;
540 if (newp
->tm_year
< oldp
->tm_year
)
541 return -delta(oldp
, newp
);
543 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
544 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
545 result
+= newp
->tm_yday
- oldp
->tm_yday
;
546 result
*= HOURSPERDAY
;
547 result
+= newp
->tm_hour
- oldp
->tm_hour
;
548 result
*= MINSPERHOUR
;
549 result
+= newp
->tm_min
- oldp
->tm_min
;
550 result
*= SECSPERMIN
;
551 result
+= newp
->tm_sec
- oldp
->tm_sec
;
557 show(char *zone
, time_t t
, int v
)
558 #else /* !__STDC__ */
563 #endif /* !__STDC__ */
565 register struct tm
* tmp
;
567 (void) printf("%-*s ", (int) longest
, zone
);
571 (void) printf(tformat(), t
);
574 (void) printf(" UTC");
576 (void) printf(" = ");
578 tmp
= my_localtime(&t
);
581 if (*abbr(tmp
) != '\0')
582 (void) printf(" %s", abbr(tmp
));
584 (void) printf(" isdst=%d", tmp
->tm_isdst
);
586 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
587 #endif /* defined TM_GMTOFF */
591 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
592 abbrok(abbr(tmp
), zone
);
599 register char * result
;
602 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
604 result
= tzname
[tmp
->tm_isdst
];
605 return (result
== NULL
) ? &nada
: result
;
609 ** The code below can fail on certain theoretical systems;
610 ** it works on all known real-world systems as of 2004-12-30.
616 if (0.5 == (time_t) 0.5) { /* floating */
617 if (sizeof (time_t) > sizeof (double))
621 if (0 > (time_t) -1) { /* signed */
622 if (sizeof (time_t) > sizeof (long))
624 if (sizeof (time_t) > sizeof (int))
628 if (sizeof (time_t) > sizeof (unsigned long))
630 if (sizeof (time_t) > sizeof (unsigned int))
637 register const struct tm
* timeptr
;
639 static const char wday_name
[][3] = {
640 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
642 static const char mon_name
[][3] = {
643 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
644 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
646 register const char * wn
;
647 register const char * mn
;
651 if (timeptr
== NULL
) {
652 (void) printf("NULL");
656 ** The packaged versions of localtime and gmtime never put out-of-range
657 ** values in tm_wday or tm_mon, but since this code might be compiled
658 ** with other (perhaps experimental) versions, paranoia is in order.
660 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
661 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
663 else wn
= wday_name
[timeptr
->tm_wday
];
664 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
665 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
667 else mn
= mon_name
[timeptr
->tm_mon
];
668 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
670 timeptr
->tm_mday
, timeptr
->tm_hour
,
671 timeptr
->tm_min
, timeptr
->tm_sec
);
673 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
674 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
677 if (trail
< 0 && lead
> 0) {
680 } else if (lead
< 0 && trail
> 0) {
685 (void) printf("%d", trail
);
686 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));