Update.
[glibc.git] / timezone / zdump.c
blob094e17251a54fea2911118c2d4fb595bc3658001
1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)zdump.c 7.28";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
7 /*
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 */
23 #ifndef TRUE
24 #define TRUE 1
25 #endif /* !defined TRUE */
27 #ifndef FALSE
28 #define FALSE 0
29 #endif /* !defined FALSE */
31 #ifndef EXIT_SUCCESS
32 #define EXIT_SUCCESS 0
33 #endif /* !defined EXIT_SUCCESS */
35 #ifndef EXIT_FAILURE
36 #define EXIT_FAILURE 1
37 #endif /* !defined EXIT_FAILURE */
39 #ifndef SECSPERMIN
40 #define SECSPERMIN 60
41 #endif /* !defined SECSPERMIN */
43 #ifndef MINSPERHOUR
44 #define MINSPERHOUR 60
45 #endif /* !defined MINSPERHOUR */
47 #ifndef SECSPERHOUR
48 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
49 #endif /* !defined SECSPERHOUR */
51 #ifndef HOURSPERDAY
52 #define HOURSPERDAY 24
53 #endif /* !defined HOURSPERDAY */
55 #ifndef EPOCH_YEAR
56 #define EPOCH_YEAR 1970
57 #endif /* !defined EPOCH_YEAR */
59 #ifndef TM_YEAR_BASE
60 #define TM_YEAR_BASE 1900
61 #endif /* !defined TM_YEAR_BASE */
63 #ifndef DAYSPERNYEAR
64 #define DAYSPERNYEAR 365
65 #endif /* !defined DAYSPERNYEAR */
67 #ifndef isleap
68 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
69 #endif /* !defined isleap */
71 #if HAVE_GETTEXT - 0
72 #include "locale.h" /* for setlocale */
73 #include "libintl.h"
74 #endif /* HAVE_GETTEXT - 0 */
76 #ifndef GNUC_or_lint
77 #ifdef lint
78 #define GNUC_or_lint
79 #endif /* defined lint */
80 #ifndef lint
81 #ifdef __GNUC__
82 #define GNUC_or_lint
83 #endif /* defined __GNUC__ */
84 #endif /* !defined lint */
85 #endif /* !defined GNUC_or_lint */
87 #ifndef INITIALIZE
88 #ifdef GNUC_or_lint
89 #define INITIALIZE(x) ((x) = 0)
90 #endif /* defined GNUC_or_lint */
91 #ifndef GNUC_or_lint
92 #define INITIALIZE(x)
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.
102 #ifndef _
103 #if HAVE_GETTEXT - 0
104 #define _(msgid) gettext(msgid)
105 #else /* !(HAVE_GETTEXT - 0) */
106 #define _(msgid) msgid
107 #endif /* !(HAVE_GETTEXT - 0) */
108 #endif /* !defined _ */
110 #ifndef TZ_DOMAIN
111 #define TZ_DOMAIN "tz"
112 #endif /* !defined TZ_DOMAIN */
114 #ifndef P
115 #ifdef __STDC__
116 #define P(x) x
117 #endif /* defined __STDC__ */
118 #ifndef __STDC__
119 #define P(x) ()
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;
127 extern int optind;
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));
138 main(argc, argv)
139 int argc;
140 char * argv[];
142 register int i;
143 register int c;
144 register int vflag;
145 register char * cutoff;
146 register int cutyear;
147 register long cuttime;
148 char ** fakeenv;
149 time_t now;
150 time_t t;
151 time_t newt;
152 time_t hibit;
153 struct tm tm;
154 struct tm newtm;
156 INITIALIZE(cuttime);
157 #if HAVE_GETTEXT - 0
158 (void) setlocale(LC_MESSAGES, "");
159 #ifdef TZ_DOMAINDIR
160 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
161 #endif /* defined(TEXTDOMAINDIR) */
162 (void) textdomain(TZ_DOMAIN);
163 #endif /* HAVE_GETTEXT - 0 */
164 progname = argv[0];
165 vflag = 0;
166 cutoff = NULL;
167 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
168 if (c == 'v')
169 vflag = 1;
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"),
175 argv[0], argv[0]);
176 (void) exit(EXIT_FAILURE);
178 if (cutoff != NULL) {
179 int y;
181 cutyear = atoi(cutoff);
182 cuttime = 0;
183 for (y = EPOCH_YEAR; y < cutyear; ++y)
184 cuttime += DAYSPERNYEAR + isleap(y);
185 cuttime *= SECSPERHOUR * HOURSPERDAY;
187 (void) time(&now);
188 longest = 0;
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)
193 continue;
195 register int from;
196 register int to;
198 for (i = 0; environ[i] != NULL; ++i)
199 continue;
200 fakeenv = (char **) malloc((size_t) ((i + 2) *
201 sizeof *fakeenv));
202 if (fakeenv == NULL ||
203 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
204 (void) perror(progname);
205 (void) exit(EXIT_FAILURE);
207 to = 0;
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];
212 fakeenv[to] = NULL;
213 environ = fakeenv;
215 for (i = optind; i < argc; ++i) {
216 static char buf[MAX_STRING_LENGTH];
218 (void) strcpy(&fakeenv[0][3], argv[i]);
219 if (!vflag) {
220 show(argv[i], now, FALSE);
221 continue;
224 ** Get lowest value of t.
226 t = hibit;
227 if (t > 0) /* time_t is unsigned */
228 t = 0;
229 show(argv[i], t, TRUE);
230 t += SECSPERHOUR * HOURSPERDAY;
231 show(argv[i], t, TRUE);
232 tm = *localtime(&t);
233 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
234 for ( ; ; ) {
235 if (cutoff != NULL && t >= cuttime)
236 break;
237 newt = t + SECSPERHOUR * 12;
238 if (cutoff != NULL && newt >= cuttime)
239 break;
240 if (newt <= t)
241 break;
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),
249 (sizeof buf) - 1);
251 t = newt;
252 tm = newtm;
255 ** Get highest value of t.
257 t = ~((time_t) 0);
258 if (t < 0) /* time_t is signed */
259 t &= ~hibit;
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 "),
267 argv[0]);
268 (void) perror(_("standard output"));
269 (void) exit(EXIT_FAILURE);
271 exit(EXIT_SUCCESS);
273 /* gcc -Wall pacifier */
274 for ( ; ; )
275 continue;
278 static time_t
279 hunt(name, lot, hit)
280 char * name;
281 time_t lot;
282 time_t hit;
284 time_t t;
285 struct tm lotm;
286 struct tm tm;
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;
293 if (t <= lot)
294 ++t;
295 else if (t >= hit)
296 --t;
297 tm = *localtime(&t);
298 if (delta(&tm, &lotm) == (t - lot) &&
299 tm.tm_isdst == lotm.tm_isdst &&
300 strcmp(abbr(&tm), loab) == 0) {
301 lot = t;
302 lotm = tm;
303 } else hit = t;
305 show(name, lot, TRUE);
306 show(name, hit, TRUE);
307 return hit;
311 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
314 static long
315 delta(newp, oldp)
316 struct tm * newp;
317 struct tm * oldp;
319 long result;
320 int tmy;
322 if (newp->tm_year < oldp->tm_year)
323 return -delta(oldp, newp);
324 result = 0;
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;
334 return result;
337 static void
338 show(zone, t, v)
339 char * zone;
340 time_t t;
341 int v;
343 struct tm * tmp;
345 (void) printf("%-*s ", (int) longest, zone);
346 if (v)
347 (void) printf("%.24s UTC = ", asctime(gmtime(&t)));
348 tmp = localtime(&t);
349 (void) printf("%.24s", asctime(tmp));
350 if (*abbr(tmp) != '\0')
351 (void) printf(" %s", abbr(tmp));
352 if (v) {
353 (void) printf(" isdst=%d", tmp->tm_isdst);
354 #ifdef TM_GMTOFF
355 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
356 #endif /* defined TM_GMTOFF */
358 (void) printf("\n");
361 static char *
362 abbr(tmp)
363 struct tm * tmp;
365 register char * result;
366 static char nada;
368 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
369 return &nada;
370 result = tzname[tmp->tm_isdst];
371 return (result == NULL) ? &nada : result;