zdump.c revision 1.42 1 1.42 christos /* $NetBSD: zdump.c,v 1.42 2015/08/13 11:21:18 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.42 christos __RCSID("$NetBSD: zdump.c,v 1.42 2015/08/13 11:21:18 christos Exp $");
10 1.1 jtc #endif /* !defined lint */
11 1.1 jtc
12 1.1 jtc /*
13 1.1 jtc ** This code has been made independent of the rest of the time
14 1.1 jtc ** conversion package to increase confidence in the verification it provides.
15 1.1 jtc ** You can use this code to help in verifying other implementations.
16 1.29 christos **
17 1.36 christos ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
18 1.1 jtc */
19 1.1 jtc
20 1.36 christos #ifndef NETBSD_INSPIRED
21 1.36 christos # define NETBSD_INSPIRED 1
22 1.36 christos #endif
23 1.36 christos #ifndef USE_LTZ
24 1.36 christos # define USE_LTZ 1
25 1.36 christos #endif
26 1.36 christos
27 1.36 christos #if USE_LTZ
28 1.29 christos #include "private.h"
29 1.36 christos #endif
30 1.36 christos
31 1.36 christos /* Enable tm_gmtoff and tm_zone on GNUish systems. */
32 1.36 christos #define _GNU_SOURCE 1
33 1.36 christos /* Enable strtoimax on Solaris 10. */
34 1.36 christos #define __EXTENSIONS__ 1
35 1.29 christos
36 1.15 christos #include "stdio.h" /* for stdout, stderr */
37 1.1 jtc #include "string.h" /* for strcpy */
38 1.1 jtc #include "sys/types.h" /* for time_t */
39 1.1 jtc #include "time.h" /* for struct tm */
40 1.1 jtc #include "stdlib.h" /* for exit, malloc, atoi */
41 1.36 christos #include "limits.h" /* for CHAR_BIT, LLONG_MAX */
42 1.36 christos #include <errno.h>
43 1.15 christos #include <err.h>
44 1.17 mlelstv
45 1.29 christos /*
46 1.29 christos ** Substitutes for pre-C99 compilers.
47 1.29 christos ** Much of this section of code is stolen from private.h.
48 1.29 christos */
49 1.29 christos
50 1.29 christos #ifndef HAVE_STDINT_H
51 1.29 christos # define HAVE_STDINT_H \
52 1.33 christos (199901 <= __STDC_VERSION__ \
53 1.33 christos || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
54 1.33 christos || __CYGWIN__)
55 1.29 christos #endif
56 1.29 christos #if HAVE_STDINT_H
57 1.29 christos # include "stdint.h"
58 1.29 christos #endif
59 1.29 christos #ifndef HAVE_INTTYPES_H
60 1.29 christos # define HAVE_INTTYPES_H HAVE_STDINT_H
61 1.29 christos #endif
62 1.29 christos #if HAVE_INTTYPES_H
63 1.29 christos # include <inttypes.h>
64 1.29 christos #endif
65 1.29 christos
66 1.29 christos #ifndef INT_FAST32_MAX
67 1.29 christos # if INT_MAX >> 31 == 0
68 1.29 christos typedef long int_fast32_t;
69 1.29 christos # else
70 1.29 christos typedef int int_fast32_t;
71 1.29 christos # endif
72 1.29 christos #endif
73 1.29 christos
74 1.36 christos /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
75 1.36 christos #if !defined LLONG_MAX && defined __LONG_LONG_MAX__
76 1.36 christos # define LLONG_MAX __LONG_LONG_MAX__
77 1.36 christos #endif
78 1.36 christos
79 1.29 christos #ifndef INTMAX_MAX
80 1.36 christos # ifdef LLONG_MAX
81 1.29 christos typedef long long intmax_t;
82 1.32 christos # define strtoimax strtoll
83 1.36 christos # define INTMAX_MAX LLONG_MAX
84 1.29 christos # else
85 1.29 christos typedef long intmax_t;
86 1.32 christos # define strtoimax strtol
87 1.36 christos # define INTMAX_MAX LONG_MAX
88 1.36 christos # endif
89 1.36 christos #endif
90 1.36 christos
91 1.36 christos #ifndef PRIdMAX
92 1.36 christos # if INTMAX_MAX == LLONG_MAX
93 1.36 christos # define PRIdMAX "lld"
94 1.36 christos # else
95 1.29 christos # define PRIdMAX "ld"
96 1.29 christos # endif
97 1.29 christos #endif
98 1.29 christos
99 1.36 christos /* Infer TM_ZONE on systems where this information is known, but suppress
100 1.36 christos guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
101 1.36 christos #if (defined __GLIBC__ \
102 1.36 christos || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
103 1.36 christos || (defined __APPLE__ && defined __MACH__))
104 1.36 christos # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
105 1.36 christos # define TM_GMTOFF tm_gmtoff
106 1.36 christos # endif
107 1.36 christos # if !defined TM_ZONE && !defined NO_TM_ZONE
108 1.36 christos # define TM_ZONE tm_zone
109 1.36 christos # endif
110 1.36 christos #endif
111 1.36 christos
112 1.36 christos #ifndef HAVE_LOCALTIME_R
113 1.36 christos # define HAVE_LOCALTIME_R 1
114 1.36 christos #endif
115 1.36 christos
116 1.36 christos #ifndef HAVE_LOCALTIME_RZ
117 1.36 christos # ifdef TM_ZONE
118 1.36 christos # define HAVE_LOCALTIME_RZ (NETBSD_INSPIRED && USE_LTZ)
119 1.36 christos # else
120 1.36 christos # define HAVE_LOCALTIME_RZ 0
121 1.36 christos # endif
122 1.36 christos #endif
123 1.36 christos
124 1.36 christos #ifndef HAVE_TZSET
125 1.36 christos # define HAVE_TZSET 1
126 1.36 christos #endif
127 1.27 christos
128 1.17 mlelstv #ifndef ZDUMP_LO_YEAR
129 1.17 mlelstv #define ZDUMP_LO_YEAR (-500)
130 1.17 mlelstv #endif /* !defined ZDUMP_LO_YEAR */
131 1.17 mlelstv
132 1.17 mlelstv #ifndef ZDUMP_HI_YEAR
133 1.17 mlelstv #define ZDUMP_HI_YEAR 2500
134 1.17 mlelstv #endif /* !defined ZDUMP_HI_YEAR */
135 1.1 jtc
136 1.1 jtc #ifndef MAX_STRING_LENGTH
137 1.1 jtc #define MAX_STRING_LENGTH 1024
138 1.1 jtc #endif /* !defined MAX_STRING_LENGTH */
139 1.1 jtc
140 1.36 christos #if __STDC_VERSION__ < 199901
141 1.36 christos # define true 1
142 1.36 christos # define false 0
143 1.36 christos # define bool int
144 1.36 christos #else
145 1.36 christos # include <stdbool.h>
146 1.36 christos #endif
147 1.1 jtc
148 1.1 jtc #ifndef EXIT_SUCCESS
149 1.1 jtc #define EXIT_SUCCESS 0
150 1.1 jtc #endif /* !defined EXIT_SUCCESS */
151 1.1 jtc
152 1.1 jtc #ifndef EXIT_FAILURE
153 1.1 jtc #define EXIT_FAILURE 1
154 1.1 jtc #endif /* !defined EXIT_FAILURE */
155 1.1 jtc
156 1.1 jtc #ifndef SECSPERMIN
157 1.1 jtc #define SECSPERMIN 60
158 1.1 jtc #endif /* !defined SECSPERMIN */
159 1.1 jtc
160 1.1 jtc #ifndef MINSPERHOUR
161 1.1 jtc #define MINSPERHOUR 60
162 1.1 jtc #endif /* !defined MINSPERHOUR */
163 1.1 jtc
164 1.1 jtc #ifndef SECSPERHOUR
165 1.1 jtc #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
166 1.1 jtc #endif /* !defined SECSPERHOUR */
167 1.1 jtc
168 1.1 jtc #ifndef HOURSPERDAY
169 1.1 jtc #define HOURSPERDAY 24
170 1.1 jtc #endif /* !defined HOURSPERDAY */
171 1.1 jtc
172 1.1 jtc #ifndef EPOCH_YEAR
173 1.1 jtc #define EPOCH_YEAR 1970
174 1.1 jtc #endif /* !defined EPOCH_YEAR */
175 1.1 jtc
176 1.1 jtc #ifndef TM_YEAR_BASE
177 1.1 jtc #define TM_YEAR_BASE 1900
178 1.1 jtc #endif /* !defined TM_YEAR_BASE */
179 1.1 jtc
180 1.1 jtc #ifndef DAYSPERNYEAR
181 1.1 jtc #define DAYSPERNYEAR 365
182 1.1 jtc #endif /* !defined DAYSPERNYEAR */
183 1.1 jtc
184 1.1 jtc #ifndef isleap
185 1.17 mlelstv #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
186 1.1 jtc #endif /* !defined isleap */
187 1.1 jtc
188 1.17 mlelstv #ifndef isleap_sum
189 1.17 mlelstv /*
190 1.17 mlelstv ** See tzfile.h for details on isleap_sum.
191 1.17 mlelstv */
192 1.17 mlelstv #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
193 1.17 mlelstv #endif /* !defined isleap_sum */
194 1.17 mlelstv
195 1.29 christos #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
196 1.17 mlelstv #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
197 1.17 mlelstv #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
198 1.31 christos #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
199 1.31 christos + SECSPERLYEAR * (intmax_t) (100 - 3))
200 1.31 christos
201 1.31 christos /*
202 1.31 christos ** True if SECSPER400YEARS is known to be representable as an
203 1.31 christos ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
204 1.31 christos ** even if SECSPER400YEARS is representable, because when that happens
205 1.31 christos ** the code merely runs a bit more slowly, and this slowness doesn't
206 1.31 christos ** occur on any practical platform.
207 1.31 christos */
208 1.31 christos enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
209 1.17 mlelstv
210 1.17 mlelstv #ifndef HAVE_GETTEXT
211 1.17 mlelstv #define HAVE_GETTEXT 0
212 1.17 mlelstv #endif
213 1.17 mlelstv #if HAVE_GETTEXT
214 1.3 jtc #include "locale.h" /* for setlocale */
215 1.3 jtc #include "libintl.h"
216 1.17 mlelstv #endif /* HAVE_GETTEXT */
217 1.3 jtc
218 1.29 christos #ifndef ATTRIBUTE_PURE
219 1.26 christos #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
220 1.29 christos # define ATTRIBUTE_PURE __attribute__ ((ATTRIBUTE_PURE__))
221 1.26 christos #else
222 1.29 christos # define ATTRIBUTE_PURE /* empty */
223 1.26 christos #endif
224 1.26 christos #endif
225 1.26 christos
226 1.1 jtc #ifndef INITIALIZE
227 1.35 christos #if defined(__GNUC__) || defined(__lint__)
228 1.1 jtc #define INITIALIZE(x) ((x) = 0)
229 1.35 christos #else /* !defined GNUC || lint */
230 1.1 jtc #define INITIALIZE(x)
231 1.35 christos #endif /* !defined GNUC || lint */
232 1.1 jtc #endif /* !defined INITIALIZE */
233 1.1 jtc
234 1.3 jtc /*
235 1.3 jtc ** For the benefit of GNU folk...
236 1.34 christos ** '_(MSGID)' uses the current locale's message library string for MSGID.
237 1.3 jtc ** The default is to use gettext if available, and use MSGID otherwise.
238 1.3 jtc */
239 1.3 jtc
240 1.3 jtc #ifndef _
241 1.17 mlelstv #if HAVE_GETTEXT
242 1.3 jtc #define _(msgid) gettext(msgid)
243 1.17 mlelstv #else /* !HAVE_GETTEXT */
244 1.21 christos #define _(msgid) msgid
245 1.17 mlelstv #endif /* !HAVE_GETTEXT */
246 1.3 jtc #endif /* !defined _ */
247 1.3 jtc
248 1.34 christos #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
249 1.34 christos # define TZ_DOMAIN "tz"
250 1.34 christos #endif
251 1.3 jtc
252 1.36 christos #if ! HAVE_LOCALTIME_RZ
253 1.36 christos # undef timezone_t
254 1.36 christos # define timezone_t char **
255 1.36 christos #endif
256 1.36 christos
257 1.1 jtc extern char ** environ;
258 1.17 mlelstv extern int getopt(int argc, char * const argv[],
259 1.17 mlelstv const char * options);
260 1.1 jtc extern char * optarg;
261 1.1 jtc extern int optind;
262 1.1 jtc
263 1.29 christos /* The minimum and maximum finite time values. */
264 1.41 christos enum { atime_shift = CHAR_BIT * sizeof (time_t) - 2 };
265 1.29 christos static time_t absolute_min_time =
266 1.31 christos ((time_t) -1 < 0
267 1.41 christos ? (- ((time_t) ~ (time_t) 0 < 0)
268 1.41 christos - (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift)))
269 1.29 christos : 0);
270 1.29 christos static time_t absolute_max_time =
271 1.31 christos ((time_t) -1 < 0
272 1.41 christos ? (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift))
273 1.29 christos : -1);
274 1.5 jtc static size_t longest;
275 1.1 jtc static char * progname;
276 1.36 christos static bool warned;
277 1.36 christos static bool errout;
278 1.17 mlelstv
279 1.36 christos static char const *abbr(struct tm const *);
280 1.36 christos static intmax_t delta(struct tm *, struct tm *) ATTRIBUTE_PURE;
281 1.36 christos static void dumptime(struct tm const *);
282 1.36 christos static time_t hunt(timezone_t, char *, time_t, time_t);
283 1.36 christos static void show(timezone_t, char *, time_t, bool);
284 1.36 christos static const char *tformat(void);
285 1.36 christos static time_t yeartot(intmax_t) ATTRIBUTE_PURE;
286 1.17 mlelstv
287 1.42 christos /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
288 1.42 christos #define is_digit(c) ((unsigned)(c) - '0' <= 9)
289 1.42 christos
290 1.34 christos /* Is A an alphabetic character in the C locale? */
291 1.36 christos static bool
292 1.34 christos is_alpha(char a)
293 1.34 christos {
294 1.34 christos switch (a) {
295 1.34 christos default:
296 1.36 christos return false;
297 1.34 christos case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
298 1.34 christos case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
299 1.34 christos case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
300 1.34 christos case 'V': case 'W': case 'X': case 'Y': case 'Z':
301 1.34 christos case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
302 1.34 christos case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
303 1.34 christos case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
304 1.34 christos case 'v': case 'w': case 'x': case 'y': case 'z':
305 1.36 christos return true;
306 1.34 christos }
307 1.34 christos }
308 1.34 christos
309 1.36 christos /* Return A + B, exiting if the result would overflow. */
310 1.36 christos static size_t
311 1.36 christos sumsize(size_t a, size_t b)
312 1.36 christos {
313 1.36 christos size_t sum = a + b;
314 1.36 christos if (sum < a)
315 1.36 christos errx(EXIT_FAILURE, "size overflow");
316 1.36 christos return sum;
317 1.36 christos }
318 1.36 christos
319 1.36 christos #if ! HAVE_TZSET
320 1.36 christos # undef tzset
321 1.36 christos # define tzset zdump_tzset
322 1.36 christos static void tzset(void) { }
323 1.36 christos #endif
324 1.36 christos
325 1.36 christos /* Assume gmtime_r works if localtime_r does.
326 1.36 christos A replacement localtime_r is defined below if needed. */
327 1.36 christos #if ! HAVE_LOCALTIME_R
328 1.36 christos
329 1.36 christos # undef gmtime_r
330 1.36 christos # define gmtime_r zdump_gmtime_r
331 1.36 christos
332 1.36 christos static struct tm *
333 1.36 christos gmtime_r(time_t *tp, struct tm *tmp)
334 1.36 christos {
335 1.36 christos struct tm *r = gmtime(tp);
336 1.36 christos if (r) {
337 1.36 christos *tmp = *r;
338 1.36 christos r = tmp;
339 1.36 christos }
340 1.36 christos return r;
341 1.36 christos }
342 1.36 christos
343 1.36 christos #endif
344 1.36 christos
345 1.36 christos /* Platforms with TM_ZONE don't need tzname, so they can use the
346 1.36 christos faster localtime_rz or localtime_r if available. */
347 1.36 christos
348 1.36 christos #if defined TM_ZONE && HAVE_LOCALTIME_RZ
349 1.36 christos # define USE_LOCALTIME_RZ true
350 1.36 christos #else
351 1.36 christos # define USE_LOCALTIME_RZ false
352 1.36 christos #endif
353 1.36 christos
354 1.36 christos #if ! USE_LOCALTIME_RZ
355 1.36 christos
356 1.36 christos # if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET
357 1.36 christos # undef localtime_r
358 1.36 christos # define localtime_r zdump_localtime_r
359 1.36 christos static struct tm *
360 1.36 christos localtime_r(time_t *tp, struct tm *tmp)
361 1.36 christos {
362 1.36 christos struct tm *r = localtime(tp);
363 1.36 christos if (r) {
364 1.36 christos *tmp = *r;
365 1.36 christos r = tmp;
366 1.36 christos }
367 1.36 christos return r;
368 1.36 christos }
369 1.36 christos # endif
370 1.36 christos
371 1.36 christos # undef localtime_rz
372 1.36 christos # define localtime_rz zdump_localtime_rz
373 1.36 christos static struct tm *
374 1.36 christos localtime_rz(timezone_t rz, time_t *tp, struct tm *tmp)
375 1.36 christos {
376 1.36 christos return localtime_r(tp, tmp);
377 1.36 christos }
378 1.36 christos
379 1.36 christos # ifdef TYPECHECK
380 1.36 christos # undef mktime_z
381 1.36 christos # define mktime_z zdump_mktime_z
382 1.36 christos static time_t
383 1.36 christos mktime_z(timezone_t tz, struct tm *tmp)
384 1.36 christos {
385 1.36 christos return mktime(tmp);
386 1.36 christos }
387 1.36 christos # endif
388 1.36 christos
389 1.36 christos # undef tzalloc
390 1.36 christos # undef tzfree
391 1.36 christos # define tzalloc zdump_tzalloc
392 1.36 christos # define tzfree zdump_tzfree
393 1.36 christos
394 1.36 christos static timezone_t
395 1.36 christos tzalloc(char const *val)
396 1.36 christos {
397 1.36 christos static char **fakeenv;
398 1.36 christos char **env = fakeenv;
399 1.36 christos char *env0;
400 1.36 christos if (! env) {
401 1.36 christos char **e = environ;
402 1.36 christos int to;
403 1.36 christos
404 1.36 christos while (*e++)
405 1.36 christos continue;
406 1.36 christos env = malloc(sumsize(sizeof *environ,
407 1.36 christos (e - environ) * sizeof *environ));
408 1.36 christos if (! env) {
409 1.36 christos err(EXIT_FAILURE, "malloc");
410 1.36 christos }
411 1.36 christos to = 1;
412 1.36 christos for (e = environ; (env[to] = *e); e++)
413 1.36 christos to += strncmp(*e, "TZ=", 3) != 0;
414 1.36 christos }
415 1.36 christos env0 = malloc(sumsize(sizeof "TZ=", strlen(val)));
416 1.36 christos if (! env0) {
417 1.36 christos err(EXIT_FAILURE, "malloc");
418 1.36 christos }
419 1.36 christos env[0] = strcat(strcpy(env0, "TZ="), val);
420 1.36 christos environ = fakeenv = env;
421 1.36 christos tzset();
422 1.36 christos return env;
423 1.36 christos }
424 1.36 christos
425 1.36 christos static void
426 1.36 christos tzfree(timezone_t env)
427 1.36 christos {
428 1.36 christos environ = env + 1;
429 1.36 christos free(env[0]);
430 1.36 christos }
431 1.36 christos #endif /* ! USE_LOCALTIME_RZ */
432 1.36 christos
433 1.36 christos /* A UTC time zone, and its initializer. */
434 1.36 christos static timezone_t gmtz;
435 1.36 christos static void
436 1.36 christos gmtzinit(void)
437 1.36 christos {
438 1.36 christos if (USE_LOCALTIME_RZ) {
439 1.36 christos static char const utc[] = "UTC0";
440 1.36 christos gmtz = tzalloc(utc);
441 1.36 christos if (!gmtz) {
442 1.36 christos err(EXIT_FAILURE, "Cannot create %s", utc);
443 1.36 christos }
444 1.36 christos }
445 1.36 christos }
446 1.36 christos
447 1.36 christos /* Convert *TP to UTC, storing the broken-down time into *TMP.
448 1.36 christos Return TMP if successful, NULL otherwise. This is like gmtime_r(TP, TMP),
449 1.36 christos except typically faster if USE_LOCALTIME_RZ. */
450 1.36 christos static struct tm *
451 1.36 christos my_gmtime_r(time_t *tp, struct tm *tmp)
452 1.36 christos {
453 1.36 christos return USE_LOCALTIME_RZ ?
454 1.36 christos localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp);
455 1.36 christos }
456 1.36 christos
457 1.17 mlelstv #ifndef TYPECHECK
458 1.36 christos #define my_localtime_rz localtime_rz
459 1.17 mlelstv #else /* !defined TYPECHECK */
460 1.17 mlelstv static struct tm *
461 1.36 christos my_localtime_rz(timezone_t tz, const time_t *tp, struct tm *tmp)
462 1.17 mlelstv {
463 1.36 christos tmp = localtime_rz(tz, tp, tmp);
464 1.36 christos if (tmp) {
465 1.17 mlelstv struct tm tm;
466 1.26 christos time_t t;
467 1.17 mlelstv
468 1.17 mlelstv tm = *tmp;
469 1.36 christos t = mktime_z(tz, &tm);
470 1.31 christos if (t != *tp) {
471 1.17 mlelstv (void) fflush(stdout);
472 1.17 mlelstv (void) fprintf(stderr, "\n%s: ", progname);
473 1.17 mlelstv (void) fprintf(stderr, tformat(), *tp);
474 1.17 mlelstv (void) fprintf(stderr, " ->");
475 1.17 mlelstv (void) fprintf(stderr, " year=%d", tmp->tm_year);
476 1.17 mlelstv (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
477 1.17 mlelstv (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
478 1.17 mlelstv (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
479 1.17 mlelstv (void) fprintf(stderr, " min=%d", tmp->tm_min);
480 1.17 mlelstv (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
481 1.17 mlelstv (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
482 1.17 mlelstv (void) fprintf(stderr, " -> ");
483 1.17 mlelstv (void) fprintf(stderr, tformat(), t);
484 1.17 mlelstv (void) fprintf(stderr, "\n");
485 1.36 christos errout = true;
486 1.17 mlelstv }
487 1.17 mlelstv }
488 1.17 mlelstv return tmp;
489 1.17 mlelstv }
490 1.17 mlelstv #endif /* !defined TYPECHECK */
491 1.17 mlelstv
492 1.17 mlelstv static void
493 1.26 christos abbrok(const char *const abbrp, const char *const zone)
494 1.17 mlelstv {
495 1.26 christos const char *cp;
496 1.26 christos const char *wp;
497 1.17 mlelstv
498 1.17 mlelstv if (warned)
499 1.17 mlelstv return;
500 1.17 mlelstv cp = abbrp;
501 1.42 christos while (is_alpha(*cp) || is_digit(*cp) || *cp == '-' || *cp == '+')
502 1.17 mlelstv ++cp;
503 1.42 christos if (cp - abbrp < 3)
504 1.42 christos wp = _("has fewer than 3 characters");
505 1.17 mlelstv else if (cp - abbrp > 6)
506 1.42 christos wp = _("has more than 6 characters");
507 1.42 christos else if (*cp)
508 1.42 christos wp = _("has characters other than ASCII alphanumerics, '-' or '+'");
509 1.42 christos else
510 1.17 mlelstv return;
511 1.17 mlelstv (void) fflush(stdout);
512 1.17 mlelstv (void) fprintf(stderr,
513 1.17 mlelstv _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
514 1.17 mlelstv progname, zone, abbrp, wp);
515 1.36 christos warned = errout = true;
516 1.36 christos }
517 1.36 christos
518 1.36 christos /* Return a time zone abbreviation. If the abbreviation needs to be
519 1.36 christos saved, use *BUF (of size *BUFALLOC) to save it, and return the
520 1.36 christos abbreviation in the possibly-reallocated *BUF. Otherwise, just
521 1.36 christos return the abbreviation. Get the abbreviation from TMP.
522 1.36 christos Exit on memory allocation failure. */
523 1.36 christos static char const *
524 1.36 christos saveabbr(char **buf, size_t *bufalloc, struct tm const *tmp)
525 1.36 christos {
526 1.36 christos char const *ab = abbr(tmp);
527 1.36 christos if (HAVE_LOCALTIME_RZ)
528 1.36 christos return ab;
529 1.36 christos else {
530 1.36 christos size_t ablen = strlen(ab);
531 1.36 christos if (*bufalloc <= ablen) {
532 1.36 christos free(*buf);
533 1.36 christos
534 1.36 christos /* Make the new buffer at least twice as long as the
535 1.36 christos old, to avoid O(N**2) behavior on repeated calls. */
536 1.36 christos *bufalloc = sumsize(*bufalloc, ablen + 1);
537 1.36 christos *buf = malloc(*bufalloc);
538 1.36 christos if (! *buf) {
539 1.36 christos err(EXIT_FAILURE, "malloc");
540 1.36 christos }
541 1.36 christos }
542 1.36 christos return strcpy(*buf, ab);
543 1.36 christos }
544 1.36 christos }
545 1.36 christos
546 1.36 christos static void
547 1.36 christos close_file(FILE *stream)
548 1.36 christos {
549 1.36 christos char const *e = (ferror(stream) ? _("I/O error")
550 1.36 christos : fclose(stream) != 0 ? strerror(errno) : NULL);
551 1.36 christos if (e) {
552 1.36 christos errx(EXIT_FAILURE, "%s", e);
553 1.36 christos }
554 1.17 mlelstv }
555 1.17 mlelstv
556 1.24 joerg __dead static void
557 1.26 christos usage(FILE *const stream, const int status)
558 1.17 mlelstv {
559 1.17 mlelstv (void) fprintf(stream,
560 1.29 christos _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
561 1.29 christos "\n"
562 1.29 christos "Report bugs to %s.\n"),
563 1.28 christos progname, progname, REPORT_BUGS_TO);
564 1.36 christos if (status == EXIT_SUCCESS)
565 1.36 christos close_file(stream);
566 1.17 mlelstv exit(status);
567 1.17 mlelstv }
568 1.1 jtc
569 1.1 jtc int
570 1.26 christos main(int argc, char *argv[])
571 1.26 christos {
572 1.36 christos /* These are static so that they're initially zero. */
573 1.36 christos static char * abbrev;
574 1.36 christos static size_t abbrevsize;
575 1.36 christos static struct tm newtm;
576 1.36 christos
577 1.26 christos int i;
578 1.36 christos bool vflag;
579 1.36 christos bool Vflag;
580 1.26 christos char * cutarg;
581 1.29 christos char * cuttimes;
582 1.26 christos time_t cutlotime;
583 1.26 christos time_t cuthitime;
584 1.26 christos time_t now;
585 1.26 christos time_t t;
586 1.26 christos time_t newt;
587 1.26 christos struct tm tm;
588 1.26 christos struct tm * tmp;
589 1.26 christos struct tm * newtmp;
590 1.1 jtc
591 1.29 christos cutlotime = absolute_min_time;
592 1.29 christos cuthitime = absolute_max_time;
593 1.17 mlelstv #if HAVE_GETTEXT
594 1.17 mlelstv (void) setlocale(LC_ALL, "");
595 1.3 jtc #ifdef TZ_DOMAINDIR
596 1.3 jtc (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
597 1.17 mlelstv #endif /* defined TEXTDOMAINDIR */
598 1.3 jtc (void) textdomain(TZ_DOMAIN);
599 1.17 mlelstv #endif /* HAVE_GETTEXT */
600 1.1 jtc progname = argv[0];
601 1.14 kleink for (i = 1; i < argc; ++i)
602 1.14 kleink if (strcmp(argv[i], "--version") == 0) {
603 1.28 christos (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
604 1.36 christos return EXIT_SUCCESS;
605 1.17 mlelstv } else if (strcmp(argv[i], "--help") == 0) {
606 1.22 christos usage(stdout, EXIT_SUCCESS);
607 1.14 kleink }
608 1.36 christos vflag = Vflag = false;
609 1.29 christos cutarg = cuttimes = NULL;
610 1.29 christos for (;;)
611 1.29 christos switch (getopt(argc, argv, "c:t:vV")) {
612 1.29 christos case 'c': cutarg = optarg; break;
613 1.29 christos case 't': cuttimes = optarg; break;
614 1.36 christos case 'v': vflag = true; break;
615 1.36 christos case 'V': Vflag = true; break;
616 1.29 christos case -1:
617 1.29 christos if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
618 1.29 christos goto arg_processing_done;
619 1.29 christos /* Fall through. */
620 1.29 christos default:
621 1.29 christos usage(stderr, EXIT_FAILURE);
622 1.29 christos }
623 1.29 christos arg_processing_done:;
624 1.29 christos
625 1.29 christos if (vflag | Vflag) {
626 1.29 christos intmax_t lo;
627 1.29 christos intmax_t hi;
628 1.32 christos char *loend, *hiend;
629 1.30 christos intmax_t cutloyear = ZDUMP_LO_YEAR;
630 1.30 christos intmax_t cuthiyear = ZDUMP_HI_YEAR;
631 1.17 mlelstv if (cutarg != NULL) {
632 1.32 christos lo = strtoimax(cutarg, &loend, 10);
633 1.32 christos if (cutarg != loend && !*loend) {
634 1.32 christos hi = lo;
635 1.32 christos cuthiyear = hi;
636 1.32 christos } else if (cutarg != loend && *loend == ','
637 1.32 christos && (hi = strtoimax(loend + 1, &hiend, 10),
638 1.32 christos loend + 1 != hiend && !*hiend)) {
639 1.32 christos cutloyear = lo;
640 1.17 mlelstv cuthiyear = hi;
641 1.17 mlelstv } else {
642 1.36 christos fprintf(stderr, _("%s: wild -c argument %s\n"),
643 1.17 mlelstv progname, cutarg);
644 1.36 christos return EXIT_FAILURE;
645 1.17 mlelstv }
646 1.17 mlelstv }
647 1.29 christos if (cutarg != NULL || cuttimes == NULL) {
648 1.29 christos cutlotime = yeartot(cutloyear);
649 1.29 christos cuthitime = yeartot(cuthiyear);
650 1.29 christos }
651 1.29 christos if (cuttimes != NULL) {
652 1.32 christos lo = strtoimax(cuttimes, &loend, 10);
653 1.32 christos if (cuttimes != loend && !*loend) {
654 1.32 christos hi = lo;
655 1.29 christos if (hi < cuthitime) {
656 1.29 christos if (hi < absolute_min_time)
657 1.29 christos hi = absolute_min_time;
658 1.29 christos cuthitime = hi;
659 1.29 christos }
660 1.32 christos } else if (cuttimes != loend && *loend == ','
661 1.32 christos && (hi = strtoimax(loend + 1, &hiend, 10),
662 1.32 christos loend + 1 != hiend && !*hiend)) {
663 1.29 christos if (cutlotime < lo) {
664 1.29 christos if (absolute_max_time < lo)
665 1.29 christos lo = absolute_max_time;
666 1.29 christos cutlotime = lo;
667 1.29 christos }
668 1.29 christos if (hi < cuthitime) {
669 1.29 christos if (hi < absolute_min_time)
670 1.29 christos hi = absolute_min_time;
671 1.29 christos cuthitime = hi;
672 1.29 christos }
673 1.29 christos } else {
674 1.29 christos (void) fprintf(stderr,
675 1.29 christos _("%s: wild -t argument %s\n"),
676 1.29 christos progname, cuttimes);
677 1.36 christos return EXIT_FAILURE;
678 1.29 christos }
679 1.29 christos }
680 1.1 jtc }
681 1.36 christos gmtzinit();
682 1.36 christos now = time(NULL);
683 1.1 jtc longest = 0;
684 1.36 christos for (i = optind; i < argc; i++) {
685 1.36 christos size_t arglen = strlen(argv[i]);
686 1.36 christos if (longest < arglen)
687 1.36 christos longest = arglen < INT_MAX ? arglen : INT_MAX;
688 1.36 christos }
689 1.1 jtc
690 1.1 jtc for (i = optind; i < argc; ++i) {
691 1.36 christos timezone_t tz = tzalloc(argv[i]);
692 1.36 christos char const *ab;
693 1.36 christos if (!tz) {
694 1.36 christos errx(EXIT_FAILURE, "%s", argv[i]);
695 1.36 christos }
696 1.29 christos if (! (vflag | Vflag)) {
697 1.36 christos show(tz, argv[i], now, false);
698 1.36 christos tzfree(tz);
699 1.1 jtc continue;
700 1.3 jtc }
701 1.36 christos warned = false;
702 1.17 mlelstv t = absolute_min_time;
703 1.29 christos if (!Vflag) {
704 1.36 christos show(tz, argv[i], t, true);
705 1.31 christos t += SECSPERDAY;
706 1.36 christos show(tz, argv[i], t, true);
707 1.29 christos }
708 1.17 mlelstv if (t < cutlotime)
709 1.17 mlelstv t = cutlotime;
710 1.36 christos tmp = my_localtime_rz(tz, &t, &tm);
711 1.36 christos if (tmp)
712 1.36 christos ab = saveabbr(&abbrev, &abbrevsize, &tm);
713 1.36 christos else
714 1.36 christos ab = NULL;
715 1.36 christos while (t < cuthitime) {
716 1.36 christos newt = ((t < absolute_max_time - SECSPERDAY / 2
717 1.36 christos && t + SECSPERDAY / 2 < cuthitime)
718 1.31 christos ? t + SECSPERDAY / 2
719 1.36 christos : cuthitime);
720 1.36 christos newtmp = localtime_rz(tz, &newt, &newtm);
721 1.17 mlelstv if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
722 1.36 christos (delta(&newtm, &tm) != (newt - t) ||
723 1.36 christos newtm.tm_isdst != tm.tm_isdst ||
724 1.36 christos strcmp(abbr(&newtm), ab) != 0)) {
725 1.36 christos newt = hunt(tz, argv[i], t, newt);
726 1.36 christos newtmp = localtime_rz(tz, &newt, &newtm);
727 1.36 christos if (newtmp)
728 1.36 christos ab = saveabbr(&abbrev, &abbrevsize,
729 1.36 christos &newtm);
730 1.1 jtc }
731 1.1 jtc t = newt;
732 1.1 jtc tm = newtm;
733 1.17 mlelstv tmp = newtmp;
734 1.1 jtc }
735 1.29 christos if (!Vflag) {
736 1.29 christos t = absolute_max_time;
737 1.31 christos t -= SECSPERDAY;
738 1.36 christos show(tz, argv[i], t, true);
739 1.31 christos t += SECSPERDAY;
740 1.36 christos show(tz, argv[i], t, true);
741 1.29 christos }
742 1.36 christos tzfree(tz);
743 1.1 jtc }
744 1.36 christos close_file(stdout);
745 1.36 christos if (errout && (ferror(stderr) || fclose(stderr) != 0))
746 1.36 christos return EXIT_FAILURE;
747 1.36 christos return EXIT_SUCCESS;
748 1.17 mlelstv }
749 1.1 jtc
750 1.1 jtc static time_t
751 1.42 christos yeartot(intmax_t y)
752 1.17 mlelstv {
753 1.31 christos intmax_t myy, seconds, years;
754 1.29 christos time_t t;
755 1.17 mlelstv
756 1.17 mlelstv myy = EPOCH_YEAR;
757 1.17 mlelstv t = 0;
758 1.31 christos while (myy < y) {
759 1.31 christos if (SECSPER400YEARS_FITS && 400 <= y - myy) {
760 1.31 christos intmax_t diff400 = (y - myy) / 400;
761 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
762 1.31 christos return absolute_max_time;
763 1.31 christos seconds = diff400 * SECSPER400YEARS;
764 1.31 christos years = diff400 * 400;
765 1.31 christos } else {
766 1.17 mlelstv seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
767 1.31 christos years = 1;
768 1.31 christos }
769 1.31 christos myy += years;
770 1.31 christos if (t > absolute_max_time - seconds)
771 1.31 christos return absolute_max_time;
772 1.31 christos t += seconds;
773 1.31 christos }
774 1.31 christos while (y < myy) {
775 1.31 christos if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
776 1.31 christos intmax_t diff400 = (myy - y) / 400;
777 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
778 1.31 christos return absolute_min_time;
779 1.31 christos seconds = diff400 * SECSPER400YEARS;
780 1.31 christos years = diff400 * 400;
781 1.17 mlelstv } else {
782 1.31 christos seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
783 1.31 christos years = 1;
784 1.17 mlelstv }
785 1.31 christos myy -= years;
786 1.31 christos if (t < absolute_min_time + seconds)
787 1.31 christos return absolute_min_time;
788 1.31 christos t -= seconds;
789 1.17 mlelstv }
790 1.17 mlelstv return t;
791 1.17 mlelstv }
792 1.17 mlelstv
793 1.17 mlelstv static time_t
794 1.36 christos hunt(timezone_t tz, char *name, time_t lot, time_t hit)
795 1.17 mlelstv {
796 1.36 christos static char * loab;
797 1.36 christos static size_t loabsize;
798 1.36 christos char const * ab;
799 1.17 mlelstv time_t t;
800 1.17 mlelstv struct tm lotm;
801 1.26 christos struct tm * lotmp;
802 1.17 mlelstv struct tm tm;
803 1.26 christos struct tm * tmp;
804 1.17 mlelstv
805 1.36 christos lotmp = my_localtime_rz(tz, &lot, &lotm);
806 1.36 christos if (lotmp)
807 1.36 christos ab = saveabbr(&loab, &loabsize, &lotm);
808 1.36 christos else
809 1.36 christos ab = NULL;
810 1.17 mlelstv for ( ; ; ) {
811 1.29 christos time_t diff = hit - lot;
812 1.17 mlelstv if (diff < 2)
813 1.17 mlelstv break;
814 1.17 mlelstv t = lot;
815 1.17 mlelstv t += diff / 2;
816 1.1 jtc if (t <= lot)
817 1.1 jtc ++t;
818 1.1 jtc else if (t >= hit)
819 1.1 jtc --t;
820 1.36 christos tmp = my_localtime_rz(tz, &t, &tm);
821 1.17 mlelstv if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
822 1.17 mlelstv (delta(&tm, &lotm) == (t - lot) &&
823 1.1 jtc tm.tm_isdst == lotm.tm_isdst &&
824 1.36 christos strcmp(abbr(&tm), ab) == 0)) {
825 1.1 jtc lot = t;
826 1.1 jtc lotm = tm;
827 1.17 mlelstv lotmp = tmp;
828 1.1 jtc } else hit = t;
829 1.1 jtc }
830 1.36 christos show(tz, name, lot, true);
831 1.36 christos show(tz, name, hit, true);
832 1.1 jtc return hit;
833 1.1 jtc }
834 1.1 jtc
835 1.1 jtc /*
836 1.17 mlelstv ** Thanks to Paul Eggert for logic used in delta.
837 1.1 jtc */
838 1.1 jtc
839 1.29 christos static intmax_t
840 1.26 christos delta(struct tm *newp, struct tm *oldp)
841 1.1 jtc {
842 1.29 christos intmax_t result;
843 1.29 christos int tmy;
844 1.1 jtc
845 1.1 jtc if (newp->tm_year < oldp->tm_year)
846 1.1 jtc return -delta(oldp, newp);
847 1.1 jtc result = 0;
848 1.1 jtc for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
849 1.17 mlelstv result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
850 1.1 jtc result += newp->tm_yday - oldp->tm_yday;
851 1.1 jtc result *= HOURSPERDAY;
852 1.1 jtc result += newp->tm_hour - oldp->tm_hour;
853 1.1 jtc result *= MINSPERHOUR;
854 1.1 jtc result += newp->tm_min - oldp->tm_min;
855 1.1 jtc result *= SECSPERMIN;
856 1.1 jtc result += newp->tm_sec - oldp->tm_sec;
857 1.1 jtc return result;
858 1.1 jtc }
859 1.1 jtc
860 1.36 christos #ifndef TM_GMTOFF
861 1.36 christos /* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday.
862 1.39 christos Assume A and B differ by at most one year. */
863 1.36 christos static int
864 1.36 christos adjusted_yday(struct tm const *a, struct tm const *b)
865 1.36 christos {
866 1.36 christos int yday = a->tm_yday;
867 1.36 christos if (b->tm_year < a->tm_year)
868 1.36 christos yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE);
869 1.36 christos return yday;
870 1.36 christos }
871 1.36 christos #endif
872 1.36 christos
873 1.36 christos /* If A is the broken-down local time and B the broken-down UTC for
874 1.36 christos the same instant, return A's UTC offset in seconds, where positive
875 1.36 christos offsets are east of Greenwich. On failure, return LONG_MIN. */
876 1.36 christos static long
877 1.36 christos gmtoff(struct tm const *a, struct tm const *b)
878 1.36 christos {
879 1.36 christos #ifdef TM_GMTOFF
880 1.36 christos return a->TM_GMTOFF;
881 1.36 christos #else
882 1.36 christos if (! b)
883 1.36 christos return LONG_MIN;
884 1.36 christos else {
885 1.36 christos int ayday = adjusted_yday(a, b);
886 1.36 christos int byday = adjusted_yday(b, a);
887 1.36 christos int days = ayday - byday;
888 1.36 christos long hours = a->tm_hour - b->tm_hour + 24 * days;
889 1.36 christos long minutes = a->tm_min - b->tm_min + 60 * hours;
890 1.36 christos long seconds = a->tm_sec - b->tm_sec + 60 * minutes;
891 1.36 christos return seconds;
892 1.36 christos }
893 1.36 christos #endif
894 1.36 christos }
895 1.36 christos
896 1.1 jtc static void
897 1.36 christos show(timezone_t tz, char *zone, time_t t, bool v)
898 1.1 jtc {
899 1.26 christos struct tm * tmp;
900 1.36 christos struct tm * gmtmp;
901 1.36 christos struct tm tm, gmtm;
902 1.1 jtc
903 1.5 jtc (void) printf("%-*s ", (int) longest, zone);
904 1.1 jtc if (v) {
905 1.36 christos gmtmp = my_gmtime_r(&t, &gmtm);
906 1.36 christos if (gmtmp == NULL) {
907 1.36 christos printf(tformat(), t);
908 1.17 mlelstv } else {
909 1.36 christos dumptime(gmtmp);
910 1.31 christos (void) printf(" UT");
911 1.17 mlelstv }
912 1.17 mlelstv (void) printf(" = ");
913 1.17 mlelstv }
914 1.36 christos tmp = my_localtime_rz(tz, &t, &tm);
915 1.17 mlelstv dumptime(tmp);
916 1.17 mlelstv if (tmp != NULL) {
917 1.17 mlelstv if (*abbr(tmp) != '\0')
918 1.17 mlelstv (void) printf(" %s", abbr(tmp));
919 1.17 mlelstv if (v) {
920 1.36 christos long off = gmtoff(tmp, gmtmp);
921 1.17 mlelstv (void) printf(" isdst=%d", tmp->tm_isdst);
922 1.36 christos if (off != LONG_MIN)
923 1.36 christos (void) printf(" gmtoff=%ld", off);
924 1.17 mlelstv }
925 1.1 jtc }
926 1.1 jtc (void) printf("\n");
927 1.17 mlelstv if (tmp != NULL && *abbr(tmp) != '\0')
928 1.17 mlelstv abbrok(abbr(tmp), zone);
929 1.1 jtc }
930 1.1 jtc
931 1.9 mycroft static const char *
932 1.36 christos abbr(struct tm const *tmp)
933 1.1 jtc {
934 1.36 christos #ifdef TM_ZONE
935 1.36 christos return tmp->TM_ZONE;
936 1.36 christos #else
937 1.36 christos return (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst]
938 1.36 christos ? tzname[0 < tmp->tm_isdst]
939 1.36 christos : "");
940 1.36 christos #endif
941 1.1 jtc }
942 1.17 mlelstv
943 1.17 mlelstv /*
944 1.17 mlelstv ** The code below can fail on certain theoretical systems;
945 1.17 mlelstv ** it works on all known real-world systems as of 2004-12-30.
946 1.17 mlelstv */
947 1.17 mlelstv
948 1.17 mlelstv static const char *
949 1.17 mlelstv tformat(void)
950 1.17 mlelstv {
951 1.17 mlelstv if (0 > (time_t) -1) { /* signed */
952 1.29 christos if (sizeof (time_t) == sizeof (intmax_t))
953 1.29 christos return "%"PRIdMAX;
954 1.17 mlelstv if (sizeof (time_t) > sizeof (long))
955 1.17 mlelstv return "%lld";
956 1.17 mlelstv if (sizeof (time_t) > sizeof (int))
957 1.17 mlelstv return "%ld";
958 1.17 mlelstv return "%d";
959 1.17 mlelstv }
960 1.29 christos #ifdef PRIuMAX
961 1.29 christos if (sizeof (time_t) == sizeof (uintmax_t))
962 1.29 christos return "%"PRIuMAX;
963 1.29 christos #endif
964 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned long))
965 1.17 mlelstv return "%llu";
966 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned int))
967 1.17 mlelstv return "%lu";
968 1.17 mlelstv return "%u";
969 1.17 mlelstv }
970 1.17 mlelstv
971 1.17 mlelstv static void
972 1.26 christos dumptime(const struct tm *timeptr)
973 1.17 mlelstv {
974 1.17 mlelstv static const char wday_name[][3] = {
975 1.17 mlelstv "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
976 1.17 mlelstv };
977 1.17 mlelstv static const char mon_name[][3] = {
978 1.17 mlelstv "Jan", "Feb", "Mar", "Apr", "May", "Jun",
979 1.17 mlelstv "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
980 1.17 mlelstv };
981 1.26 christos const char * wn;
982 1.26 christos const char * mn;
983 1.26 christos int lead;
984 1.26 christos int trail;
985 1.17 mlelstv
986 1.17 mlelstv if (timeptr == NULL) {
987 1.36 christos printf("NULL");
988 1.17 mlelstv return;
989 1.17 mlelstv }
990 1.17 mlelstv /*
991 1.36 christos ** The packaged localtime_rz and gmtime_r never put out-of-range
992 1.17 mlelstv ** values in tm_wday or tm_mon, but since this code might be compiled
993 1.17 mlelstv ** with other (perhaps experimental) versions, paranoia is in order.
994 1.17 mlelstv */
995 1.17 mlelstv if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
996 1.17 mlelstv (int) (sizeof wday_name / sizeof wday_name[0]))
997 1.17 mlelstv wn = "???";
998 1.17 mlelstv else wn = wday_name[timeptr->tm_wday];
999 1.17 mlelstv if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
1000 1.17 mlelstv (int) (sizeof mon_name / sizeof mon_name[0]))
1001 1.17 mlelstv mn = "???";
1002 1.17 mlelstv else mn = mon_name[timeptr->tm_mon];
1003 1.36 christos printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
1004 1.17 mlelstv wn, mn,
1005 1.17 mlelstv timeptr->tm_mday, timeptr->tm_hour,
1006 1.17 mlelstv timeptr->tm_min, timeptr->tm_sec);
1007 1.17 mlelstv #define DIVISOR 10
1008 1.17 mlelstv trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
1009 1.17 mlelstv lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
1010 1.17 mlelstv trail / DIVISOR;
1011 1.17 mlelstv trail %= DIVISOR;
1012 1.17 mlelstv if (trail < 0 && lead > 0) {
1013 1.17 mlelstv trail += DIVISOR;
1014 1.17 mlelstv --lead;
1015 1.17 mlelstv } else if (lead < 0 && trail > 0) {
1016 1.17 mlelstv trail -= DIVISOR;
1017 1.17 mlelstv ++lead;
1018 1.17 mlelstv }
1019 1.17 mlelstv if (lead == 0)
1020 1.36 christos printf("%d", trail);
1021 1.36 christos else printf("%d%d", lead, ((trail < 0) ? -trail : trail));
1022 1.17 mlelstv }
1023