zdump.c revision 1.41 1 1.41 christos /* $NetBSD: zdump.c,v 1.41 2015/06/21 16:06:51 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.41 christos __RCSID("$NetBSD: zdump.c,v 1.41 2015/06/21 16:06:51 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.34 christos /* Is A an alphabetic character in the C locale? */
288 1.36 christos static bool
289 1.34 christos is_alpha(char a)
290 1.34 christos {
291 1.34 christos switch (a) {
292 1.34 christos default:
293 1.36 christos return false;
294 1.34 christos case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
295 1.34 christos case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
296 1.34 christos case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
297 1.34 christos case 'V': case 'W': case 'X': case 'Y': case 'Z':
298 1.34 christos case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
299 1.34 christos case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
300 1.34 christos case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
301 1.34 christos case 'v': case 'w': case 'x': case 'y': case 'z':
302 1.36 christos return true;
303 1.34 christos }
304 1.34 christos }
305 1.34 christos
306 1.36 christos /* Return A + B, exiting if the result would overflow. */
307 1.36 christos static size_t
308 1.36 christos sumsize(size_t a, size_t b)
309 1.36 christos {
310 1.36 christos size_t sum = a + b;
311 1.36 christos if (sum < a)
312 1.36 christos errx(EXIT_FAILURE, "size overflow");
313 1.36 christos return sum;
314 1.36 christos }
315 1.36 christos
316 1.36 christos #if ! HAVE_TZSET
317 1.36 christos # undef tzset
318 1.36 christos # define tzset zdump_tzset
319 1.36 christos static void tzset(void) { }
320 1.36 christos #endif
321 1.36 christos
322 1.36 christos /* Assume gmtime_r works if localtime_r does.
323 1.36 christos A replacement localtime_r is defined below if needed. */
324 1.36 christos #if ! HAVE_LOCALTIME_R
325 1.36 christos
326 1.36 christos # undef gmtime_r
327 1.36 christos # define gmtime_r zdump_gmtime_r
328 1.36 christos
329 1.36 christos static struct tm *
330 1.36 christos gmtime_r(time_t *tp, struct tm *tmp)
331 1.36 christos {
332 1.36 christos struct tm *r = gmtime(tp);
333 1.36 christos if (r) {
334 1.36 christos *tmp = *r;
335 1.36 christos r = tmp;
336 1.36 christos }
337 1.36 christos return r;
338 1.36 christos }
339 1.36 christos
340 1.36 christos #endif
341 1.36 christos
342 1.36 christos /* Platforms with TM_ZONE don't need tzname, so they can use the
343 1.36 christos faster localtime_rz or localtime_r if available. */
344 1.36 christos
345 1.36 christos #if defined TM_ZONE && HAVE_LOCALTIME_RZ
346 1.36 christos # define USE_LOCALTIME_RZ true
347 1.36 christos #else
348 1.36 christos # define USE_LOCALTIME_RZ false
349 1.36 christos #endif
350 1.36 christos
351 1.36 christos #if ! USE_LOCALTIME_RZ
352 1.36 christos
353 1.36 christos # if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET
354 1.36 christos # undef localtime_r
355 1.36 christos # define localtime_r zdump_localtime_r
356 1.36 christos static struct tm *
357 1.36 christos localtime_r(time_t *tp, struct tm *tmp)
358 1.36 christos {
359 1.36 christos struct tm *r = localtime(tp);
360 1.36 christos if (r) {
361 1.36 christos *tmp = *r;
362 1.36 christos r = tmp;
363 1.36 christos }
364 1.36 christos return r;
365 1.36 christos }
366 1.36 christos # endif
367 1.36 christos
368 1.36 christos # undef localtime_rz
369 1.36 christos # define localtime_rz zdump_localtime_rz
370 1.36 christos static struct tm *
371 1.36 christos localtime_rz(timezone_t rz, time_t *tp, struct tm *tmp)
372 1.36 christos {
373 1.36 christos return localtime_r(tp, tmp);
374 1.36 christos }
375 1.36 christos
376 1.36 christos # ifdef TYPECHECK
377 1.36 christos # undef mktime_z
378 1.36 christos # define mktime_z zdump_mktime_z
379 1.36 christos static time_t
380 1.36 christos mktime_z(timezone_t tz, struct tm *tmp)
381 1.36 christos {
382 1.36 christos return mktime(tmp);
383 1.36 christos }
384 1.36 christos # endif
385 1.36 christos
386 1.36 christos # undef tzalloc
387 1.36 christos # undef tzfree
388 1.36 christos # define tzalloc zdump_tzalloc
389 1.36 christos # define tzfree zdump_tzfree
390 1.36 christos
391 1.36 christos static timezone_t
392 1.36 christos tzalloc(char const *val)
393 1.36 christos {
394 1.36 christos static char **fakeenv;
395 1.36 christos char **env = fakeenv;
396 1.36 christos char *env0;
397 1.36 christos if (! env) {
398 1.36 christos char **e = environ;
399 1.36 christos int to;
400 1.36 christos
401 1.36 christos while (*e++)
402 1.36 christos continue;
403 1.36 christos env = malloc(sumsize(sizeof *environ,
404 1.36 christos (e - environ) * sizeof *environ));
405 1.36 christos if (! env) {
406 1.36 christos err(EXIT_FAILURE, "malloc");
407 1.36 christos }
408 1.36 christos to = 1;
409 1.36 christos for (e = environ; (env[to] = *e); e++)
410 1.36 christos to += strncmp(*e, "TZ=", 3) != 0;
411 1.36 christos }
412 1.36 christos env0 = malloc(sumsize(sizeof "TZ=", strlen(val)));
413 1.36 christos if (! env0) {
414 1.36 christos err(EXIT_FAILURE, "malloc");
415 1.36 christos }
416 1.36 christos env[0] = strcat(strcpy(env0, "TZ="), val);
417 1.36 christos environ = fakeenv = env;
418 1.36 christos tzset();
419 1.36 christos return env;
420 1.36 christos }
421 1.36 christos
422 1.36 christos static void
423 1.36 christos tzfree(timezone_t env)
424 1.36 christos {
425 1.36 christos environ = env + 1;
426 1.36 christos free(env[0]);
427 1.36 christos }
428 1.36 christos #endif /* ! USE_LOCALTIME_RZ */
429 1.36 christos
430 1.36 christos /* A UTC time zone, and its initializer. */
431 1.36 christos static timezone_t gmtz;
432 1.36 christos static void
433 1.36 christos gmtzinit(void)
434 1.36 christos {
435 1.36 christos if (USE_LOCALTIME_RZ) {
436 1.36 christos static char const utc[] = "UTC0";
437 1.36 christos gmtz = tzalloc(utc);
438 1.36 christos if (!gmtz) {
439 1.36 christos err(EXIT_FAILURE, "Cannot create %s", utc);
440 1.36 christos }
441 1.36 christos }
442 1.36 christos }
443 1.36 christos
444 1.36 christos /* Convert *TP to UTC, storing the broken-down time into *TMP.
445 1.36 christos Return TMP if successful, NULL otherwise. This is like gmtime_r(TP, TMP),
446 1.36 christos except typically faster if USE_LOCALTIME_RZ. */
447 1.36 christos static struct tm *
448 1.36 christos my_gmtime_r(time_t *tp, struct tm *tmp)
449 1.36 christos {
450 1.36 christos return USE_LOCALTIME_RZ ?
451 1.36 christos localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp);
452 1.36 christos }
453 1.36 christos
454 1.17 mlelstv #ifndef TYPECHECK
455 1.36 christos #define my_localtime_rz localtime_rz
456 1.17 mlelstv #else /* !defined TYPECHECK */
457 1.17 mlelstv static struct tm *
458 1.36 christos my_localtime_rz(timezone_t tz, const time_t *tp, struct tm *tmp)
459 1.17 mlelstv {
460 1.36 christos tmp = localtime_rz(tz, tp, tmp);
461 1.36 christos if (tmp) {
462 1.17 mlelstv struct tm tm;
463 1.26 christos time_t t;
464 1.17 mlelstv
465 1.17 mlelstv tm = *tmp;
466 1.36 christos t = mktime_z(tz, &tm);
467 1.31 christos if (t != *tp) {
468 1.17 mlelstv (void) fflush(stdout);
469 1.17 mlelstv (void) fprintf(stderr, "\n%s: ", progname);
470 1.17 mlelstv (void) fprintf(stderr, tformat(), *tp);
471 1.17 mlelstv (void) fprintf(stderr, " ->");
472 1.17 mlelstv (void) fprintf(stderr, " year=%d", tmp->tm_year);
473 1.17 mlelstv (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
474 1.17 mlelstv (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
475 1.17 mlelstv (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
476 1.17 mlelstv (void) fprintf(stderr, " min=%d", tmp->tm_min);
477 1.17 mlelstv (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
478 1.17 mlelstv (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
479 1.17 mlelstv (void) fprintf(stderr, " -> ");
480 1.17 mlelstv (void) fprintf(stderr, tformat(), t);
481 1.17 mlelstv (void) fprintf(stderr, "\n");
482 1.36 christos errout = true;
483 1.17 mlelstv }
484 1.17 mlelstv }
485 1.17 mlelstv return tmp;
486 1.17 mlelstv }
487 1.17 mlelstv #endif /* !defined TYPECHECK */
488 1.17 mlelstv
489 1.17 mlelstv static void
490 1.26 christos abbrok(const char *const abbrp, const char *const zone)
491 1.17 mlelstv {
492 1.26 christos const char *cp;
493 1.26 christos const char *wp;
494 1.17 mlelstv
495 1.17 mlelstv if (warned)
496 1.17 mlelstv return;
497 1.17 mlelstv cp = abbrp;
498 1.17 mlelstv wp = NULL;
499 1.34 christos while (is_alpha(*cp))
500 1.17 mlelstv ++cp;
501 1.17 mlelstv if (cp - abbrp == 0)
502 1.17 mlelstv wp = _("lacks alphabetic at start");
503 1.17 mlelstv else if (cp - abbrp < 3)
504 1.17 mlelstv wp = _("has fewer than 3 alphabetics");
505 1.17 mlelstv else if (cp - abbrp > 6)
506 1.17 mlelstv wp = _("has more than 6 alphabetics");
507 1.17 mlelstv if (wp == NULL && (*cp == '+' || *cp == '-')) {
508 1.17 mlelstv ++cp;
509 1.34 christos if ('0' <= *cp && *cp <= '9')
510 1.34 christos if (*cp++ == '1' && '0' <= *cp && *cp <= '4')
511 1.34 christos cp++;
512 1.17 mlelstv if (*cp != '\0')
513 1.17 mlelstv wp = _("differs from POSIX standard");
514 1.17 mlelstv }
515 1.17 mlelstv if (wp == NULL)
516 1.17 mlelstv return;
517 1.17 mlelstv (void) fflush(stdout);
518 1.17 mlelstv (void) fprintf(stderr,
519 1.17 mlelstv _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
520 1.17 mlelstv progname, zone, abbrp, wp);
521 1.36 christos warned = errout = true;
522 1.36 christos }
523 1.36 christos
524 1.36 christos /* Return a time zone abbreviation. If the abbreviation needs to be
525 1.36 christos saved, use *BUF (of size *BUFALLOC) to save it, and return the
526 1.36 christos abbreviation in the possibly-reallocated *BUF. Otherwise, just
527 1.36 christos return the abbreviation. Get the abbreviation from TMP.
528 1.36 christos Exit on memory allocation failure. */
529 1.36 christos static char const *
530 1.36 christos saveabbr(char **buf, size_t *bufalloc, struct tm const *tmp)
531 1.36 christos {
532 1.36 christos char const *ab = abbr(tmp);
533 1.36 christos if (HAVE_LOCALTIME_RZ)
534 1.36 christos return ab;
535 1.36 christos else {
536 1.36 christos size_t ablen = strlen(ab);
537 1.36 christos if (*bufalloc <= ablen) {
538 1.36 christos free(*buf);
539 1.36 christos
540 1.36 christos /* Make the new buffer at least twice as long as the
541 1.36 christos old, to avoid O(N**2) behavior on repeated calls. */
542 1.36 christos *bufalloc = sumsize(*bufalloc, ablen + 1);
543 1.36 christos *buf = malloc(*bufalloc);
544 1.36 christos if (! *buf) {
545 1.36 christos err(EXIT_FAILURE, "malloc");
546 1.36 christos }
547 1.36 christos }
548 1.36 christos return strcpy(*buf, ab);
549 1.36 christos }
550 1.36 christos }
551 1.36 christos
552 1.36 christos static void
553 1.36 christos close_file(FILE *stream)
554 1.36 christos {
555 1.36 christos char const *e = (ferror(stream) ? _("I/O error")
556 1.36 christos : fclose(stream) != 0 ? strerror(errno) : NULL);
557 1.36 christos if (e) {
558 1.36 christos errx(EXIT_FAILURE, "%s", e);
559 1.36 christos }
560 1.17 mlelstv }
561 1.17 mlelstv
562 1.24 joerg __dead static void
563 1.26 christos usage(FILE *const stream, const int status)
564 1.17 mlelstv {
565 1.17 mlelstv (void) fprintf(stream,
566 1.29 christos _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
567 1.29 christos "\n"
568 1.29 christos "Report bugs to %s.\n"),
569 1.28 christos progname, progname, REPORT_BUGS_TO);
570 1.36 christos if (status == EXIT_SUCCESS)
571 1.36 christos close_file(stream);
572 1.17 mlelstv exit(status);
573 1.17 mlelstv }
574 1.1 jtc
575 1.1 jtc int
576 1.26 christos main(int argc, char *argv[])
577 1.26 christos {
578 1.36 christos /* These are static so that they're initially zero. */
579 1.36 christos static char * abbrev;
580 1.36 christos static size_t abbrevsize;
581 1.36 christos static struct tm newtm;
582 1.36 christos
583 1.26 christos int i;
584 1.36 christos bool vflag;
585 1.36 christos bool Vflag;
586 1.26 christos char * cutarg;
587 1.29 christos char * cuttimes;
588 1.26 christos time_t cutlotime;
589 1.26 christos time_t cuthitime;
590 1.26 christos time_t now;
591 1.26 christos time_t t;
592 1.26 christos time_t newt;
593 1.26 christos struct tm tm;
594 1.26 christos struct tm * tmp;
595 1.26 christos struct tm * newtmp;
596 1.1 jtc
597 1.29 christos cutlotime = absolute_min_time;
598 1.29 christos cuthitime = absolute_max_time;
599 1.17 mlelstv #if HAVE_GETTEXT
600 1.17 mlelstv (void) setlocale(LC_ALL, "");
601 1.3 jtc #ifdef TZ_DOMAINDIR
602 1.3 jtc (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
603 1.17 mlelstv #endif /* defined TEXTDOMAINDIR */
604 1.3 jtc (void) textdomain(TZ_DOMAIN);
605 1.17 mlelstv #endif /* HAVE_GETTEXT */
606 1.1 jtc progname = argv[0];
607 1.14 kleink for (i = 1; i < argc; ++i)
608 1.14 kleink if (strcmp(argv[i], "--version") == 0) {
609 1.28 christos (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
610 1.36 christos return EXIT_SUCCESS;
611 1.17 mlelstv } else if (strcmp(argv[i], "--help") == 0) {
612 1.22 christos usage(stdout, EXIT_SUCCESS);
613 1.14 kleink }
614 1.36 christos vflag = Vflag = false;
615 1.29 christos cutarg = cuttimes = NULL;
616 1.29 christos for (;;)
617 1.29 christos switch (getopt(argc, argv, "c:t:vV")) {
618 1.29 christos case 'c': cutarg = optarg; break;
619 1.29 christos case 't': cuttimes = optarg; break;
620 1.36 christos case 'v': vflag = true; break;
621 1.36 christos case 'V': Vflag = true; break;
622 1.29 christos case -1:
623 1.29 christos if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
624 1.29 christos goto arg_processing_done;
625 1.29 christos /* Fall through. */
626 1.29 christos default:
627 1.29 christos usage(stderr, EXIT_FAILURE);
628 1.29 christos }
629 1.29 christos arg_processing_done:;
630 1.29 christos
631 1.29 christos if (vflag | Vflag) {
632 1.29 christos intmax_t lo;
633 1.29 christos intmax_t hi;
634 1.32 christos char *loend, *hiend;
635 1.30 christos intmax_t cutloyear = ZDUMP_LO_YEAR;
636 1.30 christos intmax_t cuthiyear = ZDUMP_HI_YEAR;
637 1.17 mlelstv if (cutarg != NULL) {
638 1.32 christos lo = strtoimax(cutarg, &loend, 10);
639 1.32 christos if (cutarg != loend && !*loend) {
640 1.32 christos hi = lo;
641 1.32 christos cuthiyear = hi;
642 1.32 christos } else if (cutarg != loend && *loend == ','
643 1.32 christos && (hi = strtoimax(loend + 1, &hiend, 10),
644 1.32 christos loend + 1 != hiend && !*hiend)) {
645 1.32 christos cutloyear = lo;
646 1.17 mlelstv cuthiyear = hi;
647 1.17 mlelstv } else {
648 1.36 christos fprintf(stderr, _("%s: wild -c argument %s\n"),
649 1.17 mlelstv progname, cutarg);
650 1.36 christos return EXIT_FAILURE;
651 1.17 mlelstv }
652 1.17 mlelstv }
653 1.29 christos if (cutarg != NULL || cuttimes == NULL) {
654 1.29 christos cutlotime = yeartot(cutloyear);
655 1.29 christos cuthitime = yeartot(cuthiyear);
656 1.29 christos }
657 1.29 christos if (cuttimes != NULL) {
658 1.32 christos lo = strtoimax(cuttimes, &loend, 10);
659 1.32 christos if (cuttimes != loend && !*loend) {
660 1.32 christos hi = lo;
661 1.29 christos if (hi < cuthitime) {
662 1.29 christos if (hi < absolute_min_time)
663 1.29 christos hi = absolute_min_time;
664 1.29 christos cuthitime = hi;
665 1.29 christos }
666 1.32 christos } else if (cuttimes != loend && *loend == ','
667 1.32 christos && (hi = strtoimax(loend + 1, &hiend, 10),
668 1.32 christos loend + 1 != hiend && !*hiend)) {
669 1.29 christos if (cutlotime < lo) {
670 1.29 christos if (absolute_max_time < lo)
671 1.29 christos lo = absolute_max_time;
672 1.29 christos cutlotime = lo;
673 1.29 christos }
674 1.29 christos if (hi < cuthitime) {
675 1.29 christos if (hi < absolute_min_time)
676 1.29 christos hi = absolute_min_time;
677 1.29 christos cuthitime = hi;
678 1.29 christos }
679 1.29 christos } else {
680 1.29 christos (void) fprintf(stderr,
681 1.29 christos _("%s: wild -t argument %s\n"),
682 1.29 christos progname, cuttimes);
683 1.36 christos return EXIT_FAILURE;
684 1.29 christos }
685 1.29 christos }
686 1.1 jtc }
687 1.36 christos gmtzinit();
688 1.36 christos now = time(NULL);
689 1.1 jtc longest = 0;
690 1.36 christos for (i = optind; i < argc; i++) {
691 1.36 christos size_t arglen = strlen(argv[i]);
692 1.36 christos if (longest < arglen)
693 1.36 christos longest = arglen < INT_MAX ? arglen : INT_MAX;
694 1.36 christos }
695 1.1 jtc
696 1.1 jtc for (i = optind; i < argc; ++i) {
697 1.36 christos timezone_t tz = tzalloc(argv[i]);
698 1.36 christos char const *ab;
699 1.36 christos if (!tz) {
700 1.36 christos errx(EXIT_FAILURE, "%s", argv[i]);
701 1.36 christos }
702 1.29 christos if (! (vflag | Vflag)) {
703 1.36 christos show(tz, argv[i], now, false);
704 1.36 christos tzfree(tz);
705 1.1 jtc continue;
706 1.3 jtc }
707 1.36 christos warned = false;
708 1.17 mlelstv t = absolute_min_time;
709 1.29 christos if (!Vflag) {
710 1.36 christos show(tz, argv[i], t, true);
711 1.31 christos t += SECSPERDAY;
712 1.36 christos show(tz, argv[i], t, true);
713 1.29 christos }
714 1.17 mlelstv if (t < cutlotime)
715 1.17 mlelstv t = cutlotime;
716 1.36 christos tmp = my_localtime_rz(tz, &t, &tm);
717 1.36 christos if (tmp)
718 1.36 christos ab = saveabbr(&abbrev, &abbrevsize, &tm);
719 1.36 christos else
720 1.36 christos ab = NULL;
721 1.36 christos while (t < cuthitime) {
722 1.36 christos newt = ((t < absolute_max_time - SECSPERDAY / 2
723 1.36 christos && t + SECSPERDAY / 2 < cuthitime)
724 1.31 christos ? t + SECSPERDAY / 2
725 1.36 christos : cuthitime);
726 1.36 christos newtmp = localtime_rz(tz, &newt, &newtm);
727 1.17 mlelstv if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
728 1.36 christos (delta(&newtm, &tm) != (newt - t) ||
729 1.36 christos newtm.tm_isdst != tm.tm_isdst ||
730 1.36 christos strcmp(abbr(&newtm), ab) != 0)) {
731 1.36 christos newt = hunt(tz, argv[i], t, newt);
732 1.36 christos newtmp = localtime_rz(tz, &newt, &newtm);
733 1.36 christos if (newtmp)
734 1.36 christos ab = saveabbr(&abbrev, &abbrevsize,
735 1.36 christos &newtm);
736 1.1 jtc }
737 1.1 jtc t = newt;
738 1.1 jtc tm = newtm;
739 1.17 mlelstv tmp = newtmp;
740 1.1 jtc }
741 1.29 christos if (!Vflag) {
742 1.29 christos t = absolute_max_time;
743 1.31 christos t -= SECSPERDAY;
744 1.36 christos show(tz, argv[i], t, true);
745 1.31 christos t += SECSPERDAY;
746 1.36 christos show(tz, argv[i], t, true);
747 1.29 christos }
748 1.36 christos tzfree(tz);
749 1.1 jtc }
750 1.36 christos close_file(stdout);
751 1.36 christos if (errout && (ferror(stderr) || fclose(stderr) != 0))
752 1.36 christos return EXIT_FAILURE;
753 1.36 christos return EXIT_SUCCESS;
754 1.17 mlelstv }
755 1.1 jtc
756 1.1 jtc static time_t
757 1.38 martin yeartot(const intmax_t y)
758 1.17 mlelstv {
759 1.31 christos intmax_t myy, seconds, years;
760 1.29 christos time_t t;
761 1.17 mlelstv
762 1.17 mlelstv myy = EPOCH_YEAR;
763 1.17 mlelstv t = 0;
764 1.31 christos while (myy < y) {
765 1.31 christos if (SECSPER400YEARS_FITS && 400 <= y - myy) {
766 1.31 christos intmax_t diff400 = (y - myy) / 400;
767 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
768 1.31 christos return absolute_max_time;
769 1.31 christos seconds = diff400 * SECSPER400YEARS;
770 1.31 christos years = diff400 * 400;
771 1.31 christos } else {
772 1.17 mlelstv seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
773 1.31 christos years = 1;
774 1.31 christos }
775 1.31 christos myy += years;
776 1.31 christos if (t > absolute_max_time - seconds)
777 1.31 christos return absolute_max_time;
778 1.31 christos t += seconds;
779 1.31 christos }
780 1.31 christos while (y < myy) {
781 1.31 christos if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
782 1.31 christos intmax_t diff400 = (myy - y) / 400;
783 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
784 1.31 christos return absolute_min_time;
785 1.31 christos seconds = diff400 * SECSPER400YEARS;
786 1.31 christos years = diff400 * 400;
787 1.17 mlelstv } else {
788 1.31 christos seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
789 1.31 christos years = 1;
790 1.17 mlelstv }
791 1.31 christos myy -= years;
792 1.31 christos if (t < absolute_min_time + seconds)
793 1.31 christos return absolute_min_time;
794 1.31 christos t -= seconds;
795 1.17 mlelstv }
796 1.17 mlelstv return t;
797 1.17 mlelstv }
798 1.17 mlelstv
799 1.17 mlelstv static time_t
800 1.36 christos hunt(timezone_t tz, char *name, time_t lot, time_t hit)
801 1.17 mlelstv {
802 1.36 christos static char * loab;
803 1.36 christos static size_t loabsize;
804 1.36 christos char const * ab;
805 1.17 mlelstv time_t t;
806 1.17 mlelstv struct tm lotm;
807 1.26 christos struct tm * lotmp;
808 1.17 mlelstv struct tm tm;
809 1.26 christos struct tm * tmp;
810 1.17 mlelstv
811 1.36 christos lotmp = my_localtime_rz(tz, &lot, &lotm);
812 1.36 christos if (lotmp)
813 1.36 christos ab = saveabbr(&loab, &loabsize, &lotm);
814 1.36 christos else
815 1.36 christos ab = NULL;
816 1.17 mlelstv for ( ; ; ) {
817 1.29 christos time_t diff = hit - lot;
818 1.17 mlelstv if (diff < 2)
819 1.17 mlelstv break;
820 1.17 mlelstv t = lot;
821 1.17 mlelstv t += diff / 2;
822 1.1 jtc if (t <= lot)
823 1.1 jtc ++t;
824 1.1 jtc else if (t >= hit)
825 1.1 jtc --t;
826 1.36 christos tmp = my_localtime_rz(tz, &t, &tm);
827 1.17 mlelstv if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
828 1.17 mlelstv (delta(&tm, &lotm) == (t - lot) &&
829 1.1 jtc tm.tm_isdst == lotm.tm_isdst &&
830 1.36 christos strcmp(abbr(&tm), ab) == 0)) {
831 1.1 jtc lot = t;
832 1.1 jtc lotm = tm;
833 1.17 mlelstv lotmp = tmp;
834 1.1 jtc } else hit = t;
835 1.1 jtc }
836 1.36 christos show(tz, name, lot, true);
837 1.36 christos show(tz, name, hit, true);
838 1.1 jtc return hit;
839 1.1 jtc }
840 1.1 jtc
841 1.1 jtc /*
842 1.17 mlelstv ** Thanks to Paul Eggert for logic used in delta.
843 1.1 jtc */
844 1.1 jtc
845 1.29 christos static intmax_t
846 1.26 christos delta(struct tm *newp, struct tm *oldp)
847 1.1 jtc {
848 1.29 christos intmax_t result;
849 1.29 christos int tmy;
850 1.1 jtc
851 1.1 jtc if (newp->tm_year < oldp->tm_year)
852 1.1 jtc return -delta(oldp, newp);
853 1.1 jtc result = 0;
854 1.1 jtc for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
855 1.17 mlelstv result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
856 1.1 jtc result += newp->tm_yday - oldp->tm_yday;
857 1.1 jtc result *= HOURSPERDAY;
858 1.1 jtc result += newp->tm_hour - oldp->tm_hour;
859 1.1 jtc result *= MINSPERHOUR;
860 1.1 jtc result += newp->tm_min - oldp->tm_min;
861 1.1 jtc result *= SECSPERMIN;
862 1.1 jtc result += newp->tm_sec - oldp->tm_sec;
863 1.1 jtc return result;
864 1.1 jtc }
865 1.1 jtc
866 1.36 christos #ifndef TM_GMTOFF
867 1.36 christos /* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday.
868 1.39 christos Assume A and B differ by at most one year. */
869 1.36 christos static int
870 1.36 christos adjusted_yday(struct tm const *a, struct tm const *b)
871 1.36 christos {
872 1.36 christos int yday = a->tm_yday;
873 1.36 christos if (b->tm_year < a->tm_year)
874 1.36 christos yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE);
875 1.36 christos return yday;
876 1.36 christos }
877 1.36 christos #endif
878 1.36 christos
879 1.36 christos /* If A is the broken-down local time and B the broken-down UTC for
880 1.36 christos the same instant, return A's UTC offset in seconds, where positive
881 1.36 christos offsets are east of Greenwich. On failure, return LONG_MIN. */
882 1.36 christos static long
883 1.36 christos gmtoff(struct tm const *a, struct tm const *b)
884 1.36 christos {
885 1.36 christos #ifdef TM_GMTOFF
886 1.36 christos return a->TM_GMTOFF;
887 1.36 christos #else
888 1.36 christos if (! b)
889 1.36 christos return LONG_MIN;
890 1.36 christos else {
891 1.36 christos int ayday = adjusted_yday(a, b);
892 1.36 christos int byday = adjusted_yday(b, a);
893 1.36 christos int days = ayday - byday;
894 1.36 christos long hours = a->tm_hour - b->tm_hour + 24 * days;
895 1.36 christos long minutes = a->tm_min - b->tm_min + 60 * hours;
896 1.36 christos long seconds = a->tm_sec - b->tm_sec + 60 * minutes;
897 1.36 christos return seconds;
898 1.36 christos }
899 1.36 christos #endif
900 1.36 christos }
901 1.36 christos
902 1.1 jtc static void
903 1.36 christos show(timezone_t tz, char *zone, time_t t, bool v)
904 1.1 jtc {
905 1.26 christos struct tm * tmp;
906 1.36 christos struct tm * gmtmp;
907 1.36 christos struct tm tm, gmtm;
908 1.1 jtc
909 1.5 jtc (void) printf("%-*s ", (int) longest, zone);
910 1.1 jtc if (v) {
911 1.36 christos gmtmp = my_gmtime_r(&t, &gmtm);
912 1.36 christos if (gmtmp == NULL) {
913 1.36 christos printf(tformat(), t);
914 1.17 mlelstv } else {
915 1.36 christos dumptime(gmtmp);
916 1.31 christos (void) printf(" UT");
917 1.17 mlelstv }
918 1.17 mlelstv (void) printf(" = ");
919 1.17 mlelstv }
920 1.36 christos tmp = my_localtime_rz(tz, &t, &tm);
921 1.17 mlelstv dumptime(tmp);
922 1.17 mlelstv if (tmp != NULL) {
923 1.17 mlelstv if (*abbr(tmp) != '\0')
924 1.17 mlelstv (void) printf(" %s", abbr(tmp));
925 1.17 mlelstv if (v) {
926 1.36 christos long off = gmtoff(tmp, gmtmp);
927 1.17 mlelstv (void) printf(" isdst=%d", tmp->tm_isdst);
928 1.36 christos if (off != LONG_MIN)
929 1.36 christos (void) printf(" gmtoff=%ld", off);
930 1.17 mlelstv }
931 1.1 jtc }
932 1.1 jtc (void) printf("\n");
933 1.17 mlelstv if (tmp != NULL && *abbr(tmp) != '\0')
934 1.17 mlelstv abbrok(abbr(tmp), zone);
935 1.1 jtc }
936 1.1 jtc
937 1.9 mycroft static const char *
938 1.36 christos abbr(struct tm const *tmp)
939 1.1 jtc {
940 1.36 christos #ifdef TM_ZONE
941 1.36 christos return tmp->TM_ZONE;
942 1.36 christos #else
943 1.36 christos return (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst]
944 1.36 christos ? tzname[0 < tmp->tm_isdst]
945 1.36 christos : "");
946 1.36 christos #endif
947 1.1 jtc }
948 1.17 mlelstv
949 1.17 mlelstv /*
950 1.17 mlelstv ** The code below can fail on certain theoretical systems;
951 1.17 mlelstv ** it works on all known real-world systems as of 2004-12-30.
952 1.17 mlelstv */
953 1.17 mlelstv
954 1.17 mlelstv static const char *
955 1.17 mlelstv tformat(void)
956 1.17 mlelstv {
957 1.17 mlelstv if (0 > (time_t) -1) { /* signed */
958 1.29 christos if (sizeof (time_t) == sizeof (intmax_t))
959 1.29 christos return "%"PRIdMAX;
960 1.17 mlelstv if (sizeof (time_t) > sizeof (long))
961 1.17 mlelstv return "%lld";
962 1.17 mlelstv if (sizeof (time_t) > sizeof (int))
963 1.17 mlelstv return "%ld";
964 1.17 mlelstv return "%d";
965 1.17 mlelstv }
966 1.29 christos #ifdef PRIuMAX
967 1.29 christos if (sizeof (time_t) == sizeof (uintmax_t))
968 1.29 christos return "%"PRIuMAX;
969 1.29 christos #endif
970 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned long))
971 1.17 mlelstv return "%llu";
972 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned int))
973 1.17 mlelstv return "%lu";
974 1.17 mlelstv return "%u";
975 1.17 mlelstv }
976 1.17 mlelstv
977 1.17 mlelstv static void
978 1.26 christos dumptime(const struct tm *timeptr)
979 1.17 mlelstv {
980 1.17 mlelstv static const char wday_name[][3] = {
981 1.17 mlelstv "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
982 1.17 mlelstv };
983 1.17 mlelstv static const char mon_name[][3] = {
984 1.17 mlelstv "Jan", "Feb", "Mar", "Apr", "May", "Jun",
985 1.17 mlelstv "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
986 1.17 mlelstv };
987 1.26 christos const char * wn;
988 1.26 christos const char * mn;
989 1.26 christos int lead;
990 1.26 christos int trail;
991 1.17 mlelstv
992 1.17 mlelstv if (timeptr == NULL) {
993 1.36 christos printf("NULL");
994 1.17 mlelstv return;
995 1.17 mlelstv }
996 1.17 mlelstv /*
997 1.36 christos ** The packaged localtime_rz and gmtime_r never put out-of-range
998 1.17 mlelstv ** values in tm_wday or tm_mon, but since this code might be compiled
999 1.17 mlelstv ** with other (perhaps experimental) versions, paranoia is in order.
1000 1.17 mlelstv */
1001 1.17 mlelstv if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
1002 1.17 mlelstv (int) (sizeof wday_name / sizeof wday_name[0]))
1003 1.17 mlelstv wn = "???";
1004 1.17 mlelstv else wn = wday_name[timeptr->tm_wday];
1005 1.17 mlelstv if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
1006 1.17 mlelstv (int) (sizeof mon_name / sizeof mon_name[0]))
1007 1.17 mlelstv mn = "???";
1008 1.17 mlelstv else mn = mon_name[timeptr->tm_mon];
1009 1.36 christos printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
1010 1.17 mlelstv wn, mn,
1011 1.17 mlelstv timeptr->tm_mday, timeptr->tm_hour,
1012 1.17 mlelstv timeptr->tm_min, timeptr->tm_sec);
1013 1.17 mlelstv #define DIVISOR 10
1014 1.17 mlelstv trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
1015 1.17 mlelstv lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
1016 1.17 mlelstv trail / DIVISOR;
1017 1.17 mlelstv trail %= DIVISOR;
1018 1.17 mlelstv if (trail < 0 && lead > 0) {
1019 1.17 mlelstv trail += DIVISOR;
1020 1.17 mlelstv --lead;
1021 1.17 mlelstv } else if (lead < 0 && trail > 0) {
1022 1.17 mlelstv trail -= DIVISOR;
1023 1.17 mlelstv ++lead;
1024 1.17 mlelstv }
1025 1.17 mlelstv if (lead == 0)
1026 1.36 christos printf("%d", trail);
1027 1.36 christos else printf("%d%d", lead, ((trail < 0) ? -trail : trail));
1028 1.17 mlelstv }
1029