1 static char elsieid
[] = "@(#)zdump.c 8.6";
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 #define HAVE_GETTEXT 0
95 #include "locale.h" /* for setlocale */
97 #endif /* HAVE_GETTEXT */
102 #else /* !defined lint */
105 #endif /* defined __GNUC__ */
106 #endif /* !defined lint */
107 #endif /* !defined GNUC_or_lint */
111 #define INITIALIZE(x) ((x) = 0)
112 #else /* !defined GNUC_or_lint */
113 #define INITIALIZE(x)
114 #endif /* !defined GNUC_or_lint */
115 #endif /* !defined INITIALIZE */
118 ** For the benefit of GNU folk...
119 ** `_(MSGID)' uses the current locale's message library string for MSGID.
120 ** The default is to use gettext if available, and use MSGID otherwise.
125 #define _(msgid) gettext(msgid)
126 #else /* !HAVE_GETTEXT */
127 #define _(msgid) msgid
128 #endif /* !HAVE_GETTEXT */
129 #endif /* !defined _ */
132 #define TZ_DOMAIN "tz"
133 #endif /* !defined TZ_DOMAIN */
135 extern char ** environ
;
136 extern int getopt(int argc
, char * const argv
[],
137 const char * options
);
138 extern char * optarg
;
140 extern char * tzname
[2];
142 static time_t absolute_min_time
;
143 static time_t absolute_max_time
;
144 static size_t longest
;
145 static char * progname
;
148 static char * abbr(struct tm
* tmp
);
149 static void abbrok(const char * abbrp
, const char * zone
);
150 static long delta(struct tm
* newp
, struct tm
* oldp
);
151 static void dumptime(const struct tm
* tmp
);
152 static time_t hunt(char * name
, time_t lot
, time_t hit
);
153 static void setabsolutes(void);
154 static void show(char * zone
, time_t t
, int v
);
155 static const char * tformat(void);
156 static time_t yeartot(long y
);
159 #define my_localtime localtime
160 #else /* !defined TYPECHECK */
165 register struct tm
* tmp
;
168 if (tp
!= NULL
&& tmp
!= NULL
) {
174 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
175 (void) fflush(stdout
);
176 (void) fprintf(stderr
, "\n%s: ", progname
);
177 (void) fprintf(stderr
, tformat(), *tp
);
178 (void) fprintf(stderr
, " ->");
179 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
180 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
181 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
182 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
183 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
184 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
185 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
186 (void) fprintf(stderr
, " -> ");
187 (void) fprintf(stderr
, tformat(), t
);
188 (void) fprintf(stderr
, "\n");
193 #endif /* !defined TYPECHECK */
197 const char * const abbrp
;
198 const char * const zone
;
200 register const char * cp
;
207 while (isascii((unsigned char) *cp
) && isalpha((unsigned char) *cp
))
210 wp
= _("lacks alphabetic at start");
211 else if (cp
- abbrp
< 3)
212 wp
= _("has fewer than 3 alphabetics");
213 else if (cp
- abbrp
> 6)
214 wp
= _("has more than 6 alphabetics");
215 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
217 if (isascii((unsigned char) *cp
) &&
218 isdigit((unsigned char) *cp
))
219 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
222 wp
= _("differs from POSIX standard");
226 (void) fflush(stdout
);
227 (void) fprintf(stderr
,
228 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
229 progname
, zone
, abbrp
, wp
);
241 register char * cutarg
;
242 register long cutloyear
= ZDUMP_LO_YEAR
;
243 register long cuthiyear
= ZDUMP_HI_YEAR
;
244 register time_t cutlotime
;
245 register time_t cuthitime
;
246 register char ** fakeenv
;
252 register struct tm
* tmp
;
253 register struct tm
* newtmp
;
255 INITIALIZE(cutlotime
);
256 INITIALIZE(cuthitime
);
258 (void) setlocale(LC_ALL
, "");
260 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
261 #endif /* defined TEXTDOMAINDIR */
262 (void) textdomain(TZ_DOMAIN
);
263 #endif /* HAVE_GETTEXT */
265 for (i
= 1; i
< argc
; ++i
)
266 if (strcmp(argv
[i
], "--version") == 0) {
267 (void) printf("%s\n", elsieid
);
272 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
275 else cutarg
= optarg
;
276 if ((c
!= EOF
&& c
!= -1) ||
277 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
278 (void) fprintf(stderr
,
279 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
284 if (cutarg
!= NULL
) {
289 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
291 } else if (sscanf(cutarg
, "%ld,%ld%c",
292 &lo
, &hi
, &dummy
) == 2) {
296 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
302 cutlotime
= yeartot(cutloyear
);
303 cuthitime
= yeartot(cuthiyear
);
307 for (i
= optind
; i
< argc
; ++i
)
308 if (strlen(argv
[i
]) > longest
)
309 longest
= strlen(argv
[i
]);
314 for (i
= 0; environ
[i
] != NULL
; ++i
)
316 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
318 if (fakeenv
== NULL
||
319 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
320 (void) perror(progname
);
324 (void) strcpy(fakeenv
[to
++], "TZ=");
325 for (from
= 0; environ
[from
] != NULL
; ++from
)
326 if (strncmp(environ
[from
], "TZ=", 3) != 0)
327 fakeenv
[to
++] = environ
[from
];
331 for (i
= optind
; i
< argc
; ++i
) {
332 static char buf
[MAX_STRING_LENGTH
];
334 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
336 show(argv
[i
], now
, FALSE
);
340 t
= absolute_min_time
;
341 show(argv
[i
], t
, TRUE
);
342 t
+= SECSPERHOUR
* HOURSPERDAY
;
343 show(argv
[i
], t
, TRUE
);
346 tmp
= my_localtime(&t
);
349 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
354 newt
= t
+ SECSPERHOUR
* 12;
355 if (newt
>= cuthitime
)
359 newtmp
= localtime(&newt
);
362 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
363 (delta(&newtm
, &tm
) != (newt
- t
) ||
364 newtm
.tm_isdst
!= tm
.tm_isdst
||
365 strcmp(abbr(&newtm
), buf
) != 0)) {
366 newt
= hunt(argv
[i
], t
, newt
);
367 newtmp
= localtime(&newt
);
368 if (newtmp
!= NULL
) {
379 t
= absolute_max_time
;
380 t
-= SECSPERHOUR
* HOURSPERDAY
;
381 show(argv
[i
], t
, TRUE
);
382 t
+= SECSPERHOUR
* HOURSPERDAY
;
383 show(argv
[i
], t
, TRUE
);
385 if (fflush(stdout
) || ferror(stdout
)) {
386 (void) fprintf(stderr
, "%s: ", progname
);
387 (void) perror(_("Error writing to standard output"));
391 /* If exit fails to exit... */
398 if (0.5 == (time_t) 0.5) {
400 ** time_t is floating.
402 if (sizeof (time_t) == sizeof (float)) {
403 absolute_min_time
= (time_t) -FLT_MAX
;
404 absolute_max_time
= (time_t) FLT_MAX
;
405 } else if (sizeof (time_t) == sizeof (double)) {
406 absolute_min_time
= (time_t) -DBL_MAX
;
407 absolute_max_time
= (time_t) DBL_MAX
;
409 (void) fprintf(stderr
,
410 _("%s: use of -v on system with floating time_t other than float or double\n"),
414 } else if (0 > (time_t) -1) {
416 ** time_t is signed. Assume overflow wraps around.
426 absolute_max_time
= t
;
428 absolute_min_time
= t
- 1;
429 if (t
< absolute_min_time
)
430 absolute_min_time
= t
;
433 ** time_t is unsigned.
435 absolute_min_time
= 0;
436 absolute_max_time
= absolute_min_time
- 1;
445 register long seconds
;
452 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
454 if (t
> absolute_max_time
- seconds
) {
455 t
= absolute_max_time
;
461 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
462 if (t
< absolute_min_time
+ seconds
) {
463 t
= absolute_min_time
;
473 hunt(char *name
, time_t lot
, time_t hit
)
478 register struct tm
* lotmp
;
480 register struct tm
* tmp
;
481 char loab
[MAX_STRING_LENGTH
];
483 lotmp
= my_localtime(&lot
);
486 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
489 diff
= (long) (hit
- lot
);
498 tmp
= my_localtime(&t
);
501 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
502 (delta(&tm
, &lotm
) == (t
- lot
) &&
503 tm
.tm_isdst
== lotm
.tm_isdst
&&
504 strcmp(abbr(&tm
), loab
) == 0)) {
510 show(name
, lot
, TRUE
);
511 show(name
, hit
, TRUE
);
516 ** Thanks to Paul Eggert for logic used in delta.
524 register long result
;
527 if (newp
->tm_year
< oldp
->tm_year
)
528 return -delta(oldp
, newp
);
530 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
531 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
532 result
+= newp
->tm_yday
- oldp
->tm_yday
;
533 result
*= HOURSPERDAY
;
534 result
+= newp
->tm_hour
- oldp
->tm_hour
;
535 result
*= MINSPERHOUR
;
536 result
+= newp
->tm_min
- oldp
->tm_min
;
537 result
*= SECSPERMIN
;
538 result
+= newp
->tm_sec
- oldp
->tm_sec
;
543 show(char *zone
, time_t t
, int v
)
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
));