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