zdump.c revision 1.26 1 1.26 christos /* $NetBSD: zdump.c,v 1.26 2012/10/24 00:10:03 christos Exp $ */
2 1.17 mlelstv /*
3 1.17 mlelstv ** This file is in the public domain, so clarified as of
4 1.17 mlelstv ** 2009-05-17 by Arthur David Olson.
5 1.17 mlelstv */
6 1.2 jtc
7 1.6 christos #include <sys/cdefs.h>
8 1.1 jtc #ifndef lint
9 1.26 christos __RCSID("$NetBSD: zdump.c,v 1.26 2012/10/24 00:10:03 christos Exp $");
10 1.1 jtc #endif /* !defined lint */
11 1.1 jtc
12 1.25 christos #include "version.h"
13 1.1 jtc /*
14 1.1 jtc ** This code has been made independent of the rest of the time
15 1.1 jtc ** conversion package to increase confidence in the verification it provides.
16 1.1 jtc ** You can use this code to help in verifying other implementations.
17 1.1 jtc */
18 1.1 jtc
19 1.15 christos #include "stdio.h" /* for stdout, stderr */
20 1.1 jtc #include "string.h" /* for strcpy */
21 1.1 jtc #include "sys/types.h" /* for time_t */
22 1.1 jtc #include "time.h" /* for struct tm */
23 1.1 jtc #include "stdlib.h" /* for exit, malloc, atoi */
24 1.15 christos #include <err.h>
25 1.17 mlelstv #include "float.h" /* for FLT_MAX and DBL_MAX */
26 1.17 mlelstv #include "ctype.h" /* for isalpha et al. */
27 1.17 mlelstv #ifndef isascii
28 1.17 mlelstv #define isascii(x) 1
29 1.17 mlelstv #endif /* !defined isascii */
30 1.17 mlelstv
31 1.17 mlelstv #ifndef ZDUMP_LO_YEAR
32 1.17 mlelstv #define ZDUMP_LO_YEAR (-500)
33 1.17 mlelstv #endif /* !defined ZDUMP_LO_YEAR */
34 1.17 mlelstv
35 1.17 mlelstv #ifndef ZDUMP_HI_YEAR
36 1.17 mlelstv #define ZDUMP_HI_YEAR 2500
37 1.17 mlelstv #endif /* !defined ZDUMP_HI_YEAR */
38 1.1 jtc
39 1.1 jtc #ifndef MAX_STRING_LENGTH
40 1.1 jtc #define MAX_STRING_LENGTH 1024
41 1.1 jtc #endif /* !defined MAX_STRING_LENGTH */
42 1.1 jtc
43 1.1 jtc #ifndef TRUE
44 1.1 jtc #define TRUE 1
45 1.1 jtc #endif /* !defined TRUE */
46 1.1 jtc
47 1.1 jtc #ifndef FALSE
48 1.1 jtc #define FALSE 0
49 1.1 jtc #endif /* !defined FALSE */
50 1.1 jtc
51 1.1 jtc #ifndef EXIT_SUCCESS
52 1.1 jtc #define EXIT_SUCCESS 0
53 1.1 jtc #endif /* !defined EXIT_SUCCESS */
54 1.1 jtc
55 1.1 jtc #ifndef EXIT_FAILURE
56 1.1 jtc #define EXIT_FAILURE 1
57 1.1 jtc #endif /* !defined EXIT_FAILURE */
58 1.1 jtc
59 1.1 jtc #ifndef SECSPERMIN
60 1.1 jtc #define SECSPERMIN 60
61 1.1 jtc #endif /* !defined SECSPERMIN */
62 1.1 jtc
63 1.1 jtc #ifndef MINSPERHOUR
64 1.1 jtc #define MINSPERHOUR 60
65 1.1 jtc #endif /* !defined MINSPERHOUR */
66 1.1 jtc
67 1.1 jtc #ifndef SECSPERHOUR
68 1.1 jtc #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
69 1.1 jtc #endif /* !defined SECSPERHOUR */
70 1.1 jtc
71 1.1 jtc #ifndef HOURSPERDAY
72 1.1 jtc #define HOURSPERDAY 24
73 1.1 jtc #endif /* !defined HOURSPERDAY */
74 1.1 jtc
75 1.1 jtc #ifndef EPOCH_YEAR
76 1.1 jtc #define EPOCH_YEAR 1970
77 1.1 jtc #endif /* !defined EPOCH_YEAR */
78 1.1 jtc
79 1.1 jtc #ifndef TM_YEAR_BASE
80 1.1 jtc #define TM_YEAR_BASE 1900
81 1.1 jtc #endif /* !defined TM_YEAR_BASE */
82 1.1 jtc
83 1.1 jtc #ifndef DAYSPERNYEAR
84 1.1 jtc #define DAYSPERNYEAR 365
85 1.1 jtc #endif /* !defined DAYSPERNYEAR */
86 1.1 jtc
87 1.1 jtc #ifndef isleap
88 1.17 mlelstv #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
89 1.1 jtc #endif /* !defined isleap */
90 1.1 jtc
91 1.17 mlelstv #ifndef isleap_sum
92 1.17 mlelstv /*
93 1.17 mlelstv ** See tzfile.h for details on isleap_sum.
94 1.17 mlelstv */
95 1.17 mlelstv #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
96 1.17 mlelstv #endif /* !defined isleap_sum */
97 1.17 mlelstv
98 1.17 mlelstv #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
99 1.17 mlelstv #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
100 1.17 mlelstv #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
101 1.17 mlelstv
102 1.17 mlelstv #ifndef HAVE_GETTEXT
103 1.17 mlelstv #define HAVE_GETTEXT 0
104 1.17 mlelstv #endif
105 1.17 mlelstv #if HAVE_GETTEXT
106 1.3 jtc #include "locale.h" /* for setlocale */
107 1.3 jtc #include "libintl.h"
108 1.17 mlelstv #endif /* HAVE_GETTEXT */
109 1.3 jtc
110 1.1 jtc #ifndef GNUC_or_lint
111 1.1 jtc #ifdef lint
112 1.1 jtc #define GNUC_or_lint
113 1.17 mlelstv #else /* !defined lint */
114 1.1 jtc #ifdef __GNUC__
115 1.1 jtc #define GNUC_or_lint
116 1.1 jtc #endif /* defined __GNUC__ */
117 1.1 jtc #endif /* !defined lint */
118 1.1 jtc #endif /* !defined GNUC_or_lint */
119 1.1 jtc
120 1.26 christos #ifndef __pure
121 1.26 christos #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
122 1.26 christos # define __pure __attribute__ ((__pure__))
123 1.26 christos #else
124 1.26 christos # define __pure /* empty */
125 1.26 christos #endif
126 1.26 christos #endif
127 1.26 christos
128 1.1 jtc #ifndef INITIALIZE
129 1.1 jtc #ifdef GNUC_or_lint
130 1.1 jtc #define INITIALIZE(x) ((x) = 0)
131 1.17 mlelstv #else /* !defined GNUC_or_lint */
132 1.1 jtc #define INITIALIZE(x)
133 1.1 jtc #endif /* !defined GNUC_or_lint */
134 1.1 jtc #endif /* !defined INITIALIZE */
135 1.1 jtc
136 1.3 jtc /*
137 1.3 jtc ** For the benefit of GNU folk...
138 1.3 jtc ** `_(MSGID)' uses the current locale's message library string for MSGID.
139 1.3 jtc ** The default is to use gettext if available, and use MSGID otherwise.
140 1.3 jtc */
141 1.3 jtc
142 1.3 jtc #ifndef _
143 1.17 mlelstv #if HAVE_GETTEXT
144 1.3 jtc #define _(msgid) gettext(msgid)
145 1.17 mlelstv #else /* !HAVE_GETTEXT */
146 1.21 christos #define _(msgid) msgid
147 1.17 mlelstv #endif /* !HAVE_GETTEXT */
148 1.3 jtc #endif /* !defined _ */
149 1.3 jtc
150 1.3 jtc #ifndef TZ_DOMAIN
151 1.3 jtc #define TZ_DOMAIN "tz"
152 1.3 jtc #endif /* !defined TZ_DOMAIN */
153 1.3 jtc
154 1.1 jtc extern char ** environ;
155 1.17 mlelstv extern int getopt(int argc, char * const argv[],
156 1.17 mlelstv const char * options);
157 1.1 jtc extern char * optarg;
158 1.1 jtc extern int optind;
159 1.1 jtc
160 1.17 mlelstv static time_t absolute_min_time;
161 1.17 mlelstv static time_t absolute_max_time;
162 1.5 jtc static size_t longest;
163 1.1 jtc static char * progname;
164 1.17 mlelstv static int warned;
165 1.17 mlelstv
166 1.17 mlelstv static const char * abbr(struct tm * tmp);
167 1.17 mlelstv static void abbrok(const char * abbrp, const char * zone);
168 1.26 christos static long delta(struct tm * newp, struct tm * oldp) __pure;
169 1.17 mlelstv static void dumptime(const struct tm * tmp);
170 1.17 mlelstv static time_t hunt(char * name, time_t lot, time_t hit);
171 1.17 mlelstv int main(int, char **);
172 1.17 mlelstv static void setabsolutes(void);
173 1.17 mlelstv static void show(char * zone, time_t t, int v);
174 1.17 mlelstv static const char * tformat(void);
175 1.26 christos static time_t yeartot(long y) __pure;
176 1.17 mlelstv
177 1.17 mlelstv #ifndef TYPECHECK
178 1.17 mlelstv #define my_localtime localtime
179 1.17 mlelstv #else /* !defined TYPECHECK */
180 1.17 mlelstv static struct tm *
181 1.26 christos my_localtime(time_t *tp)
182 1.17 mlelstv {
183 1.26 christos struct tm *tmp;
184 1.17 mlelstv
185 1.17 mlelstv tmp = localtime(tp);
186 1.17 mlelstv if (tp != NULL && tmp != NULL) {
187 1.17 mlelstv struct tm tm;
188 1.26 christos time_t t;
189 1.17 mlelstv
190 1.17 mlelstv tm = *tmp;
191 1.17 mlelstv t = mktime(&tm);
192 1.17 mlelstv if (t - *tp >= 1 || *tp - t >= 1) {
193 1.17 mlelstv (void) fflush(stdout);
194 1.17 mlelstv (void) fprintf(stderr, "\n%s: ", progname);
195 1.17 mlelstv (void) fprintf(stderr, tformat(), *tp);
196 1.17 mlelstv (void) fprintf(stderr, " ->");
197 1.17 mlelstv (void) fprintf(stderr, " year=%d", tmp->tm_year);
198 1.17 mlelstv (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
199 1.17 mlelstv (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
200 1.17 mlelstv (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
201 1.17 mlelstv (void) fprintf(stderr, " min=%d", tmp->tm_min);
202 1.17 mlelstv (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
203 1.17 mlelstv (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
204 1.17 mlelstv (void) fprintf(stderr, " -> ");
205 1.17 mlelstv (void) fprintf(stderr, tformat(), t);
206 1.17 mlelstv (void) fprintf(stderr, "\n");
207 1.17 mlelstv }
208 1.17 mlelstv }
209 1.17 mlelstv return tmp;
210 1.17 mlelstv }
211 1.17 mlelstv #endif /* !defined TYPECHECK */
212 1.17 mlelstv
213 1.17 mlelstv static void
214 1.26 christos abbrok(const char *const abbrp, const char *const zone)
215 1.17 mlelstv {
216 1.26 christos const char *cp;
217 1.26 christos const char *wp;
218 1.17 mlelstv
219 1.17 mlelstv if (warned)
220 1.17 mlelstv return;
221 1.17 mlelstv cp = abbrp;
222 1.17 mlelstv wp = NULL;
223 1.17 mlelstv while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
224 1.17 mlelstv ++cp;
225 1.17 mlelstv if (cp - abbrp == 0)
226 1.17 mlelstv wp = _("lacks alphabetic at start");
227 1.17 mlelstv else if (cp - abbrp < 3)
228 1.17 mlelstv wp = _("has fewer than 3 alphabetics");
229 1.17 mlelstv else if (cp - abbrp > 6)
230 1.17 mlelstv wp = _("has more than 6 alphabetics");
231 1.17 mlelstv if (wp == NULL && (*cp == '+' || *cp == '-')) {
232 1.17 mlelstv ++cp;
233 1.17 mlelstv if (isascii((unsigned char) *cp) &&
234 1.17 mlelstv isdigit((unsigned char) *cp))
235 1.17 mlelstv if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
236 1.17 mlelstv ++cp;
237 1.17 mlelstv if (*cp != '\0')
238 1.17 mlelstv wp = _("differs from POSIX standard");
239 1.17 mlelstv }
240 1.17 mlelstv if (wp == NULL)
241 1.17 mlelstv return;
242 1.17 mlelstv (void) fflush(stdout);
243 1.17 mlelstv (void) fprintf(stderr,
244 1.17 mlelstv _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
245 1.17 mlelstv progname, zone, abbrp, wp);
246 1.17 mlelstv warned = TRUE;
247 1.17 mlelstv }
248 1.17 mlelstv
249 1.24 joerg __dead static void
250 1.26 christos usage(FILE *const stream, const int status)
251 1.17 mlelstv {
252 1.17 mlelstv (void) fprintf(stream,
253 1.17 mlelstv _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
254 1.17 mlelstv \n\
255 1.17 mlelstv Report bugs to tz (at) elsie.nci.nih.gov.\n"),
256 1.22 christos progname, progname);
257 1.17 mlelstv exit(status);
258 1.17 mlelstv }
259 1.1 jtc
260 1.1 jtc int
261 1.26 christos main(int argc, char *argv[])
262 1.26 christos {
263 1.26 christos int i;
264 1.26 christos int c;
265 1.26 christos int vflag;
266 1.26 christos char * cutarg;
267 1.26 christos long cutloyear = ZDUMP_LO_YEAR;
268 1.26 christos long cuthiyear = ZDUMP_HI_YEAR;
269 1.26 christos time_t cutlotime;
270 1.26 christos time_t cuthitime;
271 1.26 christos char ** fakeenv;
272 1.26 christos time_t now;
273 1.26 christos time_t t;
274 1.26 christos time_t newt;
275 1.26 christos struct tm tm;
276 1.26 christos struct tm newtm;
277 1.26 christos struct tm * tmp;
278 1.26 christos struct tm * newtmp;
279 1.1 jtc
280 1.17 mlelstv INITIALIZE(cutlotime);
281 1.17 mlelstv INITIALIZE(cuthitime);
282 1.17 mlelstv #if HAVE_GETTEXT
283 1.17 mlelstv (void) setlocale(LC_ALL, "");
284 1.3 jtc #ifdef TZ_DOMAINDIR
285 1.3 jtc (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
286 1.17 mlelstv #endif /* defined TEXTDOMAINDIR */
287 1.3 jtc (void) textdomain(TZ_DOMAIN);
288 1.17 mlelstv #endif /* HAVE_GETTEXT */
289 1.1 jtc progname = argv[0];
290 1.14 kleink for (i = 1; i < argc; ++i)
291 1.14 kleink if (strcmp(argv[i], "--version") == 0) {
292 1.25 christos (void) printf("%s\n", TZVERSION);
293 1.17 mlelstv exit(EXIT_SUCCESS);
294 1.17 mlelstv } else if (strcmp(argv[i], "--help") == 0) {
295 1.22 christos usage(stdout, EXIT_SUCCESS);
296 1.14 kleink }
297 1.1 jtc vflag = 0;
298 1.17 mlelstv cutarg = NULL;
299 1.1 jtc while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
300 1.1 jtc if (c == 'v')
301 1.1 jtc vflag = 1;
302 1.17 mlelstv else cutarg = optarg;
303 1.5 jtc if ((c != EOF && c != -1) ||
304 1.1 jtc (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
305 1.23 christos usage(stderr, EXIT_FAILURE);
306 1.17 mlelstv }
307 1.17 mlelstv if (vflag) {
308 1.17 mlelstv if (cutarg != NULL) {
309 1.17 mlelstv long lo;
310 1.17 mlelstv long hi;
311 1.17 mlelstv char dummy;
312 1.17 mlelstv
313 1.17 mlelstv if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
314 1.17 mlelstv cuthiyear = hi;
315 1.17 mlelstv } else if (sscanf(cutarg, "%ld,%ld%c",
316 1.17 mlelstv &lo, &hi, &dummy) == 2) {
317 1.17 mlelstv cutloyear = lo;
318 1.17 mlelstv cuthiyear = hi;
319 1.17 mlelstv } else {
320 1.17 mlelstv (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
321 1.17 mlelstv progname, cutarg);
322 1.17 mlelstv exit(EXIT_FAILURE);
323 1.17 mlelstv }
324 1.17 mlelstv }
325 1.17 mlelstv setabsolutes();
326 1.17 mlelstv cutlotime = yeartot(cutloyear);
327 1.17 mlelstv cuthitime = yeartot(cuthiyear);
328 1.1 jtc }
329 1.1 jtc (void) time(&now);
330 1.1 jtc longest = 0;
331 1.1 jtc for (i = optind; i < argc; ++i)
332 1.1 jtc if (strlen(argv[i]) > longest)
333 1.1 jtc longest = strlen(argv[i]);
334 1.1 jtc {
335 1.26 christos int from;
336 1.26 christos int to;
337 1.1 jtc
338 1.17 mlelstv for (i = 0; environ[i] != NULL; ++i)
339 1.1 jtc continue;
340 1.26 christos fakeenv = malloc((i + 2) * sizeof *fakeenv);
341 1.1 jtc if (fakeenv == NULL ||
342 1.26 christos (fakeenv[0] = malloc(longest + 4)) == NULL) {
343 1.15 christos err(EXIT_FAILURE, "Can't allocated %zu bytes",
344 1.15 christos longest + 4);
345 1.1 jtc }
346 1.1 jtc to = 0;
347 1.4 mrg (void)strcpy(fakeenv[to++], "TZ="); /* XXX strcpy is safe */
348 1.1 jtc for (from = 0; environ[from] != NULL; ++from)
349 1.1 jtc if (strncmp(environ[from], "TZ=", 3) != 0)
350 1.1 jtc fakeenv[to++] = environ[from];
351 1.1 jtc fakeenv[to] = NULL;
352 1.1 jtc environ = fakeenv;
353 1.1 jtc }
354 1.1 jtc for (i = optind; i < argc; ++i) {
355 1.1 jtc static char buf[MAX_STRING_LENGTH];
356 1.1 jtc
357 1.4 mrg (void) strcpy(&fakeenv[0][3], argv[i]); /* XXX strcpy is safe */
358 1.3 jtc if (!vflag) {
359 1.3 jtc show(argv[i], now, FALSE);
360 1.1 jtc continue;
361 1.3 jtc }
362 1.17 mlelstv warned = FALSE;
363 1.17 mlelstv t = absolute_min_time;
364 1.1 jtc show(argv[i], t, TRUE);
365 1.1 jtc t += SECSPERHOUR * HOURSPERDAY;
366 1.1 jtc show(argv[i], t, TRUE);
367 1.17 mlelstv if (t < cutlotime)
368 1.17 mlelstv t = cutlotime;
369 1.17 mlelstv tmp = my_localtime(&t);
370 1.17 mlelstv if (tmp != NULL) {
371 1.17 mlelstv tm = *tmp;
372 1.17 mlelstv (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
373 1.17 mlelstv }
374 1.1 jtc for ( ; ; ) {
375 1.17 mlelstv if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
376 1.1 jtc break;
377 1.1 jtc newt = t + SECSPERHOUR * 12;
378 1.17 mlelstv newtmp = localtime(&newt);
379 1.17 mlelstv if (newtmp != NULL)
380 1.17 mlelstv newtm = *newtmp;
381 1.17 mlelstv if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
382 1.17 mlelstv (delta(&newtm, &tm) != (newt - t) ||
383 1.1 jtc newtm.tm_isdst != tm.tm_isdst ||
384 1.17 mlelstv strcmp(abbr(&newtm), buf) != 0)) {
385 1.1 jtc newt = hunt(argv[i], t, newt);
386 1.17 mlelstv newtmp = localtime(&newt);
387 1.17 mlelstv if (newtmp != NULL) {
388 1.17 mlelstv newtm = *newtmp;
389 1.17 mlelstv (void) strncpy(buf,
390 1.17 mlelstv abbr(&newtm),
391 1.17 mlelstv (sizeof buf) - 1);
392 1.17 mlelstv }
393 1.1 jtc }
394 1.1 jtc t = newt;
395 1.1 jtc tm = newtm;
396 1.17 mlelstv tmp = newtmp;
397 1.1 jtc }
398 1.17 mlelstv t = absolute_max_time;
399 1.1 jtc t -= SECSPERHOUR * HOURSPERDAY;
400 1.1 jtc show(argv[i], t, TRUE);
401 1.1 jtc t += SECSPERHOUR * HOURSPERDAY;
402 1.1 jtc show(argv[i], t, TRUE);
403 1.1 jtc }
404 1.1 jtc if (fflush(stdout) || ferror(stdout)) {
405 1.16 kleink err(EXIT_FAILURE, _("Error writing standard output"));
406 1.1 jtc }
407 1.1 jtc exit(EXIT_SUCCESS);
408 1.17 mlelstv /* If exit fails to exit... */
409 1.17 mlelstv return EXIT_FAILURE;
410 1.17 mlelstv }
411 1.1 jtc
412 1.20 christos static time_t
413 1.20 christos ovfl_check(time_t t)
414 1.20 christos {
415 1.20 christos if (t < 0 && t - 1 >= 0)
416 1.20 christos return t;
417 1.20 christos else
418 1.20 christos return t - 1;
419 1.20 christos }
420 1.20 christos
421 1.17 mlelstv static void
422 1.17 mlelstv setabsolutes(void)
423 1.17 mlelstv {
424 1.17 mlelstv if (0.5 == (time_t) 0.5) {
425 1.17 mlelstv /*
426 1.17 mlelstv ** time_t is floating.
427 1.17 mlelstv */
428 1.17 mlelstv if (sizeof (time_t) == sizeof (float)) {
429 1.17 mlelstv absolute_min_time = (time_t) -FLT_MAX;
430 1.17 mlelstv absolute_max_time = (time_t) FLT_MAX;
431 1.17 mlelstv } else if (sizeof (time_t) == sizeof (double)) {
432 1.17 mlelstv absolute_min_time = (time_t) -DBL_MAX;
433 1.17 mlelstv absolute_max_time = (time_t) DBL_MAX;
434 1.17 mlelstv } else {
435 1.17 mlelstv (void) fprintf(stderr,
436 1.17 mlelstv _("%s: use of -v on system with floating time_t other than float or double\n"),
437 1.17 mlelstv progname);
438 1.17 mlelstv exit(EXIT_FAILURE);
439 1.17 mlelstv }
440 1.17 mlelstv } else if (0 > (time_t) -1) {
441 1.17 mlelstv /*
442 1.19 martin ** time_t is signed. Assume overflow wraps around.
443 1.17 mlelstv */
444 1.19 martin time_t t = 0;
445 1.19 martin time_t t1 = 1;
446 1.19 martin
447 1.19 martin while (t < t1) {
448 1.19 martin t = t1;
449 1.19 martin t1 = 2 * t1 + 1;
450 1.17 mlelstv }
451 1.19 martin
452 1.19 martin absolute_max_time = t;
453 1.19 martin t = -t;
454 1.20 christos absolute_min_time = ovfl_check(t);
455 1.17 mlelstv } else {
456 1.17 mlelstv /*
457 1.17 mlelstv ** time_t is unsigned.
458 1.17 mlelstv */
459 1.17 mlelstv absolute_min_time = 0;
460 1.17 mlelstv absolute_max_time = absolute_min_time - 1;
461 1.17 mlelstv }
462 1.1 jtc }
463 1.1 jtc
464 1.1 jtc static time_t
465 1.26 christos yeartot(const long y)
466 1.17 mlelstv {
467 1.26 christos long myy;
468 1.26 christos long seconds;
469 1.26 christos time_t t;
470 1.17 mlelstv
471 1.17 mlelstv myy = EPOCH_YEAR;
472 1.17 mlelstv t = 0;
473 1.17 mlelstv while (myy != y) {
474 1.17 mlelstv if (myy < y) {
475 1.17 mlelstv seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
476 1.17 mlelstv ++myy;
477 1.17 mlelstv if (t > absolute_max_time - seconds) {
478 1.17 mlelstv t = absolute_max_time;
479 1.17 mlelstv break;
480 1.17 mlelstv }
481 1.17 mlelstv t += seconds;
482 1.17 mlelstv } else {
483 1.17 mlelstv --myy;
484 1.17 mlelstv seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
485 1.17 mlelstv if (t < absolute_min_time + seconds) {
486 1.17 mlelstv t = absolute_min_time;
487 1.17 mlelstv break;
488 1.17 mlelstv }
489 1.17 mlelstv t -= seconds;
490 1.17 mlelstv }
491 1.17 mlelstv }
492 1.17 mlelstv return t;
493 1.17 mlelstv }
494 1.17 mlelstv
495 1.17 mlelstv static time_t
496 1.17 mlelstv hunt(char *name, time_t lot, time_t hit)
497 1.17 mlelstv {
498 1.17 mlelstv time_t t;
499 1.17 mlelstv long diff;
500 1.17 mlelstv struct tm lotm;
501 1.26 christos struct tm * lotmp;
502 1.17 mlelstv struct tm tm;
503 1.26 christos struct tm * tmp;
504 1.17 mlelstv char loab[MAX_STRING_LENGTH];
505 1.17 mlelstv
506 1.17 mlelstv lotmp = my_localtime(&lot);
507 1.17 mlelstv if (lotmp != NULL) {
508 1.17 mlelstv lotm = *lotmp;
509 1.17 mlelstv (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
510 1.17 mlelstv }
511 1.17 mlelstv for ( ; ; ) {
512 1.17 mlelstv diff = (long) (hit - lot);
513 1.17 mlelstv if (diff < 2)
514 1.17 mlelstv break;
515 1.17 mlelstv t = lot;
516 1.17 mlelstv t += diff / 2;
517 1.1 jtc if (t <= lot)
518 1.1 jtc ++t;
519 1.1 jtc else if (t >= hit)
520 1.1 jtc --t;
521 1.17 mlelstv tmp = my_localtime(&t);
522 1.17 mlelstv if (tmp != NULL)
523 1.17 mlelstv tm = *tmp;
524 1.17 mlelstv if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
525 1.17 mlelstv (delta(&tm, &lotm) == (t - lot) &&
526 1.1 jtc tm.tm_isdst == lotm.tm_isdst &&
527 1.17 mlelstv strcmp(abbr(&tm), loab) == 0)) {
528 1.1 jtc lot = t;
529 1.1 jtc lotm = tm;
530 1.17 mlelstv lotmp = tmp;
531 1.1 jtc } else hit = t;
532 1.1 jtc }
533 1.1 jtc show(name, lot, TRUE);
534 1.1 jtc show(name, hit, TRUE);
535 1.1 jtc return hit;
536 1.1 jtc }
537 1.1 jtc
538 1.1 jtc /*
539 1.17 mlelstv ** Thanks to Paul Eggert for logic used in delta.
540 1.1 jtc */
541 1.1 jtc
542 1.1 jtc static long
543 1.26 christos delta(struct tm *newp, struct tm *oldp)
544 1.1 jtc {
545 1.26 christos long result;
546 1.26 christos int tmy;
547 1.1 jtc
548 1.1 jtc if (newp->tm_year < oldp->tm_year)
549 1.1 jtc return -delta(oldp, newp);
550 1.1 jtc result = 0;
551 1.1 jtc for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
552 1.17 mlelstv result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
553 1.1 jtc result += newp->tm_yday - oldp->tm_yday;
554 1.1 jtc result *= HOURSPERDAY;
555 1.1 jtc result += newp->tm_hour - oldp->tm_hour;
556 1.1 jtc result *= MINSPERHOUR;
557 1.1 jtc result += newp->tm_min - oldp->tm_min;
558 1.1 jtc result *= SECSPERMIN;
559 1.1 jtc result += newp->tm_sec - oldp->tm_sec;
560 1.1 jtc return result;
561 1.1 jtc }
562 1.1 jtc
563 1.1 jtc static void
564 1.17 mlelstv show(char *zone, time_t t, int v)
565 1.1 jtc {
566 1.26 christos struct tm * tmp;
567 1.1 jtc
568 1.5 jtc (void) printf("%-*s ", (int) longest, zone);
569 1.1 jtc if (v) {
570 1.17 mlelstv tmp = gmtime(&t);
571 1.17 mlelstv if (tmp == NULL) {
572 1.17 mlelstv (void) printf(tformat(), t);
573 1.17 mlelstv } else {
574 1.17 mlelstv dumptime(tmp);
575 1.17 mlelstv (void) printf(" UTC");
576 1.17 mlelstv }
577 1.17 mlelstv (void) printf(" = ");
578 1.17 mlelstv }
579 1.17 mlelstv tmp = my_localtime(&t);
580 1.17 mlelstv dumptime(tmp);
581 1.17 mlelstv if (tmp != NULL) {
582 1.17 mlelstv if (*abbr(tmp) != '\0')
583 1.17 mlelstv (void) printf(" %s", abbr(tmp));
584 1.17 mlelstv if (v) {
585 1.17 mlelstv (void) printf(" isdst=%d", tmp->tm_isdst);
586 1.1 jtc #ifdef TM_GMTOFF
587 1.17 mlelstv (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
588 1.1 jtc #endif /* defined TM_GMTOFF */
589 1.17 mlelstv }
590 1.1 jtc }
591 1.1 jtc (void) printf("\n");
592 1.17 mlelstv if (tmp != NULL && *abbr(tmp) != '\0')
593 1.17 mlelstv abbrok(abbr(tmp), zone);
594 1.1 jtc }
595 1.1 jtc
596 1.9 mycroft static const char *
597 1.26 christos abbr(struct tm *tmp)
598 1.1 jtc {
599 1.26 christos const char * result;
600 1.9 mycroft static const char nada;
601 1.1 jtc
602 1.1 jtc if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
603 1.1 jtc return &nada;
604 1.1 jtc result = tzname[tmp->tm_isdst];
605 1.1 jtc return (result == NULL) ? &nada : result;
606 1.1 jtc }
607 1.17 mlelstv
608 1.17 mlelstv /*
609 1.17 mlelstv ** The code below can fail on certain theoretical systems;
610 1.17 mlelstv ** it works on all known real-world systems as of 2004-12-30.
611 1.17 mlelstv */
612 1.17 mlelstv
613 1.17 mlelstv static const char *
614 1.17 mlelstv tformat(void)
615 1.17 mlelstv {
616 1.17 mlelstv if (0.5 == (time_t) 0.5) { /* floating */
617 1.17 mlelstv if (sizeof (time_t) > sizeof (double))
618 1.17 mlelstv return "%Lg";
619 1.17 mlelstv return "%g";
620 1.17 mlelstv }
621 1.17 mlelstv if (0 > (time_t) -1) { /* signed */
622 1.17 mlelstv if (sizeof (time_t) > sizeof (long))
623 1.17 mlelstv return "%lld";
624 1.17 mlelstv if (sizeof (time_t) > sizeof (int))
625 1.17 mlelstv return "%ld";
626 1.17 mlelstv return "%d";
627 1.17 mlelstv }
628 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned long))
629 1.17 mlelstv return "%llu";
630 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned int))
631 1.17 mlelstv return "%lu";
632 1.17 mlelstv return "%u";
633 1.17 mlelstv }
634 1.17 mlelstv
635 1.17 mlelstv static void
636 1.26 christos dumptime(const struct tm *timeptr)
637 1.17 mlelstv {
638 1.17 mlelstv static const char wday_name[][3] = {
639 1.17 mlelstv "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
640 1.17 mlelstv };
641 1.17 mlelstv static const char mon_name[][3] = {
642 1.17 mlelstv "Jan", "Feb", "Mar", "Apr", "May", "Jun",
643 1.17 mlelstv "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
644 1.17 mlelstv };
645 1.26 christos const char * wn;
646 1.26 christos const char * mn;
647 1.26 christos int lead;
648 1.26 christos int trail;
649 1.17 mlelstv
650 1.17 mlelstv if (timeptr == NULL) {
651 1.17 mlelstv (void) printf("NULL");
652 1.17 mlelstv return;
653 1.17 mlelstv }
654 1.17 mlelstv /*
655 1.17 mlelstv ** The packaged versions of localtime and gmtime never put out-of-range
656 1.17 mlelstv ** values in tm_wday or tm_mon, but since this code might be compiled
657 1.17 mlelstv ** with other (perhaps experimental) versions, paranoia is in order.
658 1.17 mlelstv */
659 1.17 mlelstv if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
660 1.17 mlelstv (int) (sizeof wday_name / sizeof wday_name[0]))
661 1.17 mlelstv wn = "???";
662 1.17 mlelstv else wn = wday_name[timeptr->tm_wday];
663 1.17 mlelstv if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
664 1.17 mlelstv (int) (sizeof mon_name / sizeof mon_name[0]))
665 1.17 mlelstv mn = "???";
666 1.17 mlelstv else mn = mon_name[timeptr->tm_mon];
667 1.17 mlelstv (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
668 1.17 mlelstv wn, mn,
669 1.17 mlelstv timeptr->tm_mday, timeptr->tm_hour,
670 1.17 mlelstv timeptr->tm_min, timeptr->tm_sec);
671 1.17 mlelstv #define DIVISOR 10
672 1.17 mlelstv trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
673 1.17 mlelstv lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
674 1.17 mlelstv trail / DIVISOR;
675 1.17 mlelstv trail %= DIVISOR;
676 1.17 mlelstv if (trail < 0 && lead > 0) {
677 1.17 mlelstv trail += DIVISOR;
678 1.17 mlelstv --lead;
679 1.17 mlelstv } else if (lead < 0 && trail > 0) {
680 1.17 mlelstv trail -= DIVISOR;
681 1.17 mlelstv ++lead;
682 1.17 mlelstv }
683 1.17 mlelstv if (lead == 0)
684 1.17 mlelstv (void) printf("%d", trail);
685 1.17 mlelstv else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
686 1.17 mlelstv }
687