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