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. */
12 #include <float.h> /* for FLT_MAX and DBL_MAX */
13 #include <stdio.h> /* for stdout, stderr */
14 #include <stdlib.h> /* for exit, malloc, atoi */
15 #include <string.h> /* for strcpy */
16 #include <sys/types.h> /* for time_t */
17 #include <time.h> /* for struct tm */
21 #define ZDUMP_LO_YEAR (-500)
22 #endif /* !defined ZDUMP_LO_YEAR */
25 #define ZDUMP_HI_YEAR 2500
26 #endif /* !defined ZDUMP_HI_YEAR */
28 #ifndef MAX_STRING_LENGTH
29 #define MAX_STRING_LENGTH 1024
30 #endif /* !defined MAX_STRING_LENGTH */
34 #endif /* !defined TRUE */
38 #endif /* !defined FALSE */
41 #define EXIT_SUCCESS 0
42 #endif /* !defined EXIT_SUCCESS */
45 #define EXIT_FAILURE 1
46 #endif /* !defined EXIT_FAILURE */
50 #endif /* !defined SECSPERMIN */
53 #define MINSPERHOUR 60
54 #endif /* !defined MINSPERHOUR */
57 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
58 #endif /* !defined SECSPERHOUR */
61 #define HOURSPERDAY 24
62 #endif /* !defined HOURSPERDAY */
65 #define EPOCH_YEAR 1970
66 #endif /* !defined EPOCH_YEAR */
69 #define TM_YEAR_BASE 1900
70 #endif /* !defined TM_YEAR_BASE */
73 #define DAYSPERNYEAR 365
74 #endif /* !defined DAYSPERNYEAR */
77 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
78 #endif /* !defined isleap */
82 ** See tzfile.h for details on isleap_sum.
84 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
85 #endif /* !defined isleap_sum */
87 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
88 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
89 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
94 #else /* !defined lint */
97 #endif /* defined __GNUC__ */
98 #endif /* !defined lint */
99 #endif /* !defined GNUC_or_lint */
103 #define INITIALIZE(x) ((x) = 0)
104 #else /* !defined GNUC_or_lint */
105 #define INITIALIZE(x)
106 #endif /* !defined GNUC_or_lint */
107 #endif /* !defined INITIALIZE */
110 ** For the benefit of GNU folk...
111 ** `_(MSGID)' uses the current locale's message library string for MSGID.
112 ** The default is to use gettext if available, and use MSGID otherwise.
116 #define _(msgid) msgid
117 #endif /* !defined _ */
120 #define TZ_DOMAIN "tz"
121 #endif /* !defined TZ_DOMAIN */
123 extern char ** environ;
124 extern char * tzname[2];
126 static time_t absolute_min_time;
127 static time_t absolute_max_time;
128 static size_t longest;
131 static char *abbr(struct tm *tmp);
132 static void abbrok(const char *abbrp, const char *zone);
133 static long delta(struct tm *newp, struct tm *oldp);
134 static void dumptime(const struct tm *tmp);
135 static time_t hunt(char *name, time_t lot, time_t hit);
136 static void setabsolutes(void);
137 static void show(char *zone, time_t t, int v);
138 static const char *tformat(void);
139 static time_t yeartot(long y);
140 static void usage(void);
143 #define my_localtime localtime
144 #else /* !defined TYPECHECK */
146 my_localtime(time_t *tp)
151 if (tp != NULL && tmp != NULL) {
157 if (t - *tp >= 1 || *tp - t >= 1) {
159 fprintf(stderr, "\n%s: ", progname);
160 fprintf(stderr, tformat(), *tp);
161 fprintf(stderr, " ->");
162 fprintf(stderr, " year=%d", tmp->tm_year);
163 fprintf(stderr, " mon=%d", tmp->tm_mon);
164 fprintf(stderr, " mday=%d", tmp->tm_mday);
165 fprintf(stderr, " hour=%d", tmp->tm_hour);
166 fprintf(stderr, " min=%d", tmp->tm_min);
167 fprintf(stderr, " sec=%d", tmp->tm_sec);
168 fprintf(stderr, " isdst=%d", tmp->tm_isdst);
169 fprintf(stderr, " -> ");
170 fprintf(stderr, tformat(), t);
171 fprintf(stderr, "\n");
176 #endif /* !defined TYPECHECK */
179 abbrok(const char * const abbrp, const char * const zone)
188 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
191 wp = _("lacks alphabetic at start");
192 else if (cp - abbrp < 3)
193 wp = _("has fewer than 3 alphabetics");
194 else if (cp - abbrp > 6)
195 wp = _("has more than 6 alphabetics");
196 if (wp == NULL && (*cp == '+' || *cp == '-')) {
198 if (isascii((unsigned char) *cp) &&
199 isdigit((unsigned char) *cp))
200 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
203 wp = _("differs from POSIX standard");
208 warnx(_("warning: zone \"%s\" abbreviation \"%s\" %s\n"),
214 main(int argc, char *argv[])
220 long cutloyear = ZDUMP_LO_YEAR;
221 long cuthiyear = ZDUMP_HI_YEAR;
233 INITIALIZE(cutlotime);
234 INITIALIZE(cuthitime);
237 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
240 else cutarg = optarg;
241 if ((c != EOF && c != -1) ||
242 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
246 if (cutarg != NULL) {
251 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
253 } else if (sscanf(cutarg, "%ld,%ld%c",
254 &lo, &hi, &dummy) == 2) {
259 _("wild -c argument %s\n"),
264 cutlotime = yeartot(cutloyear);
265 cuthitime = yeartot(cuthiyear);
269 for (i = optind; i < argc; ++i)
270 if (strlen(argv[i]) > longest)
271 longest = strlen(argv[i]);
276 for (i = 0; environ[i] != NULL; ++i)
278 fakeenv = (char **) malloc((size_t) ((i + 2) *
280 if (fakeenv == NULL ||
281 (fakeenv[0] = (char *) malloc((size_t) (longest +
284 _("malloc() failed"));
286 strcpy(fakeenv[to++], "TZ=");
287 for (from = 0; environ[from] != NULL; ++from)
288 if (strncmp(environ[from], "TZ=", 3) != 0)
289 fakeenv[to++] = environ[from];
293 for (i = optind; i < argc; ++i) {
294 static char buf[MAX_STRING_LENGTH];
296 strcpy(&fakeenv[0][3], argv[i]);
298 show(argv[i], now, FALSE);
302 t = absolute_min_time;
303 show(argv[i], t, TRUE);
304 t += SECSPERHOUR * HOURSPERDAY;
305 show(argv[i], t, TRUE);
308 tmp = my_localtime(&t);
311 strncpy(buf, abbr(&tm), (sizeof buf) - 1);
314 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
316 newt = t + SECSPERHOUR * 12;
317 newtmp = localtime(&newt);
320 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
321 (delta(&newtm, &tm) != (newt - t) ||
322 newtm.tm_isdst != tm.tm_isdst ||
323 strcmp(abbr(&newtm), buf) != 0)) {
324 newt = hunt(argv[i], t, newt);
325 newtmp = localtime(&newt);
326 if (newtmp != NULL) {
337 t = absolute_max_time;
338 t -= SECSPERHOUR * HOURSPERDAY;
339 show(argv[i], t, TRUE);
340 t += SECSPERHOUR * HOURSPERDAY;
341 show(argv[i], t, TRUE);
343 if (fflush(stdout) || ferror(stdout))
344 errx(EXIT_FAILURE, _("error writing standard output"));
346 /* If exit fails to exit... */
353 if (0.5 == (time_t) 0.5) {
355 ** time_t is floating.
357 if (sizeof (time_t) == sizeof (float)) {
358 absolute_min_time = (time_t) -FLT_MAX;
359 absolute_max_time = (time_t) FLT_MAX;
360 } else if (sizeof (time_t) == sizeof (double)) {
361 absolute_min_time = (time_t) -DBL_MAX;
362 absolute_max_time = (time_t) DBL_MAX;
365 _("use of -v on system with floating time_t other than float or double\n"));
367 } else if (0 > (time_t) -1) {
369 ** time_t is signed. Assume overflow wraps around.
379 absolute_max_time = t;
381 absolute_min_time = t - 1;
382 if (t < absolute_min_time)
383 absolute_min_time = t;
386 ** time_t is unsigned.
388 absolute_min_time = 0;
389 absolute_max_time = absolute_min_time - 1;
396 fprintf(stderr, _("usage: zdump [-v] [-c [loyear,]hiyear] zonename ...\n"));
401 yeartot(const long y)
411 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
413 if (t > absolute_max_time - seconds) {
414 t = absolute_max_time;
420 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
421 if (t < absolute_min_time + seconds) {
422 t = absolute_min_time;
432 hunt(char *name, time_t lot, time_t hit)
440 char loab[MAX_STRING_LENGTH];
442 lotmp = my_localtime(&lot);
445 strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
448 diff = (long) (hit - lot);
457 tmp = my_localtime(&t);
460 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
461 (delta(&tm, &lotm) == (t - lot) &&
462 tm.tm_isdst == lotm.tm_isdst &&
463 strcmp(abbr(&tm), loab) == 0)) {
469 show(name, lot, TRUE);
470 show(name, hit, TRUE);
475 ** Thanks to Paul Eggert for logic used in delta.
479 delta(struct tm *newp, struct tm *oldp)
484 if (newp->tm_year < oldp->tm_year)
485 return -delta(oldp, newp);
487 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
488 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
489 result += newp->tm_yday - oldp->tm_yday;
490 result *= HOURSPERDAY;
491 result += newp->tm_hour - oldp->tm_hour;
492 result *= MINSPERHOUR;
493 result += newp->tm_min - oldp->tm_min;
494 result *= SECSPERMIN;
495 result += newp->tm_sec - oldp->tm_sec;
500 show(char *zone, time_t t, int v)
504 printf("%-*s ", (int) longest, zone);
508 printf(tformat(), t);
515 tmp = my_localtime(&t);
518 if (*abbr(tmp) != '\0')
519 printf(" %s", abbr(tmp));
521 printf(" isdst=%d", tmp->tm_isdst);
523 printf(" gmtoff=%ld", tmp->TM_GMTOFF);
524 #endif /* defined TM_GMTOFF */
528 if (tmp != NULL && *abbr(tmp) != '\0')
529 abbrok(abbr(tmp), zone);
538 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
540 result = tzname[tmp->tm_isdst];
541 return (result == NULL) ? &nada : result;
545 ** The code below can fail on certain theoretical systems;
546 ** it works on all known real-world systems as of 2004-12-30.
552 if (0.5 == (time_t) 0.5) { /* floating */
553 if (sizeof (time_t) > sizeof (double))
557 if (0 > (time_t) -1) { /* signed */
558 if (sizeof (time_t) > sizeof (long))
560 if (sizeof (time_t) > sizeof (int))
564 if (sizeof (time_t) > sizeof (unsigned long))
566 if (sizeof (time_t) > sizeof (unsigned int))
572 dumptime(const struct tm *timeptr)
574 static const char wday_name[][3] = {
575 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
577 static const char mon_name[][3] = {
578 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
579 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
586 if (timeptr == NULL) {
591 ** The packaged versions of localtime and gmtime never put out-of-range
592 ** values in tm_wday or tm_mon, but since this code might be compiled
593 ** with other (perhaps experimental) versions, paranoia is in order.
595 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
596 (int) (sizeof wday_name / sizeof wday_name[0]))
598 else wn = wday_name[timeptr->tm_wday];
599 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
600 (int) (sizeof mon_name / sizeof mon_name[0]))
602 else mn = mon_name[timeptr->tm_mon];
603 printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
605 timeptr->tm_mday, timeptr->tm_hour,
606 timeptr->tm_min, timeptr->tm_sec);
608 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
609 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
612 if (trail < 0 && lead > 0) {
615 } else if (lead < 0 && trail > 0) {
621 else printf("%d%d", lead, ((trail < 0) ? -trail : trail));