2 * $FreeBSD: src/usr.sbin/zic/zdump.c,v 1.7 1999/08/28 01:21:19 peter Exp $
5 ** This code has been made independent of the rest of the time
6 ** conversion package to increase confidence in the verification it provides.
7 ** You can use this code to help in verifying other implementations.
10 #include <ctype.h> /* for isalpha et al. */
13 #include <limits.h> /* for CHAR_BIT, LLONG_MAX */
15 #include <stdio.h> /* for stdout, stderr */
16 #include <stdlib.h> /* for exit, malloc, atoi */
17 #include <string.h> /* for strcpy */
18 #include <sys/types.h> /* for time_t */
19 #include <time.h> /* for struct tm */
23 #define ZDUMP_LO_YEAR (-500)
24 #endif /* !defined ZDUMP_LO_YEAR */
27 #define ZDUMP_HI_YEAR 2500
28 #endif /* !defined ZDUMP_HI_YEAR */
30 #ifndef MAX_STRING_LENGTH
31 #define MAX_STRING_LENGTH 1024
32 #endif /* !defined MAX_STRING_LENGTH */
36 #endif /* !defined TRUE */
40 #endif /* !defined FALSE */
43 #define EXIT_SUCCESS 0
44 #endif /* !defined EXIT_SUCCESS */
47 #define EXIT_FAILURE 1
48 #endif /* !defined EXIT_FAILURE */
52 #endif /* !defined SECSPERMIN */
55 #define MINSPERHOUR 60
56 #endif /* !defined MINSPERHOUR */
59 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
60 #endif /* !defined SECSPERHOUR */
63 #define HOURSPERDAY 24
64 #endif /* !defined HOURSPERDAY */
67 #define EPOCH_YEAR 1970
68 #endif /* !defined EPOCH_YEAR */
71 #define TM_YEAR_BASE 1900
72 #endif /* !defined TM_YEAR_BASE */
75 #define DAYSPERNYEAR 365
76 #endif /* !defined DAYSPERNYEAR */
79 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
80 #endif /* !defined isleap */
84 ** See tzfile.h for details on isleap_sum.
86 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
87 #endif /* !defined isleap_sum */
89 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
90 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
91 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
92 #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
93 + SECSPERLYEAR * (intmax_t) (100 - 3))
96 ** True if SECSPER400YEARS is known to be representable as an
97 ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
98 ** even if SECSPER400YEARS is representable, because when that happens
99 ** the code merely runs a bit more slowly, and this slowness doesn't
100 ** occur on any practical platform.
102 enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
105 ** For the benefit of GNU folk...
106 ** `_(MSGID)' uses the current locale's message library string for MSGID.
107 ** The default is to use gettext if available, and use MSGID otherwise.
111 #define _(msgid) msgid
112 #endif /* !defined _ */
115 #define TZ_DOMAIN "tz"
116 #endif /* !defined TZ_DOMAIN */
118 extern char ** environ;
119 extern char * tzname[2];
121 /* The minimum and maximum finite time values. */
122 static time_t const absolute_min_time =
124 ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
126 static time_t const absolute_max_time =
128 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
130 static size_t longest;
133 static char *abbr(struct tm *tmp);
134 static void abbrok(const char *abbrp, const char *zone);
135 static intmax_t delta(struct tm * newp, struct tm * oldp) __pure;
136 static void dumptime(const struct tm *tmp);
137 static time_t hunt(char *name, time_t lot, time_t hit);
138 static void show(char *zone, time_t t, int v);
139 static const char *tformat(void);
140 static time_t yeartot(intmax_t y) __pure;
141 static void usage(void);
144 #define my_localtime localtime
145 #else /* !defined TYPECHECK */
147 my_localtime(time_t *tp)
152 if (tp != NULL && tmp != NULL) {
160 fprintf(stderr, "\n%s: ", progname);
161 fprintf(stderr, tformat(), *tp);
162 fprintf(stderr, " ->");
163 fprintf(stderr, " year=%d", tmp->tm_year);
164 fprintf(stderr, " mon=%d", tmp->tm_mon);
165 fprintf(stderr, " mday=%d", tmp->tm_mday);
166 fprintf(stderr, " hour=%d", tmp->tm_hour);
167 fprintf(stderr, " min=%d", tmp->tm_min);
168 fprintf(stderr, " sec=%d", tmp->tm_sec);
169 fprintf(stderr, " isdst=%d", tmp->tm_isdst);
170 fprintf(stderr, " -> ");
171 fprintf(stderr, tformat(), t);
172 fprintf(stderr, "\n");
177 #endif /* !defined TYPECHECK */
180 abbrok(const char * const abbrp, const char * const zone)
189 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
192 wp = _("lacks alphabetic at start");
193 else if (cp - abbrp < 3)
194 wp = _("has fewer than 3 alphabetics");
195 else if (cp - abbrp > 6)
196 wp = _("has more than 6 alphabetics");
197 if (wp == NULL && (*cp == '+' || *cp == '-')) {
199 if (isascii((unsigned char) *cp) &&
200 isdigit((unsigned char) *cp))
201 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
204 wp = _("differs from POSIX standard");
209 warnx(_("warning: zone \"%s\" abbreviation \"%s\" %s\n"),
215 main(int argc, char *argv[])
233 cutlotime = absolute_min_time;
234 cuthitime = absolute_max_time;
236 cutarg = cuttimes = NULL;
238 switch (getopt(argc, argv, "c:t:vV")) {
239 case 'c': cutarg = optarg; break;
240 case 't': cuttimes = optarg; break;
241 case 'v': vflag = 1; break;
242 case 'V': Vflag = 1; break;
244 if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
245 goto arg_processing_done;
250 arg_processing_done:;
256 intmax_t cutloyear = ZDUMP_LO_YEAR;
257 intmax_t cuthiyear = ZDUMP_HI_YEAR;
258 if (cutarg != NULL) {
259 lo = strtoimax(cutarg, &loend, 10);
260 if (cutarg != loend && !*loend) {
263 } else if (cutarg != loend && *loend == ','
264 && (hi = strtoimax(loend + 1, &hiend, 10),
265 loend + 1 != hiend && !*hiend)) {
270 _("wild -c argument %s\n"),
274 if (cutarg != NULL || cuttimes == NULL) {
275 cutlotime = yeartot(cutloyear);
276 cuthitime = yeartot(cuthiyear);
278 if (cuttimes != NULL) {
279 lo = strtoimax(cuttimes, &loend, 10);
280 if (cuttimes != loend && !*loend) {
282 if (hi < cuthitime) {
283 if (hi < absolute_min_time)
284 hi = absolute_min_time;
287 } else if (cuttimes != loend && *loend == ','
288 && (hi = strtoimax(loend + 1, &hiend, 10),
289 loend + 1 != hiend && !*hiend)) {
290 if (cutlotime < lo) {
291 if (absolute_max_time < lo)
292 lo = absolute_max_time;
295 if (hi < cuthitime) {
296 if (hi < absolute_min_time)
297 hi = absolute_min_time;
302 _("wild -t argument %s\n"),
309 for (i = optind; i < argc; ++i)
310 if (strlen(argv[i]) > longest)
311 longest = strlen(argv[i]);
316 for (i = 0; environ[i] != NULL; ++i)
318 fakeenv = malloc((i + 2) * sizeof *fakeenv);
320 || (fakeenv[0] = malloc(longest + 4)) == NULL) {
322 _("malloc() failed"));
325 strcpy(fakeenv[to++], "TZ=");
326 for (from = 0; environ[from] != NULL; ++from)
327 if (strncmp(environ[from], "TZ=", 3) != 0)
328 fakeenv[to++] = environ[from];
332 for (i = optind; i < argc; ++i) {
333 static char buf[MAX_STRING_LENGTH];
335 strcpy(&fakeenv[0][3], argv[i]);
336 if (! (vflag | Vflag)) {
337 show(argv[i], now, FALSE);
341 t = absolute_min_time;
343 show(argv[i], t, TRUE);
345 show(argv[i], t, TRUE);
349 tmp = my_localtime(&t);
352 strncpy(buf, abbr(&tm), (sizeof buf) - 1);
355 newt = (t < absolute_max_time - SECSPERDAY / 2
357 : absolute_max_time);
358 if (cuthitime <= newt)
360 newtmp = localtime(&newt);
363 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
364 (delta(&newtm, &tm) != (newt - t) ||
365 newtm.tm_isdst != tm.tm_isdst ||
366 strcmp(abbr(&newtm), buf) != 0)) {
367 newt = hunt(argv[i], t, newt);
368 newtmp = localtime(&newt);
369 if (newtmp != NULL) {
381 t = absolute_max_time;
383 show(argv[i], t, TRUE);
385 show(argv[i], t, TRUE);
388 if (fflush(stdout) || ferror(stdout))
389 errx(EXIT_FAILURE, _("error writing standard output"));
391 /* If exit fails to exit... */
398 fprintf(stderr, _("usage: zdump [-vV] [-c [loyear,]hiyear] [-t [lotime,]hitime] zonename ...\n"));
403 yeartot(const intmax_t y)
405 intmax_t myy, seconds, years;
411 if (SECSPER400YEARS_FITS && 400 <= y - myy) {
412 intmax_t diff400 = (y - myy) / 400;
413 if (INTMAX_MAX / SECSPER400YEARS < diff400)
414 return absolute_max_time;
415 seconds = diff400 * SECSPER400YEARS;
416 years = diff400 * 400;
418 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
422 if (t > absolute_max_time - seconds)
423 return absolute_max_time;
427 if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
428 intmax_t diff400 = (myy - y) / 400;
429 if (INTMAX_MAX / SECSPER400YEARS < diff400)
430 return absolute_min_time;
431 seconds = diff400 * SECSPER400YEARS;
432 years = diff400 * 400;
434 seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
438 if (t < absolute_min_time + seconds)
439 return absolute_min_time;
446 hunt(char *name, time_t lot, time_t hit)
453 char loab[MAX_STRING_LENGTH];
455 lotmp = my_localtime(&lot);
458 strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
461 time_t diff = hit - lot;
470 tmp = my_localtime(&t);
473 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
474 (delta(&tm, &lotm) == (t - lot) &&
475 tm.tm_isdst == lotm.tm_isdst &&
476 strcmp(abbr(&tm), loab) == 0)) {
482 show(name, lot, TRUE);
483 show(name, hit, TRUE);
488 ** Thanks to Paul Eggert for logic used in delta.
492 delta(struct tm *newp, struct tm *oldp)
497 if (newp->tm_year < oldp->tm_year)
498 return -delta(oldp, newp);
500 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
501 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
502 result += newp->tm_yday - oldp->tm_yday;
503 result *= HOURSPERDAY;
504 result += newp->tm_hour - oldp->tm_hour;
505 result *= MINSPERHOUR;
506 result += newp->tm_min - oldp->tm_min;
507 result *= SECSPERMIN;
508 result += newp->tm_sec - oldp->tm_sec;
513 show(char *zone, time_t t, int v)
517 printf("%-*s ", (int) longest, zone);
521 printf(tformat(), t);
528 tmp = my_localtime(&t);
531 if (*abbr(tmp) != '\0')
532 printf(" %s", abbr(tmp));
534 printf(" isdst=%d", tmp->tm_isdst);
536 printf(" gmtoff=%ld", tmp->TM_GMTOFF);
537 #endif /* defined TM_GMTOFF */
541 if (tmp != NULL && *abbr(tmp) != '\0')
542 abbrok(abbr(tmp), zone);
551 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
553 result = tzname[tmp->tm_isdst];
554 return (result == NULL) ? &nada : result;
558 ** The code below can fail on certain theoretical systems;
559 ** it works on all known real-world systems as of 2004-12-30.
565 if (0 > (time_t) -1) { /* signed */
566 if (sizeof (time_t) == sizeof (intmax_t))
568 if (sizeof (time_t) > sizeof (long))
570 if (sizeof (time_t) > sizeof (int))
574 if (sizeof (time_t) == sizeof (uintmax_t))
576 if (sizeof (time_t) > sizeof (unsigned long))
578 if (sizeof (time_t) > sizeof (unsigned int))
584 dumptime(const struct tm *timeptr)
586 static const char wday_name[][3] = {
587 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
589 static const char mon_name[][3] = {
590 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
591 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
598 if (timeptr == NULL) {
603 ** The packaged versions of localtime and gmtime never put out-of-range
604 ** values in tm_wday or tm_mon, but since this code might be compiled
605 ** with other (perhaps experimental) versions, paranoia is in order.
607 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
608 (int) (sizeof wday_name / sizeof wday_name[0]))
610 else wn = wday_name[timeptr->tm_wday];
611 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
612 (int) (sizeof mon_name / sizeof mon_name[0]))
614 else mn = mon_name[timeptr->tm_mon];
615 printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
617 timeptr->tm_mday, timeptr->tm_hour,
618 timeptr->tm_min, timeptr->tm_sec);
620 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
621 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
624 if (trail < 0 && lead > 0) {
627 } else if (lead < 0 && trail > 0) {
633 else printf("%d%d", lead, ((trail < 0) ? -trail : trail));