1 /* $NetBSD: zdump.c,v 1.16 2006/12/04 17:24:40 kleink Exp $ */
3 ** This file is in the public domain, so clarified as of
4 ** 2009-05-17 by Arthur David Olson.
10 __RCSID("$NetBSD: zdump.c,v 1.16 2006/12/04 17:24:40 kleink Exp $");
11 #endif /* !defined NOID */
12 #endif /* !defined lint */
14 static char elsieid
[] = "@(#)zdump.c 8.9";
17 ** This code has been made independent of the rest of the time
18 ** conversion package to increase confidence in the verification it provides.
19 ** You can use this code to help in verifying other implementations.
22 #include "stdio.h" /* for stdout, stderr */
23 #include "string.h" /* for strcpy */
24 #include "sys/types.h" /* for time_t */
25 #include "time.h" /* for struct tm */
26 #include "stdlib.h" /* for exit, malloc, atoi */
28 #include "float.h" /* for FLT_MAX and DBL_MAX */
29 #include "ctype.h" /* for isalpha et al. */
32 #endif /* !defined isascii */
35 #define ZDUMP_LO_YEAR (-500)
36 #endif /* !defined ZDUMP_LO_YEAR */
39 #define ZDUMP_HI_YEAR 2500
40 #endif /* !defined ZDUMP_HI_YEAR */
42 #ifndef MAX_STRING_LENGTH
43 #define MAX_STRING_LENGTH 1024
44 #endif /* !defined MAX_STRING_LENGTH */
48 #endif /* !defined TRUE */
52 #endif /* !defined FALSE */
55 #define EXIT_SUCCESS 0
56 #endif /* !defined EXIT_SUCCESS */
59 #define EXIT_FAILURE 1
60 #endif /* !defined EXIT_FAILURE */
64 #endif /* !defined SECSPERMIN */
67 #define MINSPERHOUR 60
68 #endif /* !defined MINSPERHOUR */
71 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
72 #endif /* !defined SECSPERHOUR */
75 #define HOURSPERDAY 24
76 #endif /* !defined HOURSPERDAY */
79 #define EPOCH_YEAR 1970
80 #endif /* !defined EPOCH_YEAR */
83 #define TM_YEAR_BASE 1900
84 #endif /* !defined TM_YEAR_BASE */
87 #define DAYSPERNYEAR 365
88 #endif /* !defined DAYSPERNYEAR */
91 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
92 #endif /* !defined isleap */
96 ** See tzfile.h for details on isleap_sum.
98 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
99 #endif /* !defined isleap_sum */
101 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
102 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
103 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
106 #define HAVE_GETTEXT 0
109 #include "locale.h" /* for setlocale */
111 #endif /* HAVE_GETTEXT */
116 #else /* !defined lint */
119 #endif /* defined __GNUC__ */
120 #endif /* !defined lint */
121 #endif /* !defined GNUC_or_lint */
125 #define INITIALIZE(x) ((x) = 0)
126 #else /* !defined GNUC_or_lint */
127 #define INITIALIZE(x)
128 #endif /* !defined GNUC_or_lint */
129 #endif /* !defined INITIALIZE */
132 ** For the benefit of GNU folk...
133 ** `_(MSGID)' uses the current locale's message library string for MSGID.
134 ** The default is to use gettext if available, and use MSGID otherwise.
139 #define _(msgid) gettext(msgid)
140 #else /* !HAVE_GETTEXT */
141 #define _(msgid) __UNCONST(msgid)
142 #endif /* !HAVE_GETTEXT */
143 #endif /* !defined _ */
146 #define TZ_DOMAIN "tz"
147 #endif /* !defined TZ_DOMAIN */
149 extern char ** environ
;
150 extern int getopt(int argc
, char * const argv
[],
151 const char * options
);
152 extern char * optarg
;
155 static time_t absolute_min_time
;
156 static time_t absolute_max_time
;
157 static size_t longest
;
158 static char * progname
;
161 static const char * abbr(struct tm
* tmp
);
162 static void abbrok(const char * abbrp
, const char * zone
);
163 static long delta(struct tm
* newp
, struct tm
* oldp
);
164 static void dumptime(const struct tm
* tmp
);
165 static time_t hunt(char * name
, time_t lot
, time_t hit
);
166 int main(int, char **);
167 static void setabsolutes(void);
168 static void show(char * zone
, time_t t
, int v
);
169 static const char * tformat(void);
170 static time_t yeartot(long y
);
173 #define my_localtime localtime
174 #else /* !defined TYPECHECK */
179 register struct tm
* tmp
;
182 if (tp
!= NULL
&& tmp
!= NULL
) {
188 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
189 (void) fflush(stdout
);
190 (void) fprintf(stderr
, "\n%s: ", progname
);
191 (void) fprintf(stderr
, tformat(), *tp
);
192 (void) fprintf(stderr
, " ->");
193 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
194 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
195 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
196 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
197 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
198 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
199 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
200 (void) fprintf(stderr
, " -> ");
201 (void) fprintf(stderr
, tformat(), t
);
202 (void) fprintf(stderr
, "\n");
207 #endif /* !defined TYPECHECK */
211 const char * const abbrp
;
212 const char * const zone
;
214 register const char * cp
;
221 while (isascii((unsigned char) *cp
) && isalpha((unsigned char) *cp
))
224 wp
= _("lacks alphabetic at start");
225 else if (cp
- abbrp
< 3)
226 wp
= _("has fewer than 3 alphabetics");
227 else if (cp
- abbrp
> 6)
228 wp
= _("has more than 6 alphabetics");
229 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
231 if (isascii((unsigned char) *cp
) &&
232 isdigit((unsigned char) *cp
))
233 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
236 wp
= _("differs from POSIX standard");
240 (void) fflush(stdout
);
241 (void) fprintf(stderr
,
242 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
243 progname
, zone
, abbrp
, wp
);
248 usage(const char *xprogname
, FILE *stream
, int status
)
250 (void) fprintf(stream
,
251 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
253 Report bugs to tz@elsie.nci.nih.gov.\n"),
254 xprogname
, xprogname
);
266 register char * cutarg
;
267 register long cutloyear
= ZDUMP_LO_YEAR
;
268 register long cuthiyear
= ZDUMP_HI_YEAR
;
269 register time_t cutlotime
;
270 register time_t cuthitime
;
271 register char ** fakeenv
;
277 register struct tm
* tmp
;
278 register struct tm
* newtmp
;
280 INITIALIZE(cutlotime
);
281 INITIALIZE(cuthitime
);
283 (void) setlocale(LC_ALL
, "");
285 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
286 #endif /* defined TEXTDOMAINDIR */
287 (void) textdomain(TZ_DOMAIN
);
288 #endif /* HAVE_GETTEXT */
290 for (i
= 1; i
< argc
; ++i
)
291 if (strcmp(argv
[i
], "--version") == 0) {
292 (void) printf("%s\n", elsieid
);
294 } else if (strcmp(argv
[i
], "--help") == 0) {
295 usage(progname
, stdout
, EXIT_SUCCESS
);
299 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
302 else cutarg
= optarg
;
303 if ((c
!= EOF
&& c
!= -1) ||
304 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
305 usage(progname
, stderr
, EXIT_FAILURE
);
308 if (cutarg
!= NULL
) {
313 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
315 } else if (sscanf(cutarg
, "%ld,%ld%c",
316 &lo
, &hi
, &dummy
) == 2) {
320 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
326 cutlotime
= yeartot(cutloyear
);
327 cuthitime
= yeartot(cuthiyear
);
331 for (i
= optind
; i
< argc
; ++i
)
332 if (strlen(argv
[i
]) > longest
)
333 longest
= strlen(argv
[i
]);
338 for (i
= 0; environ
[i
] != NULL
; ++i
)
340 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
342 if (fakeenv
== NULL
||
343 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
344 err(EXIT_FAILURE
, "Can't allocated %zu bytes",
348 (void)strcpy(fakeenv
[to
++], "TZ="); /* XXX strcpy is safe */
349 for (from
= 0; environ
[from
] != NULL
; ++from
)
350 if (strncmp(environ
[from
], "TZ=", 3) != 0)
351 fakeenv
[to
++] = environ
[from
];
355 for (i
= optind
; i
< argc
; ++i
) {
356 static char buf
[MAX_STRING_LENGTH
];
358 (void) strcpy(&fakeenv
[0][3], argv
[i
]); /* XXX strcpy is safe */
360 show(argv
[i
], now
, FALSE
);
364 t
= absolute_min_time
;
365 show(argv
[i
], t
, TRUE
);
366 t
+= SECSPERHOUR
* HOURSPERDAY
;
367 show(argv
[i
], t
, TRUE
);
370 tmp
= my_localtime(&t
);
373 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
376 if (t
>= cuthitime
|| t
>= cuthitime
- SECSPERHOUR
* 12)
378 newt
= t
+ SECSPERHOUR
* 12;
379 newtmp
= localtime(&newt
);
382 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
383 (delta(&newtm
, &tm
) != (newt
- t
) ||
384 newtm
.tm_isdst
!= tm
.tm_isdst
||
385 strcmp(abbr(&newtm
), buf
) != 0)) {
386 newt
= hunt(argv
[i
], t
, newt
);
387 newtmp
= localtime(&newt
);
388 if (newtmp
!= NULL
) {
399 t
= absolute_max_time
;
400 t
-= SECSPERHOUR
* HOURSPERDAY
;
401 show(argv
[i
], t
, TRUE
);
402 t
+= SECSPERHOUR
* HOURSPERDAY
;
403 show(argv
[i
], t
, TRUE
);
405 if (fflush(stdout
) || ferror(stdout
)) {
406 err(EXIT_FAILURE
, _("Error writing standard output"));
409 /* If exit fails to exit... */
416 if (0.5 == (time_t) 0.5) {
418 ** time_t is floating.
420 if (sizeof (time_t) == sizeof (float)) {
421 absolute_min_time
= (time_t) -FLT_MAX
;
422 absolute_max_time
= (time_t) FLT_MAX
;
423 } else if (sizeof (time_t) == sizeof (double)) {
424 absolute_min_time
= (time_t) -DBL_MAX
;
425 absolute_max_time
= (time_t) DBL_MAX
;
427 (void) fprintf(stderr
,
428 _("%s: use of -v on system with floating time_t other than float or double\n"),
432 } else if (0 > (time_t) -1) {
434 ** time_t is signed. Assume overflow wraps around.
444 absolute_max_time
= t
;
446 absolute_min_time
= t
- 1;
447 if (t
< absolute_min_time
)
448 absolute_min_time
= t
;
451 ** time_t is unsigned.
453 absolute_min_time
= 0;
454 absolute_max_time
= absolute_min_time
- 1;
463 register long seconds
;
470 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
472 if (t
> absolute_max_time
- seconds
) {
473 t
= absolute_max_time
;
479 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
480 if (t
< absolute_min_time
+ seconds
) {
481 t
= absolute_min_time
;
491 hunt(char *name
, time_t lot
, time_t hit
)
496 register struct tm
* lotmp
;
498 register struct tm
* tmp
;
499 char loab
[MAX_STRING_LENGTH
];
501 lotmp
= my_localtime(&lot
);
504 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
507 diff
= (long) (hit
- lot
);
516 tmp
= my_localtime(&t
);
519 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
520 (delta(&tm
, &lotm
) == (t
- lot
) &&
521 tm
.tm_isdst
== lotm
.tm_isdst
&&
522 strcmp(abbr(&tm
), loab
) == 0)) {
528 show(name
, lot
, TRUE
);
529 show(name
, hit
, TRUE
);
534 ** Thanks to Paul Eggert for logic used in delta.
542 register long result
;
545 if (newp
->tm_year
< oldp
->tm_year
)
546 return -delta(oldp
, newp
);
548 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
549 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
550 result
+= newp
->tm_yday
- oldp
->tm_yday
;
551 result
*= HOURSPERDAY
;
552 result
+= newp
->tm_hour
- oldp
->tm_hour
;
553 result
*= MINSPERHOUR
;
554 result
+= newp
->tm_min
- oldp
->tm_min
;
555 result
*= SECSPERMIN
;
556 result
+= newp
->tm_sec
- oldp
->tm_sec
;
561 show(char *zone
, time_t t
, int v
)
563 register struct tm
* tmp
;
565 (void) printf("%-*s ", (int) longest
, zone
);
569 (void) printf(tformat(), t
);
572 (void) printf(" UTC");
574 (void) printf(" = ");
576 tmp
= my_localtime(&t
);
579 if (*abbr(tmp
) != '\0')
580 (void) printf(" %s", abbr(tmp
));
582 (void) printf(" isdst=%d", tmp
->tm_isdst
);
584 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
585 #endif /* defined TM_GMTOFF */
589 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
590 abbrok(abbr(tmp
), zone
);
597 register const char * result
;
598 static const char nada
;
600 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
602 result
= tzname
[tmp
->tm_isdst
];
603 return (result
== NULL
) ? &nada
: result
;
607 ** The code below can fail on certain theoretical systems;
608 ** it works on all known real-world systems as of 2004-12-30.
614 if (0.5 == (time_t) 0.5) { /* floating */
615 if (sizeof (time_t) > sizeof (double))
619 if (0 > (time_t) -1) { /* signed */
620 if (sizeof (time_t) > sizeof (long))
622 if (sizeof (time_t) > sizeof (int))
626 if (sizeof (time_t) > sizeof (unsigned long))
628 if (sizeof (time_t) > sizeof (unsigned int))
635 register const struct tm
* timeptr
;
637 static const char wday_name
[][3] = {
638 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
640 static const char mon_name
[][3] = {
641 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
642 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
644 register const char * wn
;
645 register const char * mn
;
649 if (timeptr
== NULL
) {
650 (void) printf("NULL");
654 ** The packaged versions of localtime and gmtime never put out-of-range
655 ** values in tm_wday or tm_mon, but since this code might be compiled
656 ** with other (perhaps experimental) versions, paranoia is in order.
658 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
659 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
661 else wn
= wday_name
[timeptr
->tm_wday
];
662 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
663 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
665 else mn
= mon_name
[timeptr
->tm_mon
];
666 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
668 timeptr
->tm_mday
, timeptr
->tm_hour
,
669 timeptr
->tm_min
, timeptr
->tm_sec
);
671 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
672 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
675 if (trail
< 0 && lead
> 0) {
678 } else if (lead
< 0 && trail
> 0) {
683 (void) printf("%d", trail
);
684 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));