2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
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.
13 ** However, include private.h when debugging, so that it overrides
14 ** time_t consistently with the rest of the package.
21 #include "stdio.h" /* for stdout, stderr, perror */
22 #include "string.h" /* for strcpy */
23 #include "sys/types.h" /* for time_t */
24 #include "time.h" /* for struct tm */
25 #include "stdlib.h" /* for exit, malloc, atoi */
26 #include "limits.h" /* for CHAR_BIT, LLONG_MAX */
27 #include "ctype.h" /* for isalpha et al. */
30 #endif /* !defined isascii */
33 ** Substitutes for pre-C99 compilers.
34 ** Much of this section of code is stolen from private.h.
38 # define HAVE_STDINT_H \
39 (199901 <= __STDC_VERSION__ || 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
44 #ifndef HAVE_INTTYPES_H
45 # define HAVE_INTTYPES_H HAVE_STDINT_H
48 # include <inttypes.h>
51 #ifndef INT_FAST32_MAX
52 # if INT_MAX >> 31 == 0
53 typedef long int_fast32_t;
55 typedef int int_fast32_t;
60 # if defined LLONG_MAX || defined __LONG_LONG_MAX__
61 typedef long long intmax_t;
62 # define strtoimax strtoll
63 # define PRIdMAX "lld"
65 # define INTMAX_MAX LLONG_MAX
67 # define INTMAX_MAX __LONG_LONG_MAX__
70 typedef long intmax_t;
71 # define strtoimax strtol
73 # define INTMAX_MAX LONG_MAX
79 #define ZDUMP_LO_YEAR (-500)
80 #endif /* !defined ZDUMP_LO_YEAR */
83 #define ZDUMP_HI_YEAR 2500
84 #endif /* !defined ZDUMP_HI_YEAR */
86 #ifndef MAX_STRING_LENGTH
87 #define MAX_STRING_LENGTH 1024
88 #endif /* !defined MAX_STRING_LENGTH */
92 #endif /* !defined TRUE */
96 #endif /* !defined FALSE */
99 #define EXIT_SUCCESS 0
100 #endif /* !defined EXIT_SUCCESS */
103 #define EXIT_FAILURE 1
104 #endif /* !defined EXIT_FAILURE */
107 #define SECSPERMIN 60
108 #endif /* !defined SECSPERMIN */
111 #define MINSPERHOUR 60
112 #endif /* !defined MINSPERHOUR */
115 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
116 #endif /* !defined SECSPERHOUR */
119 #define HOURSPERDAY 24
120 #endif /* !defined HOURSPERDAY */
123 #define EPOCH_YEAR 1970
124 #endif /* !defined EPOCH_YEAR */
127 #define TM_YEAR_BASE 1900
128 #endif /* !defined TM_YEAR_BASE */
131 #define DAYSPERNYEAR 365
132 #endif /* !defined DAYSPERNYEAR */
135 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
136 #endif /* !defined isleap */
140 ** See tzfile.h for details on isleap_sum.
142 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
143 #endif /* !defined isleap_sum */
145 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
146 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
147 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
148 #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
149 + SECSPERLYEAR * (intmax_t) (100 - 3))
152 ** True if SECSPER400YEARS is known to be representable as an
153 ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
154 ** even if SECSPER400YEARS is representable, because when that happens
155 ** the code merely runs a bit more slowly, and this slowness doesn't
156 ** occur on any practical platform.
158 enum { SECSPER400YEARS_FITS
= SECSPERLYEAR
<= INTMAX_MAX
/ 400 };
161 #define HAVE_GETTEXT 0
164 #include "locale.h" /* for setlocale */
166 #endif /* HAVE_GETTEXT */
171 #else /* !defined lint */
174 #endif /* defined __GNUC__ */
175 #endif /* !defined lint */
176 #endif /* !defined GNUC_or_lint */
178 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
179 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
181 # define ATTRIBUTE_PURE /* empty */
185 ** For the benefit of GNU folk...
186 ** `_(MSGID)' uses the current locale's message library string for MSGID.
187 ** The default is to use gettext if available, and use MSGID otherwise.
192 #define _(msgid) gettext(msgid)
193 #else /* !HAVE_GETTEXT */
194 #define _(msgid) msgid
195 #endif /* !HAVE_GETTEXT */
196 #endif /* !defined _ */
199 #define TZ_DOMAIN "tz"
200 #endif /* !defined TZ_DOMAIN */
202 extern char ** environ
;
203 extern int getopt(int argc
, char * const argv
[],
204 const char * options
);
205 extern char * optarg
;
207 extern char * tzname
[2];
209 /* The minimum and maximum finite time values. */
210 static time_t const absolute_min_time
=
212 ? (time_t) -1 << (CHAR_BIT
* sizeof (time_t) - 1)
214 static time_t const absolute_max_time
=
216 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT
* sizeof (time_t) - 1))
218 static size_t longest
;
219 static char * progname
;
222 static char * abbr(struct tm
* tmp
);
223 static void abbrok(const char * abbrp
, const char * zone
);
224 static intmax_t delta(struct tm
* newp
, struct tm
* oldp
) ATTRIBUTE_PURE
;
225 static void dumptime(const struct tm
* tmp
);
226 static time_t hunt(char * name
, time_t lot
, time_t hit
);
227 static void show(char * zone
, time_t t
, int v
);
228 static const char * tformat(void);
229 static time_t yeartot(intmax_t y
) ATTRIBUTE_PURE
;
232 #define my_localtime localtime
233 #else /* !defined TYPECHECK */
235 my_localtime(time_t *tp
)
237 register struct tm
* tmp
;
240 if (tp
!= NULL
&& tmp
!= NULL
) {
247 (void) fflush(stdout
);
248 (void) fprintf(stderr
, "\n%s: ", progname
);
249 (void) fprintf(stderr
, tformat(), *tp
);
250 (void) fprintf(stderr
, " ->");
251 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
252 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
253 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
254 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
255 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
256 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
257 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
258 (void) fprintf(stderr
, " -> ");
259 (void) fprintf(stderr
, tformat(), t
);
260 (void) fprintf(stderr
, "\n");
265 #endif /* !defined TYPECHECK */
268 abbrok(const char *const abbrp
, const char *const zone
)
270 register const char * cp
;
271 register const char * wp
;
277 while (isascii((unsigned char) *cp
) && isalpha((unsigned char) *cp
))
280 wp
= _("lacks alphabetic at start");
281 else if (cp
- abbrp
< 3)
282 wp
= _("has fewer than 3 alphabetics");
283 else if (cp
- abbrp
> 6)
284 wp
= _("has more than 6 alphabetics");
285 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
287 if (isascii((unsigned char) *cp
) &&
288 isdigit((unsigned char) *cp
))
289 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
292 wp
= _("differs from POSIX standard");
296 (void) fflush(stdout
);
297 (void) fprintf(stderr
,
298 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
299 progname
, zone
, abbrp
, wp
);
304 usage(FILE * const stream
, const int status
)
306 (void) fprintf(stream
,
307 _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
309 "Report bugs to %s.\n"),
310 progname
, progname
, REPORT_BUGS_TO
);
315 main(int argc
, char *argv
[])
320 register char * cutarg
;
321 register char * cuttimes
;
322 register time_t cutlotime
;
323 register time_t cuthitime
;
324 register char ** fakeenv
;
330 register struct tm
* tmp
;
331 register struct tm
* newtmp
;
333 cutlotime
= absolute_min_time
;
334 cuthitime
= absolute_max_time
;
336 (void) setlocale(LC_ALL
, "");
338 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
339 #endif /* defined TEXTDOMAINDIR */
340 (void) textdomain(TZ_DOMAIN
);
341 #endif /* HAVE_GETTEXT */
343 for (i
= 1; i
< argc
; ++i
)
344 if (strcmp(argv
[i
], "--version") == 0) {
345 (void) printf("zdump %s%s\n", PKGVERSION
, TZVERSION
);
347 } else if (strcmp(argv
[i
], "--help") == 0) {
348 usage(stdout
, EXIT_SUCCESS
);
351 cutarg
= cuttimes
= NULL
;
353 switch (getopt(argc
, argv
, "c:t:vV")) {
354 case 'c': cutarg
= optarg
; break;
355 case 't': cuttimes
= optarg
; break;
356 case 'v': vflag
= 1; break;
357 case 'V': Vflag
= 1; break;
359 if (! (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0))
360 goto arg_processing_done
;
363 usage(stderr
, EXIT_FAILURE
);
365 arg_processing_done
:;
371 register intmax_t cutloyear
= ZDUMP_LO_YEAR
;
372 register intmax_t cuthiyear
= ZDUMP_HI_YEAR
;
373 if (cutarg
!= NULL
) {
374 lo
= strtoimax(cutarg
, &loend
, 10);
375 if (cutarg
!= loend
&& !*loend
) {
378 } else if (cutarg
!= loend
&& *loend
== ','
379 && (hi
= strtoimax(loend
+ 1, &hiend
, 10),
380 loend
+ 1 != hiend
&& !*hiend
)) {
384 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
389 if (cutarg
!= NULL
|| cuttimes
== NULL
) {
390 cutlotime
= yeartot(cutloyear
);
391 cuthitime
= yeartot(cuthiyear
);
393 if (cuttimes
!= NULL
) {
394 lo
= strtoimax(cuttimes
, &loend
, 10);
395 if (cuttimes
!= loend
&& !*loend
) {
397 if (hi
< cuthitime
) {
398 if (hi
< absolute_min_time
)
399 hi
= absolute_min_time
;
402 } else if (cuttimes
!= loend
&& *loend
== ','
403 && (hi
= strtoimax(loend
+ 1, &hiend
, 10),
404 loend
+ 1 != hiend
&& !*hiend
)) {
405 if (cutlotime
< lo
) {
406 if (absolute_max_time
< lo
)
407 lo
= absolute_max_time
;
410 if (hi
< cuthitime
) {
411 if (hi
< absolute_min_time
)
412 hi
= absolute_min_time
;
416 (void) fprintf(stderr
,
417 _("%s: wild -t argument %s\n"),
425 for (i
= optind
; i
< argc
; ++i
)
426 if (strlen(argv
[i
]) > longest
)
427 longest
= strlen(argv
[i
]);
432 for (i
= 0; environ
[i
] != NULL
; ++i
)
434 fakeenv
= malloc((i
+ 2) * sizeof *fakeenv
);
436 || (fakeenv
[0] = malloc(longest
+ 4)) == NULL
) {
437 (void) perror(progname
);
441 (void) strcpy(fakeenv
[to
++], "TZ=");
442 for (from
= 0; environ
[from
] != NULL
; ++from
)
443 if (strncmp(environ
[from
], "TZ=", 3) != 0)
444 fakeenv
[to
++] = environ
[from
];
448 for (i
= optind
; i
< argc
; ++i
) {
449 static char buf
[MAX_STRING_LENGTH
];
451 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
452 if (! (vflag
| Vflag
)) {
453 show(argv
[i
], now
, FALSE
);
457 t
= absolute_min_time
;
459 show(argv
[i
], t
, TRUE
);
461 show(argv
[i
], t
, TRUE
);
465 tmp
= my_localtime(&t
);
468 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
471 newt
= (t
< absolute_max_time
- SECSPERDAY
/ 2
473 : absolute_max_time
);
474 if (cuthitime
<= newt
)
476 newtmp
= localtime(&newt
);
479 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
480 (delta(&newtm
, &tm
) != (newt
- t
) ||
481 newtm
.tm_isdst
!= tm
.tm_isdst
||
482 strcmp(abbr(&newtm
), buf
) != 0)) {
483 newt
= hunt(argv
[i
], t
, newt
);
484 newtmp
= localtime(&newt
);
485 if (newtmp
!= NULL
) {
497 t
= absolute_max_time
;
499 show(argv
[i
], t
, TRUE
);
501 show(argv
[i
], t
, TRUE
);
504 if (fflush(stdout
) || ferror(stdout
)) {
505 (void) fprintf(stderr
, "%s: ", progname
);
506 (void) perror(_("Error writing to standard output"));
510 /* If exit fails to exit... */
515 yeartot(const intmax_t y
)
517 register intmax_t myy
, seconds
, years
;
523 if (SECSPER400YEARS_FITS
&& 400 <= y
- myy
) {
524 intmax_t diff400
= (y
- myy
) / 400;
525 if (INTMAX_MAX
/ SECSPER400YEARS
< diff400
)
526 return absolute_max_time
;
527 seconds
= diff400
* SECSPER400YEARS
;
528 years
= diff400
* 400;
530 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
534 if (t
> absolute_max_time
- seconds
)
535 return absolute_max_time
;
539 if (SECSPER400YEARS_FITS
&& y
+ 400 <= myy
&& myy
< 0) {
540 intmax_t diff400
= (myy
- y
) / 400;
541 if (INTMAX_MAX
/ SECSPER400YEARS
< diff400
)
542 return absolute_min_time
;
543 seconds
= diff400
* SECSPER400YEARS
;
544 years
= diff400
* 400;
546 seconds
= isleap(myy
- 1) ? SECSPERLYEAR
: SECSPERNYEAR
;
550 if (t
< absolute_min_time
+ seconds
)
551 return absolute_min_time
;
558 hunt(char *name
, time_t lot
, time_t hit
)
562 register struct tm
* lotmp
;
564 register struct tm
* tmp
;
565 char loab
[MAX_STRING_LENGTH
];
567 lotmp
= my_localtime(&lot
);
570 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
573 time_t diff
= hit
- lot
;
582 tmp
= my_localtime(&t
);
585 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
586 (delta(&tm
, &lotm
) == (t
- lot
) &&
587 tm
.tm_isdst
== lotm
.tm_isdst
&&
588 strcmp(abbr(&tm
), loab
) == 0)) {
594 show(name
, lot
, TRUE
);
595 show(name
, hit
, TRUE
);
600 ** Thanks to Paul Eggert for logic used in delta.
604 delta(struct tm
* newp
, struct tm
*oldp
)
606 register intmax_t result
;
609 if (newp
->tm_year
< oldp
->tm_year
)
610 return -delta(oldp
, newp
);
612 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
613 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
614 result
+= newp
->tm_yday
- oldp
->tm_yday
;
615 result
*= HOURSPERDAY
;
616 result
+= newp
->tm_hour
- oldp
->tm_hour
;
617 result
*= MINSPERHOUR
;
618 result
+= newp
->tm_min
- oldp
->tm_min
;
619 result
*= SECSPERMIN
;
620 result
+= newp
->tm_sec
- oldp
->tm_sec
;
625 show(char *zone
, time_t t
, int v
)
627 register struct tm
* tmp
;
629 (void) printf("%-*s ", (int) longest
, zone
);
633 (void) printf(tformat(), t
);
636 (void) printf(" UT");
638 (void) printf(" = ");
640 tmp
= my_localtime(&t
);
643 if (*abbr(tmp
) != '\0')
644 (void) printf(" %s", abbr(tmp
));
646 (void) printf(" isdst=%d", tmp
->tm_isdst
);
648 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
649 #endif /* defined TM_GMTOFF */
653 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
654 abbrok(abbr(tmp
), zone
);
660 register char * result
;
663 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
665 result
= tzname
[tmp
->tm_isdst
];
666 return (result
== NULL
) ? &nada
: result
;
670 ** The code below can fail on certain theoretical systems;
671 ** it works on all known real-world systems as of 2004-12-30.
677 if (0 > (time_t) -1) { /* signed */
678 if (sizeof (time_t) == sizeof (intmax_t))
680 if (sizeof (time_t) > sizeof (long))
682 if (sizeof (time_t) > sizeof (int))
687 if (sizeof (time_t) == sizeof (uintmax_t))
690 if (sizeof (time_t) > sizeof (unsigned long))
692 if (sizeof (time_t) > sizeof (unsigned int))
698 dumptime(register const struct tm
*timeptr
)
700 static const char wday_name
[][3] = {
701 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
703 static const char mon_name
[][3] = {
704 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
705 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
707 register const char * wn
;
708 register const char * mn
;
712 if (timeptr
== NULL
) {
713 (void) printf("NULL");
717 ** The packaged versions of localtime and gmtime never put out-of-range
718 ** values in tm_wday or tm_mon, but since this code might be compiled
719 ** with other (perhaps experimental) versions, paranoia is in order.
721 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
722 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
724 else wn
= wday_name
[timeptr
->tm_wday
];
725 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
726 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
728 else mn
= mon_name
[timeptr
->tm_mon
];
729 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
731 timeptr
->tm_mday
, timeptr
->tm_hour
,
732 timeptr
->tm_min
, timeptr
->tm_sec
);
734 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
735 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
738 if (trail
< 0 && lead
> 0) {
741 } else if (lead
< 0 && trail
> 0) {
746 (void) printf("%d", trail
);
747 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));