1 static char elsieid
[] = "@(#)zdump.c 7.64";
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 */
17 #define ZDUMP_LO_YEAR (-500)
18 #endif /* !defined ZDUMP_LO_YEAR */
21 #define ZDUMP_HI_YEAR 2500
22 #endif /* !defined ZDUMP_HI_YEAR */
24 #ifndef MAX_STRING_LENGTH
25 #define MAX_STRING_LENGTH 1024
26 #endif /* !defined MAX_STRING_LENGTH */
30 #endif /* !defined TRUE */
34 #endif /* !defined FALSE */
37 #define EXIT_SUCCESS 0
38 #endif /* !defined EXIT_SUCCESS */
41 #define EXIT_FAILURE 1
42 #endif /* !defined EXIT_FAILURE */
46 #endif /* !defined SECSPERMIN */
49 #define MINSPERHOUR 60
50 #endif /* !defined MINSPERHOUR */
53 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
54 #endif /* !defined SECSPERHOUR */
57 #define HOURSPERDAY 24
58 #endif /* !defined HOURSPERDAY */
61 #define EPOCH_YEAR 1970
62 #endif /* !defined EPOCH_YEAR */
65 #define TM_YEAR_BASE 1900
66 #endif /* !defined TM_YEAR_BASE */
69 #define DAYSPERNYEAR 365
70 #endif /* !defined DAYSPERNYEAR */
73 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
74 #endif /* !defined isleap */
78 ** See tzfile.h for details on isleap_sum.
80 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
81 #endif /* !defined isleap_sum */
83 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
84 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
85 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
88 #include "locale.h" /* for setlocale */
90 #endif /* HAVE_GETTEXT */
95 #else /* !defined lint */
98 #endif /* defined __GNUC__ */
99 #endif /* !defined lint */
100 #endif /* !defined GNUC_or_lint */
104 #define INITIALIZE(x) ((x) = 0)
105 #else /* !defined GNUC_or_lint */
106 #define INITIALIZE(x)
107 #endif /* !defined GNUC_or_lint */
108 #endif /* !defined INITIALIZE */
111 ** For the benefit of GNU folk...
112 ** `_(MSGID)' uses the current locale's message library string for MSGID.
113 ** The default is to use gettext if available, and use MSGID otherwise.
118 #define _(msgid) gettext(msgid)
119 #else /* !HAVE_GETTEXT */
120 #define _(msgid) msgid
121 #endif /* !HAVE_GETTEXT */
122 #endif /* !defined _ */
125 #define TZ_DOMAIN "tz"
126 #endif /* !defined TZ_DOMAIN */
131 #else /* !defined __STDC__ */
133 #endif /* !defined __STDC__ */
134 #endif /* !defined P */
136 extern char ** environ
;
137 extern int getopt
P((int argc
, char * const argv
[],
138 const char * options
));
139 extern char * optarg
;
141 extern char * tzname
[2];
143 static time_t absolute_min_time
;
144 static time_t absolute_max_time
;
145 static size_t longest
;
146 static char * progname
;
148 static char * abbr
P((struct tm
* tmp
));
149 static long delta
P((struct tm
* newp
, struct tm
* oldp
));
150 static void dumptime
P((const struct tm
* tmp
));
151 static time_t hunt
P((char * name
, time_t lot
, time_t hit
));
152 static void setabsolutes
P((void));
153 static void show
P((char * zone
, time_t t
, int v
));
154 static const char * tformat
P((void));
155 static time_t yeartot
P((long y
));
158 #define my_localtime localtime
159 #else /* !defined TYPECHECK */
164 register struct tm
* tmp
;
167 if (tp
!= NULL
&& tmp
!= NULL
) {
173 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
174 (void) fflush(stdout
);
175 (void) fprintf(stderr
, "\n%s: ", progname
);
176 (void) fprintf(stderr
, tformat(), *tp
);
177 (void) fprintf(stderr
, " ->");
178 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
179 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
180 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
181 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
182 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
183 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
184 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
185 (void) fprintf(stderr
, " -> ");
186 (void) fprintf(stderr
, tformat(), t
);
187 (void) fprintf(stderr
, "\n");
192 #endif /* !defined TYPECHECK */
202 register char * cutarg
;
203 register long cutloyear
= ZDUMP_LO_YEAR
;
204 register long cuthiyear
= ZDUMP_HI_YEAR
;
205 register time_t cutlotime
;
206 register time_t cuthitime
;
207 register char ** fakeenv
;
213 register struct tm
* tmp
;
214 register struct tm
* newtmp
;
216 INITIALIZE(cutlotime
);
217 INITIALIZE(cuthitime
);
219 (void) setlocale(LC_MESSAGES
, "");
221 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
222 #endif /* defined TEXTDOMAINDIR */
223 (void) textdomain(TZ_DOMAIN
);
224 #endif /* HAVE_GETTEXT */
226 for (i
= 1; i
< argc
; ++i
)
227 if (strcmp(argv
[i
], "--version") == 0) {
228 (void) printf("%s\n", elsieid
);
229 (void) exit(EXIT_SUCCESS
);
233 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
236 else cutarg
= optarg
;
237 if ((c
!= EOF
&& c
!= -1) ||
238 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
239 (void) fprintf(stderr
,
240 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
242 (void) exit(EXIT_FAILURE
);
245 if (cutarg
!= NULL
) {
250 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
252 } else if (sscanf(cutarg
, "%ld,%ld%c",
253 &lo
, &hi
, &dummy
) == 2) {
257 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
259 (void) exit(EXIT_FAILURE
);
263 cutlotime
= yeartot(cutloyear
);
264 cuthitime
= yeartot(cuthiyear
);
268 for (i
= optind
; i
< argc
; ++i
)
269 if (strlen(argv
[i
]) > longest
)
270 longest
= strlen(argv
[i
]);
275 for (i
= 0; environ
[i
] != NULL
; ++i
)
277 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
279 if (fakeenv
== NULL
||
280 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
281 (void) perror(progname
);
282 (void) exit(EXIT_FAILURE
);
285 (void) strcpy(fakeenv
[to
++], "TZ=");
286 for (from
= 0; environ
[from
] != NULL
; ++from
)
287 if (strncmp(environ
[from
], "TZ=", 3) != 0)
288 fakeenv
[to
++] = environ
[from
];
292 for (i
= optind
; i
< argc
; ++i
) {
293 static char buf
[MAX_STRING_LENGTH
];
295 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
297 show(argv
[i
], now
, FALSE
);
300 t
= absolute_min_time
;
301 show(argv
[i
], t
, TRUE
);
302 t
+= SECSPERHOUR
* HOURSPERDAY
;
303 show(argv
[i
], t
, TRUE
);
306 tmp
= my_localtime(&t
);
309 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
314 newt
= t
+ SECSPERHOUR
* 12;
315 if (newt
>= cuthitime
)
319 newtmp
= localtime(&newt
);
322 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
323 (delta(&newtm
, &tm
) != (newt
- t
) ||
324 newtm
.tm_isdst
!= tm
.tm_isdst
||
325 strcmp(abbr(&newtm
), buf
) != 0)) {
326 newt
= hunt(argv
[i
], t
, newt
);
327 newtmp
= localtime(&newt
);
328 if (newtmp
!= NULL
) {
339 t
= absolute_max_time
;
340 t
-= SECSPERHOUR
* HOURSPERDAY
;
341 show(argv
[i
], t
, TRUE
);
342 t
+= SECSPERHOUR
* HOURSPERDAY
;
343 show(argv
[i
], t
, TRUE
);
345 if (fflush(stdout
) || ferror(stdout
)) {
346 (void) fprintf(stderr
, "%s: ", progname
);
347 (void) perror(_("Error writing standard output"));
348 (void) exit(EXIT_FAILURE
);
351 /* If exit fails to exit... */
358 if (0.5 == (time_t) 0.5) {
360 ** time_t is floating.
362 if (sizeof (time_t) == sizeof (float)) {
363 absolute_min_time
= (time_t) -FLT_MAX
;
364 absolute_max_time
= (time_t) FLT_MAX
;
365 } else if (sizeof (time_t) == sizeof (double)) {
366 absolute_min_time
= (time_t) -DBL_MAX
;
367 absolute_max_time
= (time_t) DBL_MAX
;
369 (void) fprintf(stderr
,
370 _("%s: use of -v on system with floating time_t other than float or double\n"),
372 (void) exit(EXIT_FAILURE
);
374 } else if (0 > (time_t) -1) {
378 register time_t hibit
;
380 for (hibit
= 1; (hibit
* 2) != 0; hibit
*= 2)
382 absolute_min_time
= hibit
;
383 absolute_max_time
= -(hibit
+ 1);
386 ** time_t is unsigned.
388 absolute_min_time
= 0;
389 absolute_max_time
= absolute_min_time
- 1;
398 register long seconds
;
405 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
407 if (t
> absolute_max_time
- seconds
) {
408 t
= absolute_max_time
;
414 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
415 if (t
< absolute_min_time
+ seconds
) {
416 t
= absolute_min_time
;
434 register struct tm
* lotmp
;
436 register struct tm
* tmp
;
437 char loab
[MAX_STRING_LENGTH
];
439 lotmp
= my_localtime(&lot
);
442 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
445 diff
= (long) (hit
- lot
);
454 tmp
= my_localtime(&t
);
457 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
458 (delta(&tm
, &lotm
) == (t
- lot
) &&
459 tm
.tm_isdst
== lotm
.tm_isdst
&&
460 strcmp(abbr(&tm
), loab
) == 0)) {
466 show(name
, lot
, TRUE
);
467 show(name
, hit
, TRUE
);
472 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
480 register long result
;
483 if (newp
->tm_year
< oldp
->tm_year
)
484 return -delta(oldp
, newp
);
486 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
487 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
488 result
+= newp
->tm_yday
- oldp
->tm_yday
;
489 result
*= HOURSPERDAY
;
490 result
+= newp
->tm_hour
- oldp
->tm_hour
;
491 result
*= MINSPERHOUR
;
492 result
+= newp
->tm_min
- oldp
->tm_min
;
493 result
*= SECSPERMIN
;
494 result
+= newp
->tm_sec
- oldp
->tm_sec
;
504 register struct tm
* tmp
;
506 (void) printf("%-*s ", (int) longest
, zone
);
510 (void) printf(tformat(), t
);
513 (void) printf(" UTC");
515 (void) printf(" = ");
517 tmp
= my_localtime(&t
);
520 if (*abbr(tmp
) != '\0')
521 (void) printf(" %s", abbr(tmp
));
523 (void) printf(" isdst=%d", tmp
->tm_isdst
);
525 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
526 #endif /* defined TM_GMTOFF */
536 register char * result
;
539 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
541 result
= tzname
[tmp
->tm_isdst
];
542 return (result
== NULL
) ? &nada
: result
;
546 ** The code below can fail on certain theoretical systems;
547 ** it works on all known real-world systems as of 2004-12-30.
553 if (0.5 == (time_t) 0.5) { /* floating */
554 if (sizeof (time_t) > sizeof (double))
558 if (0 > (time_t) -1) { /* signed */
559 if (sizeof (time_t) > sizeof (long))
561 if (sizeof (time_t) > sizeof (int))
565 if (sizeof (time_t) > sizeof (unsigned long))
567 if (sizeof (time_t) > sizeof (unsigned int))
574 register const struct tm
* timeptr
;
576 static const char wday_name
[][3] = {
577 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
579 static const char mon_name
[][3] = {
580 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
581 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
583 register const char * wn
;
584 register const char * mn
;
588 if (timeptr
== NULL
) {
589 (void) printf("NULL");
593 ** The packaged versions of localtime and gmtime never put out-of-range
594 ** values in tm_wday or tm_mon, but since this code might be compiled
595 ** with other (perhaps experimental) versions, paranoia is in order.
597 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
598 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
600 else wn
= wday_name
[timeptr
->tm_wday
];
601 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
602 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
604 else mn
= mon_name
[timeptr
->tm_mon
];
605 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
607 timeptr
->tm_mday
, timeptr
->tm_hour
,
608 timeptr
->tm_min
, timeptr
->tm_sec
);
610 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
611 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
614 if (trail
< 0 && lead
> 0) {
617 } else if (lead
< 0 && trail
> 0) {
622 (void) printf("%d", trail
);
623 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));