3 * $FreeBSD: src/usr.sbin/zic/zdump.c,v 1.7 1999/08/28 01:21:19 peter Exp $
4 * $DragonFly: src/usr.sbin/zic/zdump.c,v 1.5 2008/10/19 20:15:58 swildner Exp $
7 ** This code has been made independent of the rest of the time
8 ** conversion package to increase confidence in the verification it provides.
9 ** You can use this code to help in verifying other implementations.
12 #include <ctype.h> /* for isalpha et al. */
14 #include <float.h> /* for FLT_MAX and DBL_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 ((long) SECSPERHOUR * HOURSPERDAY)
90 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
91 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
96 #else /* !defined lint */
99 #endif /* defined __GNUC__ */
100 #endif /* !defined lint */
101 #endif /* !defined GNUC_or_lint */
105 #define INITIALIZE(x) ((x) = 0)
106 #else /* !defined GNUC_or_lint */
107 #define INITIALIZE(x)
108 #endif /* !defined GNUC_or_lint */
109 #endif /* !defined INITIALIZE */
112 ** For the benefit of GNU folk...
113 ** `_(MSGID)' uses the current locale's message library string for MSGID.
114 ** The default is to use gettext if available, and use MSGID otherwise.
118 #define _(msgid) msgid
119 #endif /* !defined _ */
122 #define TZ_DOMAIN "tz"
123 #endif /* !defined TZ_DOMAIN */
125 extern char ** environ;
126 extern char * tzname[2];
128 static time_t absolute_min_time;
129 static time_t absolute_max_time;
130 static size_t longest;
133 static char *abbr(struct tm *tmp);
134 static void abbrok(const char *abbrp, const char *zone);
135 static long delta(struct tm *newp, struct tm *oldp);
136 static void dumptime(const struct tm *tmp);
137 static time_t hunt(char *name, time_t lot, time_t hit);
138 static void setabsolutes(void);
139 static void show(char *zone, time_t t, int v);
140 static const char *tformat(void);
141 static time_t yeartot(long y);
142 static void usage(void);
145 #define my_localtime localtime
146 #else /* !defined TYPECHECK */
148 my_localtime(time_t *tp)
153 if (tp != NULL && tmp != NULL) {
159 if (t - *tp >= 1 || *tp - t >= 1) {
161 fprintf(stderr, "\n%s: ", progname);
162 fprintf(stderr, tformat(), *tp);
163 fprintf(stderr, " ->");
164 fprintf(stderr, " year=%d", tmp->tm_year);
165 fprintf(stderr, " mon=%d", tmp->tm_mon);
166 fprintf(stderr, " mday=%d", tmp->tm_mday);
167 fprintf(stderr, " hour=%d", tmp->tm_hour);
168 fprintf(stderr, " min=%d", tmp->tm_min);
169 fprintf(stderr, " sec=%d", tmp->tm_sec);
170 fprintf(stderr, " isdst=%d", tmp->tm_isdst);
171 fprintf(stderr, " -> ");
172 fprintf(stderr, tformat(), t);
173 fprintf(stderr, "\n");
178 #endif /* !defined TYPECHECK */
181 abbrok(const char * const abbrp, const char * const zone)
190 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
193 wp = _("lacks alphabetic at start");
194 else if (cp - abbrp < 3)
195 wp = _("has fewer than 3 alphabetics");
196 else if (cp - abbrp > 6)
197 wp = _("has more than 6 alphabetics");
198 if (wp == NULL && (*cp == '+' || *cp == '-')) {
200 if (isascii((unsigned char) *cp) &&
201 isdigit((unsigned char) *cp))
202 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
205 wp = _("differs from POSIX standard");
210 warnx(_("warning: zone \"%s\" abbreviation \"%s\" %s\n"),
216 main(int argc, char *argv[])
222 long cutloyear = ZDUMP_LO_YEAR;
223 long cuthiyear = ZDUMP_HI_YEAR;
235 INITIALIZE(cutlotime);
236 INITIALIZE(cuthitime);
239 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
242 else cutarg = optarg;
243 if ((c != EOF && c != -1) ||
244 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
248 if (cutarg != NULL) {
253 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
255 } else if (sscanf(cutarg, "%ld,%ld%c",
256 &lo, &hi, &dummy) == 2) {
261 _("wild -c argument %s\n"),
266 cutlotime = yeartot(cutloyear);
267 cuthitime = yeartot(cuthiyear);
271 for (i = optind; i < argc; ++i)
272 if (strlen(argv[i]) > longest)
273 longest = strlen(argv[i]);
278 for (i = 0; environ[i] != NULL; ++i)
280 fakeenv = (char **) malloc((size_t) ((i + 2) *
282 if (fakeenv == NULL ||
283 (fakeenv[0] = (char *) malloc((size_t) (longest +
286 _("malloc() failed"));
288 strcpy(fakeenv[to++], "TZ=");
289 for (from = 0; environ[from] != NULL; ++from)
290 if (strncmp(environ[from], "TZ=", 3) != 0)
291 fakeenv[to++] = environ[from];
295 for (i = optind; i < argc; ++i) {
296 static char buf[MAX_STRING_LENGTH];
298 strcpy(&fakeenv[0][3], argv[i]);
300 show(argv[i], now, FALSE);
304 t = absolute_min_time;
305 show(argv[i], t, TRUE);
306 t += SECSPERHOUR * HOURSPERDAY;
307 show(argv[i], t, TRUE);
310 tmp = my_localtime(&t);
313 strncpy(buf, abbr(&tm), (sizeof buf) - 1);
316 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
318 newt = t + SECSPERHOUR * 12;
319 newtmp = localtime(&newt);
322 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
323 (delta(&newtm, &tm) != (newt - t) ||
324 newtm.tm_isdst != tm.tm_isdst ||
325 strcmp(abbr(&newtm), buf) != 0)) {
326 newt = hunt(argv[i], t, newt);
327 newtmp = localtime(&newt);
328 if (newtmp != NULL) {
339 t = absolute_max_time;
340 t -= SECSPERHOUR * HOURSPERDAY;
341 show(argv[i], t, TRUE);
342 t += SECSPERHOUR * HOURSPERDAY;
343 show(argv[i], t, TRUE);
345 if (fflush(stdout) || ferror(stdout))
346 errx(EXIT_FAILURE, _("error writing standard output"));
348 /* If exit fails to exit... */
355 if (0.5 == (time_t) 0.5) {
357 ** time_t is floating.
359 if (sizeof (time_t) == sizeof (float)) {
360 absolute_min_time = (time_t) -FLT_MAX;
361 absolute_max_time = (time_t) FLT_MAX;
362 } else if (sizeof (time_t) == sizeof (double)) {
363 absolute_min_time = (time_t) -DBL_MAX;
364 absolute_max_time = (time_t) DBL_MAX;
367 _("use of -v on system with floating time_t other than float or double\n"));
369 } else if (0 > (time_t) -1) {
371 ** time_t is signed. Assume overflow wraps around.
381 absolute_max_time = t;
383 absolute_min_time = t - 1;
384 if (t < absolute_min_time)
385 absolute_min_time = t;
388 ** time_t is unsigned.
390 absolute_min_time = 0;
391 absolute_max_time = absolute_min_time - 1;
398 fprintf(stderr, _("usage: zdump [-v] [-c [loyear,]hiyear] zonename ...\n"));
403 yeartot(const long y)
413 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
415 if (t > absolute_max_time - seconds) {
416 t = absolute_max_time;
422 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
423 if (t < absolute_min_time + seconds) {
424 t = absolute_min_time;
434 hunt(char *name, time_t lot, time_t hit)
442 char loab[MAX_STRING_LENGTH];
444 lotmp = my_localtime(&lot);
447 strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
450 diff = (long) (hit - lot);
459 tmp = my_localtime(&t);
462 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
463 (delta(&tm, &lotm) == (t - lot) &&
464 tm.tm_isdst == lotm.tm_isdst &&
465 strcmp(abbr(&tm), loab) == 0)) {
471 show(name, lot, TRUE);
472 show(name, hit, TRUE);
477 ** Thanks to Paul Eggert for logic used in delta.
481 delta(struct tm *newp, struct tm *oldp)
486 if (newp->tm_year < oldp->tm_year)
487 return -delta(oldp, newp);
489 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
490 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
491 result += newp->tm_yday - oldp->tm_yday;
492 result *= HOURSPERDAY;
493 result += newp->tm_hour - oldp->tm_hour;
494 result *= MINSPERHOUR;
495 result += newp->tm_min - oldp->tm_min;
496 result *= SECSPERMIN;
497 result += newp->tm_sec - oldp->tm_sec;
502 show(char *zone, time_t t, int v)
506 printf("%-*s ", (int) longest, zone);
510 printf(tformat(), t);
517 tmp = my_localtime(&t);
520 if (*abbr(tmp) != '\0')
521 printf(" %s", abbr(tmp));
523 printf(" isdst=%d", tmp->tm_isdst);
525 printf(" gmtoff=%ld", tmp->TM_GMTOFF);
526 #endif /* defined TM_GMTOFF */
530 if (tmp != NULL && *abbr(tmp) != '\0')
531 abbrok(abbr(tmp), zone);
540 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
542 result = tzname[tmp->tm_isdst];
543 return (result == NULL) ? &nada : result;
547 ** The code below can fail on certain theoretical systems;
548 ** it works on all known real-world systems as of 2004-12-30.
554 if (0.5 == (time_t) 0.5) { /* floating */
555 if (sizeof (time_t) > sizeof (double))
559 if (0 > (time_t) -1) { /* signed */
560 if (sizeof (time_t) > sizeof (long))
562 if (sizeof (time_t) > sizeof (int))
566 if (sizeof (time_t) > sizeof (unsigned long))
568 if (sizeof (time_t) > sizeof (unsigned int))
574 dumptime(const struct tm *timeptr)
576 static const char wday_name[][3] = {
577 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
579 static const char mon_name[][3] = {
580 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
581 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
588 if (timeptr == NULL) {
593 ** The packaged versions of localtime and gmtime never put out-of-range
594 ** values in tm_wday or tm_mon, but since this code might be compiled
595 ** with other (perhaps experimental) versions, paranoia is in order.
597 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
598 (int) (sizeof wday_name / sizeof wday_name[0]))
600 else wn = wday_name[timeptr->tm_wday];
601 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
602 (int) (sizeof mon_name / sizeof mon_name[0]))
604 else mn = mon_name[timeptr->tm_mon];
605 printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
607 timeptr->tm_mday, timeptr->tm_hour,
608 timeptr->tm_min, timeptr->tm_sec);
610 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
611 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
614 if (trail < 0 && lead > 0) {
617 } else if (lead < 0 && trail > 0) {
623 else printf("%d%d", lead, ((trail < 0) ? -trail : trail));