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__ \
40 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
46 #ifndef HAVE_INTTYPES_H
47 # define HAVE_INTTYPES_H HAVE_STDINT_H
50 # include <inttypes.h>
53 #ifndef INT_FAST32_MAX
54 # if INT_MAX >> 31 == 0
55 typedef long int_fast32_t;
57 typedef int int_fast32_t;
62 # if defined LLONG_MAX || defined __LONG_LONG_MAX__
63 typedef long long intmax_t;
64 # define strtoimax strtoll
65 # define PRIdMAX "lld"
67 # define INTMAX_MAX LLONG_MAX
69 # define INTMAX_MAX __LONG_LONG_MAX__
72 typedef long intmax_t;
73 # define strtoimax strtol
75 # define INTMAX_MAX LONG_MAX
81 #define ZDUMP_LO_YEAR (-500)
82 #endif /* !defined ZDUMP_LO_YEAR */
85 #define ZDUMP_HI_YEAR 2500
86 #endif /* !defined ZDUMP_HI_YEAR */
88 #ifndef MAX_STRING_LENGTH
89 #define MAX_STRING_LENGTH 1024
90 #endif /* !defined MAX_STRING_LENGTH */
94 #endif /* !defined TRUE */
98 #endif /* !defined FALSE */
101 #define EXIT_SUCCESS 0
102 #endif /* !defined EXIT_SUCCESS */
105 #define EXIT_FAILURE 1
106 #endif /* !defined EXIT_FAILURE */
109 #define SECSPERMIN 60
110 #endif /* !defined SECSPERMIN */
113 #define MINSPERHOUR 60
114 #endif /* !defined MINSPERHOUR */
117 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
118 #endif /* !defined SECSPERHOUR */
121 #define HOURSPERDAY 24
122 #endif /* !defined HOURSPERDAY */
125 #define EPOCH_YEAR 1970
126 #endif /* !defined EPOCH_YEAR */
129 #define TM_YEAR_BASE 1900
130 #endif /* !defined TM_YEAR_BASE */
133 #define DAYSPERNYEAR 365
134 #endif /* !defined DAYSPERNYEAR */
137 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
138 #endif /* !defined isleap */
142 ** See tzfile.h for details on isleap_sum.
144 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
145 #endif /* !defined isleap_sum */
147 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
148 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
149 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
150 #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
151 + SECSPERLYEAR * (intmax_t) (100 - 3))
154 ** True if SECSPER400YEARS is known to be representable as an
155 ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
156 ** even if SECSPER400YEARS is representable, because when that happens
157 ** the code merely runs a bit more slowly, and this slowness doesn't
158 ** occur on any practical platform.
160 enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
163 #define HAVE_GETTEXT 0
166 #include "locale.h" /* for setlocale */
168 #endif /* HAVE_GETTEXT */
173 #else /* !defined lint */
176 #endif /* defined __GNUC__ */
177 #endif /* !defined lint */
178 #endif /* !defined GNUC_or_lint */
180 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
181 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
183 # define ATTRIBUTE_PURE /* empty */
187 ** For the benefit of GNU folk...
188 ** `_(MSGID)' uses the current locale's message library string for MSGID.
189 ** The default is to use gettext if available, and use MSGID otherwise.
194 #define _(msgid) gettext(msgid)
195 #else /* !HAVE_GETTEXT */
196 #define _(msgid) msgid
197 #endif /* !HAVE_GETTEXT */
198 #endif /* !defined _ */
201 #define TZ_DOMAIN "tz"
202 #endif /* !defined TZ_DOMAIN */
204 extern char ** environ;
205 extern int getopt(int argc, char * const argv[],
206 const char * options);
207 extern char * optarg;
209 extern char * tzname[2];
211 /* The minimum and maximum finite time values. */
212 static time_t const absolute_min_time =
214 ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
216 static time_t const absolute_max_time =
218 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
220 static size_t longest;
221 static char * progname;
224 static char * abbr(struct tm * tmp);
225 static void abbrok(const char * abbrp, const char * zone);
226 static intmax_t delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
227 static void dumptime(const struct tm * tmp);
228 static time_t hunt(char * name, time_t lot, time_t hit);
229 static void show(char * zone, time_t t, int v);
230 static const char * tformat(void);
231 static time_t yeartot(intmax_t y) ATTRIBUTE_PURE;
234 #define my_localtime localtime
235 #else /* !defined TYPECHECK */
237 my_localtime(time_t *tp)
239 register struct tm * tmp;
242 if (tp != NULL && tmp != NULL) {
249 (void) fflush(stdout);
250 (void) fprintf(stderr, "\n%s: ", progname);
251 (void) fprintf(stderr, tformat(), *tp);
252 (void) fprintf(stderr, " ->");
253 (void) fprintf(stderr, " year=%d", tmp->tm_year);
254 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
255 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
256 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
257 (void) fprintf(stderr, " min=%d", tmp->tm_min);
258 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
259 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
260 (void) fprintf(stderr, " -> ");
261 (void) fprintf(stderr, tformat(), t);
262 (void) fprintf(stderr, "\n");
267 #endif /* !defined TYPECHECK */
270 abbrok(const char *const abbrp, const char *const zone)
272 register const char * cp;
273 register const char * wp;
279 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
282 wp = _("lacks alphabetic at start");
283 else if (cp - abbrp < 3)
284 wp = _("has fewer than 3 alphabetics");
285 else if (cp - abbrp > 6)
286 wp = _("has more than 6 alphabetics");
287 if (wp == NULL && (*cp == '+' || *cp == '-')) {
289 if (isascii((unsigned char) *cp) &&
290 isdigit((unsigned char) *cp))
291 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
294 wp = _("differs from POSIX standard");
298 (void) fflush(stdout);
299 (void) fprintf(stderr,
300 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
301 progname, zone, abbrp, wp);
306 usage(FILE * const stream, const int status)
308 (void) fprintf(stream,
309 _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
311 "Report bugs to %s.\n"),
312 progname, progname, REPORT_BUGS_TO);
317 main(int argc, char *argv[])
322 register char * cutarg;
323 register char * cuttimes;
324 register time_t cutlotime;
325 register time_t cuthitime;
326 register char ** fakeenv;
332 register struct tm * tmp;
333 register struct tm * newtmp;
335 cutlotime = absolute_min_time;
336 cuthitime = absolute_max_time;
338 (void) setlocale(LC_ALL, "");
340 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
341 #endif /* defined TEXTDOMAINDIR */
342 (void) textdomain(TZ_DOMAIN);
343 #endif /* HAVE_GETTEXT */
345 for (i = 1; i < argc; ++i)
346 if (strcmp(argv[i], "--version") == 0) {
347 (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
349 } else if (strcmp(argv[i], "--help") == 0) {
350 usage(stdout, EXIT_SUCCESS);
353 cutarg = cuttimes = NULL;
355 switch (getopt(argc, argv, "c:t:vV")) {
356 case 'c': cutarg = optarg; break;
357 case 't': cuttimes = optarg; break;
358 case 'v': vflag = 1; break;
359 case 'V': Vflag = 1; break;
361 if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
362 goto arg_processing_done;
365 usage(stderr, EXIT_FAILURE);
367 arg_processing_done:;
373 register intmax_t cutloyear = ZDUMP_LO_YEAR;
374 register intmax_t cuthiyear = ZDUMP_HI_YEAR;
375 if (cutarg != NULL) {
376 lo = strtoimax(cutarg, &loend, 10);
377 if (cutarg != loend && !*loend) {
380 } else if (cutarg != loend && *loend == ','
381 && (hi = strtoimax(loend + 1, &hiend, 10),
382 loend + 1 != hiend && !*hiend)) {
386 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
391 if (cutarg != NULL || cuttimes == NULL) {
392 cutlotime = yeartot(cutloyear);
393 cuthitime = yeartot(cuthiyear);
395 if (cuttimes != NULL) {
396 lo = strtoimax(cuttimes, &loend, 10);
397 if (cuttimes != loend && !*loend) {
399 if (hi < cuthitime) {
400 if (hi < absolute_min_time)
401 hi = absolute_min_time;
404 } else if (cuttimes != loend && *loend == ','
405 && (hi = strtoimax(loend + 1, &hiend, 10),
406 loend + 1 != hiend && !*hiend)) {
407 if (cutlotime < lo) {
408 if (absolute_max_time < lo)
409 lo = absolute_max_time;
412 if (hi < cuthitime) {
413 if (hi < absolute_min_time)
414 hi = absolute_min_time;
418 (void) fprintf(stderr,
419 _("%s: wild -t argument %s\n"),
427 for (i = optind; i < argc; ++i)
428 if (strlen(argv[i]) > longest)
429 longest = strlen(argv[i]);
434 for (i = 0; environ[i] != NULL; ++i)
436 fakeenv = malloc((i + 2) * sizeof *fakeenv);
438 || (fakeenv[0] = malloc(longest + 4)) == NULL) {
439 (void) perror(progname);
443 (void) strcpy(fakeenv[to++], "TZ=");
444 for (from = 0; environ[from] != NULL; ++from)
445 if (strncmp(environ[from], "TZ=", 3) != 0)
446 fakeenv[to++] = environ[from];
450 for (i = optind; i < argc; ++i) {
451 static char buf[MAX_STRING_LENGTH];
453 (void) strcpy(&fakeenv[0][3], argv[i]);
454 if (! (vflag | Vflag)) {
455 show(argv[i], now, FALSE);
459 t = absolute_min_time;
461 show(argv[i], t, TRUE);
463 show(argv[i], t, TRUE);
467 tmp = my_localtime(&t);
470 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
473 newt = (t < absolute_max_time - SECSPERDAY / 2
475 : absolute_max_time);
476 if (cuthitime <= newt)
478 newtmp = localtime(&newt);
481 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
482 (delta(&newtm, &tm) != (newt - t) ||
483 newtm.tm_isdst != tm.tm_isdst ||
484 strcmp(abbr(&newtm), buf) != 0)) {
485 newt = hunt(argv[i], t, newt);
486 newtmp = localtime(&newt);
487 if (newtmp != NULL) {
499 t = absolute_max_time;
501 show(argv[i], t, TRUE);
503 show(argv[i], t, TRUE);
506 if (fflush(stdout) || ferror(stdout)) {
507 (void) fprintf(stderr, "%s: ", progname);
508 (void) perror(_("Error writing to standard output"));
512 /* If exit fails to exit... */
517 yeartot(const intmax_t y)
519 register intmax_t myy, seconds, years;
525 if (SECSPER400YEARS_FITS && 400 <= y - myy) {
526 intmax_t diff400 = (y - myy) / 400;
527 if (INTMAX_MAX / SECSPER400YEARS < diff400)
528 return absolute_max_time;
529 seconds = diff400 * SECSPER400YEARS;
530 years = diff400 * 400;
532 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
536 if (t > absolute_max_time - seconds)
537 return absolute_max_time;
541 if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
542 intmax_t diff400 = (myy - y) / 400;
543 if (INTMAX_MAX / SECSPER400YEARS < diff400)
544 return absolute_min_time;
545 seconds = diff400 * SECSPER400YEARS;
546 years = diff400 * 400;
548 seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
552 if (t < absolute_min_time + seconds)
553 return absolute_min_time;
560 hunt(char *name, time_t lot, time_t hit)
564 register struct tm * lotmp;
566 register struct tm * tmp;
567 char loab[MAX_STRING_LENGTH];
569 lotmp = my_localtime(&lot);
572 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
575 time_t diff = hit - lot;
584 tmp = my_localtime(&t);
587 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
588 (delta(&tm, &lotm) == (t - lot) &&
589 tm.tm_isdst == lotm.tm_isdst &&
590 strcmp(abbr(&tm), loab) == 0)) {
596 show(name, lot, TRUE);
597 show(name, hit, TRUE);
602 ** Thanks to Paul Eggert for logic used in delta.
606 delta(struct tm * newp, struct tm *oldp)
608 register intmax_t result;
611 if (newp->tm_year < oldp->tm_year)
612 return -delta(oldp, newp);
614 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
615 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
616 result += newp->tm_yday - oldp->tm_yday;
617 result *= HOURSPERDAY;
618 result += newp->tm_hour - oldp->tm_hour;
619 result *= MINSPERHOUR;
620 result += newp->tm_min - oldp->tm_min;
621 result *= SECSPERMIN;
622 result += newp->tm_sec - oldp->tm_sec;
627 show(char *zone, time_t t, int v)
629 register struct tm * tmp;
631 (void) printf("%-*s ", (int) longest, zone);
635 (void) printf(tformat(), t);
638 (void) printf(" UT");
640 (void) printf(" = ");
642 tmp = my_localtime(&t);
645 if (*abbr(tmp) != '\0')
646 (void) printf(" %s", abbr(tmp));
648 (void) printf(" isdst=%d", tmp->tm_isdst);
650 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
651 #endif /* defined TM_GMTOFF */
655 if (tmp != NULL && *abbr(tmp) != '\0')
656 abbrok(abbr(tmp), zone);
662 register char * result;
665 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
667 result = tzname[tmp->tm_isdst];
668 return (result == NULL) ? &nada : result;
672 ** The code below can fail on certain theoretical systems;
673 ** it works on all known real-world systems as of 2004-12-30.
679 if (0 > (time_t) -1) { /* signed */
680 if (sizeof (time_t) == sizeof (intmax_t))
682 if (sizeof (time_t) > sizeof (long))
684 if (sizeof (time_t) > sizeof (int))
689 if (sizeof (time_t) == sizeof (uintmax_t))
692 if (sizeof (time_t) > sizeof (unsigned long))
694 if (sizeof (time_t) > sizeof (unsigned int))
700 dumptime(register const struct tm *timeptr)
702 static const char wday_name[][3] = {
703 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
705 static const char mon_name[][3] = {
706 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
707 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
709 register const char * wn;
710 register const char * mn;
714 if (timeptr == NULL) {
715 (void) printf("NULL");
719 ** The packaged versions of localtime and gmtime never put out-of-range
720 ** values in tm_wday or tm_mon, but since this code might be compiled
721 ** with other (perhaps experimental) versions, paranoia is in order.
723 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
724 (int) (sizeof wday_name / sizeof wday_name[0]))
726 else wn = wday_name[timeptr->tm_wday];
727 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
728 (int) (sizeof mon_name / sizeof mon_name[0]))
730 else mn = mon_name[timeptr->tm_mon];
731 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
733 timeptr->tm_mday, timeptr->tm_hour,
734 timeptr->tm_min, timeptr->tm_sec);
736 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
737 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
740 if (trail < 0 && lead > 0) {
743 } else if (lead < 0 && trail > 0) {
748 (void) printf("%d", trail);
749 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));