2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
6 static char elsieid
[] = "@(#)zdump.c 8.9";
9 ** This code has been made independent of the rest of the time
10 ** conversion package to increase confidence in the verification it provides.
11 ** You can use this code to help in verifying other implementations.
14 #include "stdio.h" /* for stdout, stderr, perror */
15 #include "string.h" /* for strcpy */
16 #include "sys/types.h" /* for time_t */
17 #include "time.h" /* for struct tm */
18 #include "stdlib.h" /* for exit, malloc, atoi */
19 #include "float.h" /* for FLT_MAX and DBL_MAX */
20 #include "ctype.h" /* for isalpha et al. */
23 #endif /* !defined isascii */
26 #define ZDUMP_LO_YEAR (-500)
27 #endif /* !defined ZDUMP_LO_YEAR */
30 #define ZDUMP_HI_YEAR 2500
31 #endif /* !defined ZDUMP_HI_YEAR */
33 #ifndef MAX_STRING_LENGTH
34 #define MAX_STRING_LENGTH 1024
35 #endif /* !defined MAX_STRING_LENGTH */
39 #endif /* !defined TRUE */
43 #endif /* !defined FALSE */
46 #define EXIT_SUCCESS 0
47 #endif /* !defined EXIT_SUCCESS */
50 #define EXIT_FAILURE 1
51 #endif /* !defined EXIT_FAILURE */
55 #endif /* !defined SECSPERMIN */
58 #define MINSPERHOUR 60
59 #endif /* !defined MINSPERHOUR */
62 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
63 #endif /* !defined SECSPERHOUR */
66 #define HOURSPERDAY 24
67 #endif /* !defined HOURSPERDAY */
70 #define EPOCH_YEAR 1970
71 #endif /* !defined EPOCH_YEAR */
74 #define TM_YEAR_BASE 1900
75 #endif /* !defined TM_YEAR_BASE */
78 #define DAYSPERNYEAR 365
79 #endif /* !defined DAYSPERNYEAR */
82 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
83 #endif /* !defined isleap */
87 ** See tzfile.h for details on isleap_sum.
89 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
90 #endif /* !defined isleap_sum */
92 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
93 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
94 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
97 #define HAVE_GETTEXT 0
100 #include "locale.h" /* for setlocale */
102 #endif /* HAVE_GETTEXT */
107 #else /* !defined lint */
110 #endif /* defined __GNUC__ */
111 #endif /* !defined lint */
112 #endif /* !defined GNUC_or_lint */
116 #define INITIALIZE(x) ((x) = 0)
117 #else /* !defined GNUC_or_lint */
118 #define INITIALIZE(x)
119 #endif /* !defined GNUC_or_lint */
120 #endif /* !defined INITIALIZE */
123 ** For the benefit of GNU folk...
124 ** `_(MSGID)' uses the current locale's message library string for MSGID.
125 ** The default is to use gettext if available, and use MSGID otherwise.
130 #define _(msgid) gettext(msgid)
131 #else /* !HAVE_GETTEXT */
132 #define _(msgid) msgid
133 #endif /* !HAVE_GETTEXT */
134 #endif /* !defined _ */
137 #define TZ_DOMAIN "tz"
138 #endif /* !defined TZ_DOMAIN */
140 extern char ** environ
;
141 extern int getopt(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(struct tm
* tmp
);
154 static void abbrok(const char * abbrp
, const char * zone
);
155 static long delta(struct tm
* newp
, struct tm
* oldp
);
156 static void dumptime(const struct tm
* tmp
);
157 static time_t hunt(char * name
, time_t lot
, time_t hit
);
158 static void setabsolutes(void);
159 static void show(char * zone
, time_t t
, int v
);
160 static const char * tformat(void);
161 static time_t yeartot(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 usage(const char *progname
, FILE *stream
, int status
)
241 (void) fprintf(stream
,
242 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
244 Report bugs to tz@elsie.nci.nih.gov.\n"),
257 register char * cutarg
;
258 register long cutloyear
= ZDUMP_LO_YEAR
;
259 register long cuthiyear
= ZDUMP_HI_YEAR
;
260 register time_t cutlotime
;
261 register time_t cuthitime
;
262 register char ** fakeenv
;
268 register struct tm
* tmp
;
269 register struct tm
* newtmp
;
271 INITIALIZE(cutlotime
);
272 INITIALIZE(cuthitime
);
274 (void) setlocale(LC_ALL
, "");
276 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
277 #endif /* defined TEXTDOMAINDIR */
278 (void) textdomain(TZ_DOMAIN
);
279 #endif /* HAVE_GETTEXT */
281 for (i
= 1; i
< argc
; ++i
)
282 if (strcmp(argv
[i
], "--version") == 0) {
283 (void) printf("%s\n", elsieid
);
285 } else if (strcmp(argv
[i
], "--help") == 0) {
286 usage(progname
, stdout
, EXIT_SUCCESS
);
290 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
293 else cutarg
= optarg
;
294 if ((c
!= EOF
&& c
!= -1) ||
295 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
296 usage(progname
, stderr
, EXIT_FAILURE
);
299 if (cutarg
!= NULL
) {
304 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
306 } else if (sscanf(cutarg
, "%ld,%ld%c",
307 &lo
, &hi
, &dummy
) == 2) {
311 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
317 cutlotime
= yeartot(cutloyear
);
318 cuthitime
= yeartot(cuthiyear
);
322 for (i
= optind
; i
< argc
; ++i
)
323 if (strlen(argv
[i
]) > longest
)
324 longest
= strlen(argv
[i
]);
329 for (i
= 0; environ
[i
] != NULL
; ++i
)
331 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
333 if (fakeenv
== NULL
||
334 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
335 (void) perror(progname
);
339 (void) strcpy(fakeenv
[to
++], "TZ=");
340 for (from
= 0; environ
[from
] != NULL
; ++from
)
341 if (strncmp(environ
[from
], "TZ=", 3) != 0)
342 fakeenv
[to
++] = environ
[from
];
346 for (i
= optind
; i
< argc
; ++i
) {
347 static char buf
[MAX_STRING_LENGTH
];
349 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
351 show(argv
[i
], now
, FALSE
);
355 t
= absolute_min_time
;
356 show(argv
[i
], t
, TRUE
);
357 t
+= SECSPERHOUR
* HOURSPERDAY
;
358 show(argv
[i
], t
, TRUE
);
361 tmp
= my_localtime(&t
);
364 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
367 if (t
>= cuthitime
|| t
>= cuthitime
- SECSPERHOUR
* 12)
369 newt
= t
+ SECSPERHOUR
* 12;
370 newtmp
= localtime(&newt
);
373 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
374 (delta(&newtm
, &tm
) != (newt
- t
) ||
375 newtm
.tm_isdst
!= tm
.tm_isdst
||
376 strcmp(abbr(&newtm
), buf
) != 0)) {
377 newt
= hunt(argv
[i
], t
, newt
);
378 newtmp
= localtime(&newt
);
379 if (newtmp
!= NULL
) {
390 t
= absolute_max_time
;
391 t
-= SECSPERHOUR
* HOURSPERDAY
;
392 show(argv
[i
], t
, TRUE
);
393 t
+= SECSPERHOUR
* HOURSPERDAY
;
394 show(argv
[i
], t
, TRUE
);
396 if (fflush(stdout
) || ferror(stdout
)) {
397 (void) fprintf(stderr
, "%s: ", progname
);
398 (void) perror(_("Error writing to standard output"));
402 /* If exit fails to exit... */
409 if (0.5 == (time_t) 0.5) {
411 ** time_t is floating.
413 if (sizeof (time_t) == sizeof (float)) {
414 absolute_min_time
= (time_t) -FLT_MAX
;
415 absolute_max_time
= (time_t) FLT_MAX
;
416 } else if (sizeof (time_t) == sizeof (double)) {
417 absolute_min_time
= (time_t) -DBL_MAX
;
418 absolute_max_time
= (time_t) DBL_MAX
;
420 (void) fprintf(stderr
,
421 _("%s: use of -v on system with floating time_t other than float or double\n"),
425 } else if (0 > (time_t) -1) {
427 ** time_t is signed. Assume overflow wraps around.
437 absolute_max_time
= t
;
439 absolute_min_time
= t
- 1;
440 if (t
< absolute_min_time
)
441 absolute_min_time
= t
;
444 ** time_t is unsigned.
446 absolute_min_time
= 0;
447 absolute_max_time
= absolute_min_time
- 1;
456 register long seconds
;
463 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
465 if (t
> absolute_max_time
- seconds
) {
466 t
= absolute_max_time
;
472 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
473 if (t
< absolute_min_time
+ seconds
) {
474 t
= absolute_min_time
;
484 hunt(char *name
, time_t lot
, time_t hit
)
489 register struct tm
* lotmp
;
491 register struct tm
* tmp
;
492 char loab
[MAX_STRING_LENGTH
];
494 lotmp
= my_localtime(&lot
);
497 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
500 diff
= (long) (hit
- lot
);
509 tmp
= my_localtime(&t
);
512 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
513 (delta(&tm
, &lotm
) == (t
- lot
) &&
514 tm
.tm_isdst
== lotm
.tm_isdst
&&
515 strcmp(abbr(&tm
), loab
) == 0)) {
521 show(name
, lot
, TRUE
);
522 show(name
, hit
, TRUE
);
527 ** Thanks to Paul Eggert for logic used in delta.
535 register long result
;
538 if (newp
->tm_year
< oldp
->tm_year
)
539 return -delta(oldp
, newp
);
541 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
542 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
543 result
+= newp
->tm_yday
- oldp
->tm_yday
;
544 result
*= HOURSPERDAY
;
545 result
+= newp
->tm_hour
- oldp
->tm_hour
;
546 result
*= MINSPERHOUR
;
547 result
+= newp
->tm_min
- oldp
->tm_min
;
548 result
*= SECSPERMIN
;
549 result
+= newp
->tm_sec
- oldp
->tm_sec
;
554 show(char *zone
, time_t t
, int v
)
556 register struct tm
* tmp
;
558 (void) printf("%-*s ", (int) longest
, zone
);
562 (void) printf(tformat(), t
);
565 (void) printf(" UTC");
567 (void) printf(" = ");
569 tmp
= my_localtime(&t
);
572 if (*abbr(tmp
) != '\0')
573 (void) printf(" %s", abbr(tmp
));
575 (void) printf(" isdst=%d", tmp
->tm_isdst
);
577 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
578 #endif /* defined TM_GMTOFF */
582 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
583 abbrok(abbr(tmp
), zone
);
590 register char * result
;
593 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
595 result
= tzname
[tmp
->tm_isdst
];
596 return (result
== NULL
) ? &nada
: result
;
600 ** The code below can fail on certain theoretical systems;
601 ** it works on all known real-world systems as of 2004-12-30.
607 if (0.5 == (time_t) 0.5) { /* floating */
608 if (sizeof (time_t) > sizeof (double))
612 if (0 > (time_t) -1) { /* signed */
613 if (sizeof (time_t) > sizeof (long))
615 if (sizeof (time_t) > sizeof (int))
619 if (sizeof (time_t) > sizeof (unsigned long))
621 if (sizeof (time_t) > sizeof (unsigned int))
628 register const struct tm
* timeptr
;
630 static const char wday_name
[][3] = {
631 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
633 static const char mon_name
[][3] = {
634 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
635 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
637 register const char * wn
;
638 register const char * mn
;
642 if (timeptr
== NULL
) {
643 (void) printf("NULL");
647 ** The packaged versions of localtime and gmtime never put out-of-range
648 ** values in tm_wday or tm_mon, but since this code might be compiled
649 ** with other (perhaps experimental) versions, paranoia is in order.
651 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
652 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
654 else wn
= wday_name
[timeptr
->tm_wday
];
655 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
656 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
658 else mn
= mon_name
[timeptr
->tm_mon
];
659 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
661 timeptr
->tm_mday
, timeptr
->tm_hour
,
662 timeptr
->tm_min
, timeptr
->tm_sec
);
664 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
665 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
668 if (trail
< 0 && lead
> 0) {
671 } else if (lead
< 0 && trail
> 0) {
676 (void) printf("%d", trail
);
677 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));