3 static char elsieid[] = "@(#)zdump.c 7.28";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
8 ** This code has been made independent of the rest of the time
9 ** conversion package to increase confidence in the verification it provides.
10 ** You can use this code to help in verifying other implementations.
13 #include "stdio.h" /* for stdout, stderr, perror */
14 #include "string.h" /* for strcpy */
15 #include "sys/types.h" /* for time_t */
16 #include "time.h" /* for struct tm */
17 #include "stdlib.h" /* for exit, malloc, atoi */
19 #ifndef MAX_STRING_LENGTH
20 #define MAX_STRING_LENGTH 1024
21 #endif /* !defined MAX_STRING_LENGTH */
25 #endif /* !defined TRUE */
29 #endif /* !defined FALSE */
32 #define EXIT_SUCCESS 0
33 #endif /* !defined EXIT_SUCCESS */
36 #define EXIT_FAILURE 1
37 #endif /* !defined EXIT_FAILURE */
41 #endif /* !defined SECSPERMIN */
44 #define MINSPERHOUR 60
45 #endif /* !defined MINSPERHOUR */
48 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
49 #endif /* !defined SECSPERHOUR */
52 #define HOURSPERDAY 24
53 #endif /* !defined HOURSPERDAY */
56 #define EPOCH_YEAR 1970
57 #endif /* !defined EPOCH_YEAR */
60 #define TM_YEAR_BASE 1900
61 #endif /* !defined TM_YEAR_BASE */
64 #define DAYSPERNYEAR 365
65 #endif /* !defined DAYSPERNYEAR */
68 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
69 #endif /* !defined isleap */
72 #include "locale.h" /* for setlocale */
74 #endif /* HAVE_GETTEXT - 0 */
79 #endif /* defined lint */
83 #endif /* defined __GNUC__ */
84 #endif /* !defined lint */
85 #endif /* !defined GNUC_or_lint */
89 #define INITIALIZE(x) ((x) = 0)
90 #endif /* defined GNUC_or_lint */
93 #endif /* !defined GNUC_or_lint */
94 #endif /* !defined INITIALIZE */
97 ** For the benefit of GNU folk...
98 ** `_(MSGID)' uses the current locale's message library string for MSGID.
99 ** The default is to use gettext if available, and use MSGID otherwise.
104 #define _(msgid) gettext(msgid)
105 #else /* !(HAVE_GETTEXT - 0) */
106 #define _(msgid) msgid
107 #endif /* !(HAVE_GETTEXT - 0) */
108 #endif /* !defined _ */
111 #define TZ_DOMAIN "tz"
112 #endif /* !defined TZ_DOMAIN */
117 #endif /* defined __STDC__ */
120 #endif /* !defined __STDC__ */
121 #endif /* !defined P */
123 extern char ** environ;
124 extern int getopt P((int argc, char * const argv[],
125 const char * options));
126 extern char * optarg;
128 extern char * tzname[2];
130 static char * abbr P((struct tm * tmp));
131 static long delta P((struct tm * newp, struct tm * oldp));
132 static time_t hunt P((char * name, time_t lot, time_t hit));
133 static size_t longest;
134 static char * progname;
135 static void show P((char * zone, time_t t, int v));
145 register char * cutoff;
146 register int cutyear;
147 register long cuttime;
158 (void) setlocale(LC_MESSAGES, "");
160 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
161 #endif /* defined(TEXTDOMAINDIR) */
162 (void) textdomain(TZ_DOMAIN);
163 #endif /* HAVE_GETTEXT - 0 */
167 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
170 else cutoff = optarg;
171 if ((c != EOF && c != -1) ||
172 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
173 (void) fprintf(stderr,
174 _("%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n"),
176 (void) exit(EXIT_FAILURE);
178 if (cutoff != NULL) {
181 cutyear = atoi(cutoff);
183 for (y = EPOCH_YEAR; y < cutyear; ++y)
184 cuttime += DAYSPERNYEAR + isleap(y);
185 cuttime *= SECSPERHOUR * HOURSPERDAY;
189 for (i = optind; i < argc; ++i)
190 if (strlen(argv[i]) > longest)
191 longest = strlen(argv[i]);
192 for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
198 for (i = 0; environ[i] != NULL; ++i)
200 fakeenv = (char **) malloc((size_t) ((i + 2) *
202 if (fakeenv == NULL ||
203 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
204 (void) perror(progname);
205 (void) exit(EXIT_FAILURE);
208 (void) strcpy(fakeenv[to++], "TZ=");
209 for (from = 0; environ[from] != NULL; ++from)
210 if (strncmp(environ[from], "TZ=", 3) != 0)
211 fakeenv[to++] = environ[from];
215 for (i = optind; i < argc; ++i) {
216 static char buf[MAX_STRING_LENGTH];
218 (void) strcpy(&fakeenv[0][3], argv[i]);
220 show(argv[i], now, FALSE);
224 ** Get lowest value of t.
227 if (t > 0) /* time_t is unsigned */
229 show(argv[i], t, TRUE);
230 t += SECSPERHOUR * HOURSPERDAY;
231 show(argv[i], t, TRUE);
233 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
235 if (cutoff != NULL && t >= cuttime)
237 newt = t + SECSPERHOUR * 12;
238 if (cutoff != NULL && newt >= cuttime)
242 newtm = *localtime(&newt);
243 if (delta(&newtm, &tm) != (newt - t) ||
244 newtm.tm_isdst != tm.tm_isdst ||
245 strcmp(abbr(&newtm), buf) != 0) {
246 newt = hunt(argv[i], t, newt);
247 newtm = *localtime(&newt);
248 (void) strncpy(buf, abbr(&newtm),
255 ** Get highest value of t.
258 if (t < 0) /* time_t is signed */
260 t -= SECSPERHOUR * HOURSPERDAY;
261 show(argv[i], t, TRUE);
262 t += SECSPERHOUR * HOURSPERDAY;
263 show(argv[i], t, TRUE);
265 if (fflush(stdout) || ferror(stdout)) {
266 (void) fprintf(stderr, _("%s: Error writing "),
268 (void) perror(_("standard output"));
269 (void) exit(EXIT_FAILURE);
273 /* gcc -Wall pacifier */
287 static char loab[MAX_STRING_LENGTH];
289 lotm = *localtime(&lot);
290 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
291 while ((hit - lot) >= 2) {
292 t = lot / 2 + hit / 2;
298 if (delta(&tm, &lotm) == (t - lot) &&
299 tm.tm_isdst == lotm.tm_isdst &&
300 strcmp(abbr(&tm), loab) == 0) {
305 show(name, lot, TRUE);
306 show(name, hit, TRUE);
311 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
322 if (newp->tm_year < oldp->tm_year)
323 return -delta(oldp, newp);
325 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
326 result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
327 result += newp->tm_yday - oldp->tm_yday;
328 result *= HOURSPERDAY;
329 result += newp->tm_hour - oldp->tm_hour;
330 result *= MINSPERHOUR;
331 result += newp->tm_min - oldp->tm_min;
332 result *= SECSPERMIN;
333 result += newp->tm_sec - oldp->tm_sec;
345 (void) printf("%-*s ", (int) longest, zone);
347 (void) printf("%.24s UTC = ", asctime(gmtime(&t)));
349 (void) printf("%.24s", asctime(tmp));
350 if (*abbr(tmp) != '\0')
351 (void) printf(" %s", abbr(tmp));
353 (void) printf(" isdst=%d", tmp->tm_isdst);
355 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
356 #endif /* defined TM_GMTOFF */
365 register char * result;
368 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
370 result = tzname[tmp->tm_isdst];
371 return (result == NULL) ? &nada : result;