1 static char elsieid
[] = "@(#)zdump.c 7.61";
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
, " sec %d", tmp
->tm_sec
);
179 (void) fprintf(stderr
, " min %d", tmp
->tm_min
);
180 (void) fprintf(stderr
, " hour %d", tmp
->tm_hour
);
181 (void) fprintf(stderr
, " mday %d", tmp
->tm_mday
);
182 (void) fprintf(stderr
, " mon %d", tmp
->tm_mon
);
183 (void) fprintf(stderr
, " year %d", tmp
->tm_year
);
184 (void) fprintf(stderr
, " -> ");
185 (void) fprintf(stderr
, tformat(), t
);
186 (void) fprintf(stderr
, "\n");
191 #endif /* !defined TYPECHECK */
201 register char * cutarg
;
202 register long cutloyear
= ZDUMP_LO_YEAR
;
203 register long cuthiyear
= ZDUMP_HI_YEAR
;
204 register time_t cutlotime
;
205 register time_t cuthitime
;
206 register char ** fakeenv
;
212 register struct tm
* tmp
;
213 register struct tm
* newtmp
;
215 INITIALIZE(cutlotime
);
216 INITIALIZE(cuthitime
);
218 (void) setlocale(LC_MESSAGES
, "");
220 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
221 #endif /* defined TEXTDOMAINDIR */
222 (void) textdomain(TZ_DOMAIN
);
223 #endif /* HAVE_GETTEXT */
225 for (i
= 1; i
< argc
; ++i
)
226 if (strcmp(argv
[i
], "--version") == 0) {
227 (void) printf("%s\n", elsieid
);
228 (void) exit(EXIT_SUCCESS
);
232 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
235 else cutarg
= optarg
;
236 if ((c
!= EOF
&& c
!= -1) ||
237 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
238 (void) fprintf(stderr
,
239 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
241 (void) exit(EXIT_FAILURE
);
244 if (cutarg
!= NULL
) {
249 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
251 } else if (sscanf(cutarg
, "%ld,%ld%c",
252 &lo
, &hi
, &dummy
) == 2) {
256 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
258 (void) exit(EXIT_FAILURE
);
262 cutlotime
= yeartot(cutloyear
);
263 cuthitime
= yeartot(cuthiyear
);
267 for (i
= optind
; i
< argc
; ++i
)
268 if (strlen(argv
[i
]) > longest
)
269 longest
= strlen(argv
[i
]);
274 for (i
= 0; environ
[i
] != NULL
; ++i
)
276 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
278 if (fakeenv
== NULL
||
279 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
280 (void) perror(progname
);
281 (void) exit(EXIT_FAILURE
);
284 (void) strcpy(fakeenv
[to
++], "TZ=");
285 for (from
= 0; environ
[from
] != NULL
; ++from
)
286 if (strncmp(environ
[from
], "TZ=", 3) != 0)
287 fakeenv
[to
++] = environ
[from
];
291 for (i
= optind
; i
< argc
; ++i
) {
292 static char buf
[MAX_STRING_LENGTH
];
294 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
296 show(argv
[i
], now
, FALSE
);
299 t
= absolute_min_time
;
300 show(argv
[i
], t
, TRUE
);
301 t
+= SECSPERHOUR
* HOURSPERDAY
;
302 show(argv
[i
], t
, TRUE
);
305 tmp
= my_localtime(&t
);
308 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
313 newt
= t
+ SECSPERHOUR
* 12;
314 if (newt
>= cuthitime
)
318 newtmp
= localtime(&newt
);
321 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
322 (delta(&newtm
, &tm
) != (newt
- t
) ||
323 newtm
.tm_isdst
!= tm
.tm_isdst
||
324 strcmp(abbr(&newtm
), buf
) != 0)) {
325 newt
= hunt(argv
[i
], t
, newt
);
326 newtmp
= localtime(&newt
);
327 if (newtmp
!= NULL
) {
338 t
= absolute_max_time
;
339 t
-= SECSPERHOUR
* HOURSPERDAY
;
340 show(argv
[i
], t
, TRUE
);
341 t
+= SECSPERHOUR
* HOURSPERDAY
;
342 show(argv
[i
], t
, TRUE
);
344 if (fflush(stdout
) || ferror(stdout
)) {
345 (void) fprintf(stderr
, "%s: ", progname
);
346 (void) perror(_("Error writing standard output"));
347 (void) exit(EXIT_FAILURE
);
350 /* If exit fails to exit... */
357 if (0.5 == (time_t) 0.5) {
359 ** time_t is floating.
361 if (sizeof (time_t) == sizeof (float)) {
362 absolute_min_time
= (time_t) -FLT_MAX
;
363 absolute_max_time
= (time_t) FLT_MAX
;
364 } else if (sizeof (time_t) == sizeof (double)) {
365 absolute_min_time
= (time_t) -DBL_MAX
;
366 absolute_max_time
= (time_t) DBL_MAX
;
368 (void) fprintf(stderr
,
369 _("%s: use of -v on system with floating time_t other than float or double\n"),
371 (void) exit(EXIT_FAILURE
);
373 } else if (0 > (time_t) -1) {
377 register time_t hibit
;
379 for (hibit
= 1; (hibit
* 2) != 0; hibit
*= 2)
381 absolute_min_time
= hibit
;
382 absolute_max_time
= -(hibit
+ 1);
385 ** time_t is unsigned.
387 absolute_min_time
= 0;
388 absolute_max_time
= absolute_min_time
- 1;
397 register long seconds
;
404 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
406 if (t
> absolute_max_time
- seconds
) {
407 t
= absolute_max_time
;
413 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
414 if (t
< absolute_min_time
+ seconds
) {
415 t
= absolute_min_time
;
433 register struct tm
* lotmp
;
435 register struct tm
* tmp
;
436 char loab
[MAX_STRING_LENGTH
];
438 lotmp
= my_localtime(&lot
);
441 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
444 diff
= (long) (hit
- lot
);
453 tmp
= my_localtime(&t
);
456 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
457 (delta(&tm
, &lotm
) == (t
- lot
) &&
458 tm
.tm_isdst
== lotm
.tm_isdst
&&
459 strcmp(abbr(&tm
), loab
) == 0)) {
465 show(name
, lot
, TRUE
);
466 show(name
, hit
, TRUE
);
471 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
479 register long result
;
482 if (newp
->tm_year
< oldp
->tm_year
)
483 return -delta(oldp
, newp
);
485 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
486 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
487 result
+= newp
->tm_yday
- oldp
->tm_yday
;
488 result
*= HOURSPERDAY
;
489 result
+= newp
->tm_hour
- oldp
->tm_hour
;
490 result
*= MINSPERHOUR
;
491 result
+= newp
->tm_min
- oldp
->tm_min
;
492 result
*= SECSPERMIN
;
493 result
+= newp
->tm_sec
- oldp
->tm_sec
;
503 register struct tm
* tmp
;
505 (void) printf("%-*s ", (int) longest
, zone
);
509 (void) printf(tformat(), t
);
512 (void) printf(" UTC");
514 (void) printf(" = ");
516 tmp
= my_localtime(&t
);
519 if (*abbr(tmp
) != '\0')
520 (void) printf(" %s", abbr(tmp
));
522 (void) printf(" isdst=%d", tmp
->tm_isdst
);
524 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
525 #endif /* defined TM_GMTOFF */
535 register char * result
;
538 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
540 result
= tzname
[tmp
->tm_isdst
];
541 return (result
== NULL
) ? &nada
: result
;
545 ** The code below can fail on certain theoretical systems;
546 ** it works on all known real-world systems as of 2004-12-30.
552 if (0.5 == (time_t) 0.5) { /* floating */
553 if (sizeof (time_t) > sizeof (double))
557 if (0 > (time_t) -1) { /* signed */
558 if (sizeof (time_t) > sizeof (long))
560 if (sizeof (time_t) > sizeof (int))
564 if (sizeof (time_t) > sizeof (unsigned long))
566 if (sizeof (time_t) > sizeof (unsigned int))
573 register const struct tm
* timeptr
;
575 static const char wday_name
[][3] = {
576 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
578 static const char mon_name
[][3] = {
579 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
580 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
582 register const char * wn
;
583 register const char * mn
;
587 if (timeptr
== NULL
) {
588 (void) printf("NULL");
592 ** The packaged versions of localtime and gmtime never put out-of-range
593 ** values in tm_wday or tm_mon, but since this code might be compiled
594 ** with other (perhaps experimental) versions, paranoia is in order.
596 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
597 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
599 else wn
= wday_name
[timeptr
->tm_wday
];
600 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
601 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
603 else mn
= mon_name
[timeptr
->tm_mon
];
604 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
606 timeptr
->tm_mday
, timeptr
->tm_hour
,
607 timeptr
->tm_min
, timeptr
->tm_sec
);
609 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
610 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
613 if (trail
< 0 && lead
> 0) {
616 } else if (lead
< 0 && trail
> 0) {
621 (void) printf("%d", trail
);
622 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));