zdump.c revision 1.62 1 1.62 christos /* $NetBSD: zdump.c,v 1.62 2023/09/16 18:40:26 christos Exp $ */
2 1.50 christos /* Dump time zone data in a textual format. */
3 1.50 christos
4 1.17 mlelstv /*
5 1.17 mlelstv ** This file is in the public domain, so clarified as of
6 1.17 mlelstv ** 2009-05-17 by Arthur David Olson.
7 1.17 mlelstv */
8 1.2 jtc
9 1.6 christos #include <sys/cdefs.h>
10 1.1 jtc #ifndef lint
11 1.62 christos __RCSID("$NetBSD: zdump.c,v 1.62 2023/09/16 18:40:26 christos Exp $");
12 1.1 jtc #endif /* !defined lint */
13 1.1 jtc
14 1.36 christos #ifndef NETBSD_INSPIRED
15 1.36 christos # define NETBSD_INSPIRED 1
16 1.36 christos #endif
17 1.36 christos
18 1.46 christos #include <err.h>
19 1.29 christos #include "private.h"
20 1.47 christos #include <stdio.h>
21 1.47 christos
22 1.47 christos #ifndef HAVE_SNPRINTF
23 1.62 christos # define HAVE_SNPRINTF (!PORT_TO_C89 || 199901 <= __STDC_VERSION__)
24 1.47 christos #endif
25 1.36 christos
26 1.36 christos #ifndef HAVE_LOCALTIME_R
27 1.36 christos # define HAVE_LOCALTIME_R 1
28 1.36 christos #endif
29 1.36 christos
30 1.36 christos #ifndef HAVE_LOCALTIME_RZ
31 1.36 christos # ifdef TM_ZONE
32 1.36 christos # define HAVE_LOCALTIME_RZ (NETBSD_INSPIRED && USE_LTZ)
33 1.36 christos # else
34 1.36 christos # define HAVE_LOCALTIME_RZ 0
35 1.36 christos # endif
36 1.36 christos #endif
37 1.36 christos
38 1.36 christos #ifndef HAVE_TZSET
39 1.36 christos # define HAVE_TZSET 1
40 1.36 christos #endif
41 1.27 christos
42 1.17 mlelstv #ifndef ZDUMP_LO_YEAR
43 1.57 christos # define ZDUMP_LO_YEAR (-500)
44 1.17 mlelstv #endif /* !defined ZDUMP_LO_YEAR */
45 1.17 mlelstv
46 1.17 mlelstv #ifndef ZDUMP_HI_YEAR
47 1.57 christos # define ZDUMP_HI_YEAR 2500
48 1.17 mlelstv #endif /* !defined ZDUMP_HI_YEAR */
49 1.1 jtc
50 1.17 mlelstv #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
51 1.17 mlelstv #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
52 1.31 christos #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
53 1.31 christos + SECSPERLYEAR * (intmax_t) (100 - 3))
54 1.31 christos
55 1.31 christos /*
56 1.31 christos ** True if SECSPER400YEARS is known to be representable as an
57 1.31 christos ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
58 1.31 christos ** even if SECSPER400YEARS is representable, because when that happens
59 1.31 christos ** the code merely runs a bit more slowly, and this slowness doesn't
60 1.31 christos ** occur on any practical platform.
61 1.31 christos */
62 1.31 christos enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
63 1.17 mlelstv
64 1.17 mlelstv #if HAVE_GETTEXT
65 1.57 christos # include <locale.h> /* for setlocale */
66 1.17 mlelstv #endif /* HAVE_GETTEXT */
67 1.3 jtc
68 1.36 christos #if ! HAVE_LOCALTIME_RZ
69 1.36 christos # undef timezone_t
70 1.36 christos # define timezone_t char **
71 1.36 christos #endif
72 1.36 christos
73 1.43 christos #if !HAVE_POSIX_DECLS
74 1.17 mlelstv extern int getopt(int argc, char * const argv[],
75 1.17 mlelstv const char * options);
76 1.1 jtc extern char * optarg;
77 1.1 jtc extern int optind;
78 1.43 christos #endif
79 1.1 jtc
80 1.29 christos /* The minimum and maximum finite time values. */
81 1.54 christos enum { atime_shift = CHAR_BIT * sizeof(time_t) - 2 };
82 1.29 christos static time_t absolute_min_time =
83 1.31 christos ((time_t) -1 < 0
84 1.41 christos ? (- ((time_t) ~ (time_t) 0 < 0)
85 1.41 christos - (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift)))
86 1.29 christos : 0);
87 1.29 christos static time_t absolute_max_time =
88 1.31 christos ((time_t) -1 < 0
89 1.41 christos ? (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift))
90 1.29 christos : -1);
91 1.5 jtc static size_t longest;
92 1.59 christos static char const *progname;
93 1.36 christos static bool warned;
94 1.36 christos static bool errout;
95 1.17 mlelstv
96 1.36 christos static char const *abbr(struct tm const *);
97 1.61 christos ATTRIBUTE_REPRODUCIBLE static intmax_t delta(struct tm *, struct tm *);
98 1.36 christos static void dumptime(struct tm const *);
99 1.59 christos static time_t hunt(timezone_t, time_t, time_t, bool);
100 1.36 christos static void show(timezone_t, char *, time_t, bool);
101 1.54 christos static void showextrema(timezone_t, char *, time_t, struct tm *, time_t);
102 1.44 christos static void showtrans(char const *, struct tm const *, time_t, char const *,
103 1.44 christos char const *);
104 1.36 christos static const char *tformat(void);
105 1.61 christos ATTRIBUTE_REPRODUCIBLE static time_t yeartot(intmax_t);
106 1.17 mlelstv
107 1.54 christos /* Is C an ASCII digit? */
108 1.54 christos static bool
109 1.54 christos is_digit(char c)
110 1.54 christos {
111 1.54 christos return '0' <= c && c <= '9';
112 1.54 christos }
113 1.42 christos
114 1.34 christos /* Is A an alphabetic character in the C locale? */
115 1.36 christos static bool
116 1.34 christos is_alpha(char a)
117 1.34 christos {
118 1.34 christos switch (a) {
119 1.34 christos default:
120 1.36 christos return false;
121 1.34 christos case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
122 1.34 christos case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
123 1.34 christos case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
124 1.34 christos case 'V': case 'W': case 'X': case 'Y': case 'Z':
125 1.34 christos case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
126 1.34 christos case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
127 1.34 christos case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
128 1.34 christos case 'v': case 'w': case 'x': case 'y': case 'z':
129 1.36 christos return true;
130 1.34 christos }
131 1.34 christos }
132 1.34 christos
133 1.61 christos ATTRIBUTE_NORETURN static void
134 1.59 christos size_overflow(void)
135 1.59 christos {
136 1.59 christos fprintf(stderr, _("%s: size overflow\n"), progname);
137 1.59 christos exit(EXIT_FAILURE);
138 1.59 christos }
139 1.59 christos
140 1.59 christos /* Return A + B, exiting if the result would overflow either ptrdiff_t
141 1.62 christos or size_t. A and B are both nonnegative. */
142 1.61 christos ATTRIBUTE_REPRODUCIBLE static ptrdiff_t
143 1.36 christos sumsize(size_t a, size_t b)
144 1.36 christos {
145 1.59 christos #ifdef ckd_add
146 1.59 christos ptrdiff_t sum;
147 1.62 christos if (!ckd_add(&sum, a, b) && sum <= INDEX_MAX)
148 1.59 christos return sum;
149 1.59 christos #else
150 1.62 christos if (a <= INDEX_MAX && b <= INDEX_MAX - a)
151 1.59 christos return a + b;
152 1.59 christos #endif
153 1.59 christos size_overflow();
154 1.36 christos }
155 1.36 christos
156 1.62 christos /* Return the size of of the string STR, including its trailing NUL.
157 1.62 christos Report an error and exit if this would exceed INDEX_MAX which means
158 1.62 christos pointer subtraction wouldn't work. */
159 1.62 christos static ptrdiff_t
160 1.62 christos xstrsize(char const *str)
161 1.62 christos {
162 1.62 christos size_t len = strlen(str);
163 1.62 christos if (len < INDEX_MAX)
164 1.62 christos return len + 1;
165 1.62 christos size_overflow();
166 1.62 christos }
167 1.62 christos
168 1.44 christos /* Return a pointer to a newly allocated buffer of size SIZE, exiting
169 1.62 christos on failure. SIZE should be positive. */
170 1.61 christos ATTRIBUTE_MALLOC static void *
171 1.62 christos xmalloc(ptrdiff_t size)
172 1.44 christos {
173 1.44 christos void *p = malloc(size);
174 1.44 christos if (!p) {
175 1.54 christos fprintf(stderr, _("%s: Memory exhausted\n"), progname);
176 1.44 christos exit(EXIT_FAILURE);
177 1.44 christos }
178 1.44 christos return p;
179 1.44 christos }
180 1.44 christos
181 1.36 christos #if ! HAVE_TZSET
182 1.36 christos # undef tzset
183 1.36 christos # define tzset zdump_tzset
184 1.36 christos static void tzset(void) { }
185 1.36 christos #endif
186 1.36 christos
187 1.36 christos /* Assume gmtime_r works if localtime_r does.
188 1.36 christos A replacement localtime_r is defined below if needed. */
189 1.36 christos #if ! HAVE_LOCALTIME_R
190 1.36 christos
191 1.36 christos # undef gmtime_r
192 1.36 christos # define gmtime_r zdump_gmtime_r
193 1.36 christos
194 1.36 christos static struct tm *
195 1.36 christos gmtime_r(time_t *tp, struct tm *tmp)
196 1.36 christos {
197 1.36 christos struct tm *r = gmtime(tp);
198 1.36 christos if (r) {
199 1.36 christos *tmp = *r;
200 1.36 christos r = tmp;
201 1.36 christos }
202 1.36 christos return r;
203 1.36 christos }
204 1.36 christos
205 1.36 christos #endif
206 1.36 christos
207 1.36 christos /* Platforms with TM_ZONE don't need tzname, so they can use the
208 1.36 christos faster localtime_rz or localtime_r if available. */
209 1.36 christos
210 1.36 christos #if defined TM_ZONE && HAVE_LOCALTIME_RZ
211 1.36 christos # define USE_LOCALTIME_RZ true
212 1.36 christos #else
213 1.36 christos # define USE_LOCALTIME_RZ false
214 1.36 christos #endif
215 1.36 christos
216 1.36 christos #if ! USE_LOCALTIME_RZ
217 1.36 christos
218 1.36 christos # if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET
219 1.36 christos # undef localtime_r
220 1.36 christos # define localtime_r zdump_localtime_r
221 1.36 christos static struct tm *
222 1.36 christos localtime_r(time_t *tp, struct tm *tmp)
223 1.36 christos {
224 1.36 christos struct tm *r = localtime(tp);
225 1.36 christos if (r) {
226 1.36 christos *tmp = *r;
227 1.36 christos r = tmp;
228 1.36 christos }
229 1.36 christos return r;
230 1.36 christos }
231 1.36 christos # endif
232 1.36 christos
233 1.36 christos # undef localtime_rz
234 1.36 christos # define localtime_rz zdump_localtime_rz
235 1.36 christos static struct tm *
236 1.62 christos localtime_rz(ATTRIBUTE_MAYBE_UNUSED timezone_t rz, time_t *tp, struct tm *tmp)
237 1.36 christos {
238 1.36 christos return localtime_r(tp, tmp);
239 1.36 christos }
240 1.36 christos
241 1.36 christos # ifdef TYPECHECK
242 1.36 christos # undef mktime_z
243 1.36 christos # define mktime_z zdump_mktime_z
244 1.36 christos static time_t
245 1.36 christos mktime_z(timezone_t tz, struct tm *tmp)
246 1.36 christos {
247 1.36 christos return mktime(tmp);
248 1.36 christos }
249 1.36 christos # endif
250 1.36 christos
251 1.36 christos # undef tzalloc
252 1.36 christos # undef tzfree
253 1.36 christos # define tzalloc zdump_tzalloc
254 1.36 christos # define tzfree zdump_tzfree
255 1.36 christos
256 1.36 christos static timezone_t
257 1.36 christos tzalloc(char const *val)
258 1.36 christos {
259 1.58 christos # if HAVE_SETENV
260 1.58 christos if (setenv("TZ", val, 1) != 0) {
261 1.62 christos char const *e = strerror(errno);
262 1.62 christos fprintf(stderr, _("%s: setenv: %s\n"), progname, e);
263 1.58 christos exit(EXIT_FAILURE);
264 1.58 christos }
265 1.58 christos tzset();
266 1.59 christos return &optarg; /* Any valid non-null char ** will do. */
267 1.58 christos # else
268 1.58 christos enum { TZeqlen = 3 };
269 1.58 christos static char const TZeq[TZeqlen] = "TZ=";
270 1.59 christos static ptrdiff_t fakeenv0size;
271 1.58 christos void *freeable = NULL;
272 1.58 christos char **env = fakeenv, **initial_environ;
273 1.62 christos ptrdiff_t valsize = xstrsize(val);
274 1.58 christos if (fakeenv0size < valsize) {
275 1.58 christos char **e = environ, **to;
276 1.59 christos ptrdiff_t initial_nenvptrs = 1; /* Counting the trailing NULL pointer. */
277 1.58 christos
278 1.59 christos while (*e++) {
279 1.59 christos # ifdef ckd_add
280 1.62 christos if (ckd_add(&initial_nenvptrs, initial_nenvptrs, 1)
281 1.62 christos || INDEX_MAX < initial_nenvptrs)
282 1.59 christos size_overflow();
283 1.59 christos # else
284 1.62 christos if (initial_nenvptrs == INDEX_MAX / sizeof *environ))
285 1.59 christos size_overflow();
286 1.59 christos initial_nenvptrs++;
287 1.59 christos # endif
288 1.58 christos fakeenv0size = sumsize(valsize, valsize);
289 1.58 christos fakeenv0size = max(fakeenv0size, 64);
290 1.58 christos freeable = env;
291 1.58 christos fakeenv = env =
292 1.58 christos xmalloc(sumsize(sumsize(sizeof *environ,
293 1.58 christos initial_nenvptrs * sizeof *environ),
294 1.58 christos sumsize(TZeqlen, fakeenv0size)));
295 1.58 christos to = env + 1;
296 1.58 christos for (e = environ; (*to = *e); e++)
297 1.58 christos to += strncmp(*e, TZeq, TZeqlen) != 0;
298 1.58 christos env[0] = memcpy(to + 1, TZeq, TZeqlen);
299 1.58 christos }
300 1.58 christos memcpy(env[0] + TZeqlen, val, valsize);
301 1.58 christos initial_environ = environ;
302 1.58 christos environ = env;
303 1.58 christos tzset();
304 1.58 christos free(freeable);
305 1.58 christos return initial_environ;
306 1.58 christos # endif
307 1.36 christos }
308 1.36 christos
309 1.36 christos static void
310 1.62 christos tzfree(ATTRIBUTE_MAYBE_UNUSED timezone_t initial_environ)
311 1.36 christos {
312 1.58 christos # if !HAVE_SETENV
313 1.58 christos environ = initial_environ;
314 1.58 christos tzset();
315 1.58 christos # endif
316 1.36 christos }
317 1.36 christos #endif /* ! USE_LOCALTIME_RZ */
318 1.36 christos
319 1.48 christos /* A UT time zone, and its initializer. */
320 1.36 christos static timezone_t gmtz;
321 1.36 christos static void
322 1.36 christos gmtzinit(void)
323 1.36 christos {
324 1.54 christos if (USE_LOCALTIME_RZ) {
325 1.54 christos /* Try "GMT" first to find out whether this is one of the rare
326 1.54 christos platforms where time_t counts leap seconds; this works due to
327 1.58 christos the "Zone GMT 0 - GMT" line in the "etcetera" file. If "GMT"
328 1.54 christos fails, fall back on "GMT0" which might be similar due to the
329 1.58 christos "Link GMT GMT0" line in the "backward" file, and which
330 1.54 christos should work on all POSIX platforms. The rest of zdump does not
331 1.54 christos use the "GMT" abbreviation that comes from this setting, so it
332 1.62 christos is OK to use "GMT" here rather than the modern "UTC" which
333 1.54 christos would not work on platforms that omit the "backward" file. */
334 1.54 christos gmtz = tzalloc("GMT");
335 1.54 christos if (!gmtz) {
336 1.54 christos static char const gmt0[] = "GMT0";
337 1.54 christos gmtz = tzalloc(gmt0);
338 1.54 christos if (!gmtz) {
339 1.54 christos err(EXIT_FAILURE, "Cannot create %s", gmt0);
340 1.54 christos }
341 1.54 christos }
342 1.54 christos }
343 1.36 christos }
344 1.36 christos
345 1.48 christos /* Convert *TP to UT, storing the broken-down time into *TMP.
346 1.36 christos Return TMP if successful, NULL otherwise. This is like gmtime_r(TP, TMP),
347 1.36 christos except typically faster if USE_LOCALTIME_RZ. */
348 1.36 christos static struct tm *
349 1.36 christos my_gmtime_r(time_t *tp, struct tm *tmp)
350 1.36 christos {
351 1.36 christos return USE_LOCALTIME_RZ ?
352 1.36 christos localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp);
353 1.36 christos }
354 1.36 christos
355 1.17 mlelstv #ifndef TYPECHECK
356 1.36 christos #define my_localtime_rz localtime_rz
357 1.17 mlelstv #else /* !defined TYPECHECK */
358 1.17 mlelstv static struct tm *
359 1.36 christos my_localtime_rz(timezone_t tz, const time_t *tp, struct tm *tmp)
360 1.17 mlelstv {
361 1.36 christos tmp = localtime_rz(tz, tp, tmp);
362 1.36 christos if (tmp) {
363 1.17 mlelstv struct tm tm;
364 1.26 christos time_t t;
365 1.17 mlelstv
366 1.17 mlelstv tm = *tmp;
367 1.36 christos t = mktime_z(tz, &tm);
368 1.31 christos if (t != *tp) {
369 1.17 mlelstv (void) fflush(stdout);
370 1.17 mlelstv (void) fprintf(stderr, "\n%s: ", progname);
371 1.17 mlelstv (void) fprintf(stderr, tformat(), *tp);
372 1.17 mlelstv (void) fprintf(stderr, " ->");
373 1.17 mlelstv (void) fprintf(stderr, " year=%d", tmp->tm_year);
374 1.17 mlelstv (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
375 1.17 mlelstv (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
376 1.17 mlelstv (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
377 1.17 mlelstv (void) fprintf(stderr, " min=%d", tmp->tm_min);
378 1.17 mlelstv (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
379 1.17 mlelstv (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
380 1.17 mlelstv (void) fprintf(stderr, " -> ");
381 1.17 mlelstv (void) fprintf(stderr, tformat(), t);
382 1.17 mlelstv (void) fprintf(stderr, "\n");
383 1.36 christos errout = true;
384 1.17 mlelstv }
385 1.17 mlelstv }
386 1.17 mlelstv return tmp;
387 1.17 mlelstv }
388 1.17 mlelstv #endif /* !defined TYPECHECK */
389 1.17 mlelstv
390 1.17 mlelstv static void
391 1.26 christos abbrok(const char *const abbrp, const char *const zone)
392 1.17 mlelstv {
393 1.26 christos const char *cp;
394 1.26 christos const char *wp;
395 1.17 mlelstv
396 1.17 mlelstv if (warned)
397 1.17 mlelstv return;
398 1.17 mlelstv cp = abbrp;
399 1.42 christos while (is_alpha(*cp) || is_digit(*cp) || *cp == '-' || *cp == '+')
400 1.17 mlelstv ++cp;
401 1.53 christos if (*cp)
402 1.53 christos wp = _("has characters other than ASCII alphanumerics, '-' or '+'");
403 1.53 christos else if (cp - abbrp < 3)
404 1.42 christos wp = _("has fewer than 3 characters");
405 1.17 mlelstv else if (cp - abbrp > 6)
406 1.42 christos wp = _("has more than 6 characters");
407 1.42 christos else
408 1.17 mlelstv return;
409 1.17 mlelstv (void) fflush(stdout);
410 1.17 mlelstv (void) fprintf(stderr,
411 1.17 mlelstv _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
412 1.17 mlelstv progname, zone, abbrp, wp);
413 1.36 christos warned = errout = true;
414 1.36 christos }
415 1.36 christos
416 1.36 christos /* Return a time zone abbreviation. If the abbreviation needs to be
417 1.36 christos saved, use *BUF (of size *BUFALLOC) to save it, and return the
418 1.62 christos abbreviation in the possibly reallocated *BUF. Otherwise, just
419 1.36 christos return the abbreviation. Get the abbreviation from TMP.
420 1.36 christos Exit on memory allocation failure. */
421 1.36 christos static char const *
422 1.59 christos saveabbr(char **buf, ptrdiff_t *bufalloc, struct tm const *tmp)
423 1.36 christos {
424 1.36 christos char const *ab = abbr(tmp);
425 1.36 christos if (HAVE_LOCALTIME_RZ)
426 1.36 christos return ab;
427 1.36 christos else {
428 1.62 christos ptrdiff_t absize = xstrsize(ab);
429 1.62 christos if (*bufalloc < absize) {
430 1.36 christos free(*buf);
431 1.36 christos
432 1.36 christos /* Make the new buffer at least twice as long as the
433 1.36 christos old, to avoid O(N**2) behavior on repeated calls. */
434 1.62 christos *bufalloc = sumsize(*bufalloc, absize);
435 1.44 christos *buf = xmalloc(*bufalloc);
436 1.36 christos }
437 1.36 christos return strcpy(*buf, ab);
438 1.36 christos }
439 1.36 christos }
440 1.36 christos
441 1.36 christos static void
442 1.36 christos close_file(FILE *stream)
443 1.36 christos {
444 1.36 christos char const *e = (ferror(stream) ? _("I/O error")
445 1.36 christos : fclose(stream) != 0 ? strerror(errno) : NULL);
446 1.36 christos if (e) {
447 1.36 christos errx(EXIT_FAILURE, "%s", e);
448 1.36 christos }
449 1.17 mlelstv }
450 1.17 mlelstv
451 1.24 joerg __dead static void
452 1.26 christos usage(FILE *const stream, const int status)
453 1.17 mlelstv {
454 1.17 mlelstv (void) fprintf(stream,
455 1.50 christos _("%s: usage: %s OPTIONS TIMEZONE ...\n"
456 1.44 christos "Options include:\n"
457 1.44 christos " -c [L,]U Start at year L (default -500), end before year U (default 2500)\n"
458 1.44 christos " -t [L,]U Start at time L, end before time U (in seconds since 1970)\n"
459 1.44 christos " -i List transitions briefly (format is experimental)\n" \
460 1.44 christos " -v List transitions verbosely\n"
461 1.44 christos " -V List transitions a bit less verbosely\n"
462 1.44 christos " --help Output this help\n"
463 1.44 christos " --version Output version info\n"
464 1.29 christos "\n"
465 1.29 christos "Report bugs to %s.\n"),
466 1.44 christos progname, progname, REPORT_BUGS_TO);
467 1.36 christos if (status == EXIT_SUCCESS)
468 1.36 christos close_file(stream);
469 1.17 mlelstv exit(status);
470 1.17 mlelstv }
471 1.1 jtc
472 1.1 jtc int
473 1.26 christos main(int argc, char *argv[])
474 1.26 christos {
475 1.36 christos /* These are static so that they're initially zero. */
476 1.36 christos static char * abbrev;
477 1.59 christos static ptrdiff_t abbrevsize;
478 1.36 christos
479 1.26 christos int i;
480 1.36 christos bool vflag;
481 1.36 christos bool Vflag;
482 1.26 christos char * cutarg;
483 1.29 christos char * cuttimes;
484 1.26 christos time_t cutlotime;
485 1.26 christos time_t cuthitime;
486 1.26 christos time_t now;
487 1.44 christos bool iflag = false;
488 1.1 jtc
489 1.29 christos cutlotime = absolute_min_time;
490 1.29 christos cuthitime = absolute_max_time;
491 1.17 mlelstv #if HAVE_GETTEXT
492 1.17 mlelstv (void) setlocale(LC_ALL, "");
493 1.57 christos # ifdef TZ_DOMAINDIR
494 1.3 jtc (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
495 1.57 christos # endif /* defined TEXTDOMAINDIR */
496 1.3 jtc (void) textdomain(TZ_DOMAIN);
497 1.17 mlelstv #endif /* HAVE_GETTEXT */
498 1.59 christos progname = argv[0] ? argv[0] : "zdump";
499 1.14 kleink for (i = 1; i < argc; ++i)
500 1.14 kleink if (strcmp(argv[i], "--version") == 0) {
501 1.28 christos (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
502 1.36 christos return EXIT_SUCCESS;
503 1.17 mlelstv } else if (strcmp(argv[i], "--help") == 0) {
504 1.22 christos usage(stdout, EXIT_SUCCESS);
505 1.14 kleink }
506 1.36 christos vflag = Vflag = false;
507 1.29 christos cutarg = cuttimes = NULL;
508 1.29 christos for (;;)
509 1.44 christos switch (getopt(argc, argv, "c:it:vV")) {
510 1.29 christos case 'c': cutarg = optarg; break;
511 1.29 christos case 't': cuttimes = optarg; break;
512 1.44 christos case 'i': iflag = true; break;
513 1.36 christos case 'v': vflag = true; break;
514 1.36 christos case 'V': Vflag = true; break;
515 1.29 christos case -1:
516 1.29 christos if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
517 1.29 christos goto arg_processing_done;
518 1.59 christos ATTRIBUTE_FALLTHROUGH;
519 1.29 christos default:
520 1.29 christos usage(stderr, EXIT_FAILURE);
521 1.29 christos }
522 1.29 christos arg_processing_done:;
523 1.29 christos
524 1.44 christos if (iflag | vflag | Vflag) {
525 1.29 christos intmax_t lo;
526 1.29 christos intmax_t hi;
527 1.32 christos char *loend, *hiend;
528 1.30 christos intmax_t cutloyear = ZDUMP_LO_YEAR;
529 1.30 christos intmax_t cuthiyear = ZDUMP_HI_YEAR;
530 1.17 mlelstv if (cutarg != NULL) {
531 1.32 christos lo = strtoimax(cutarg, &loend, 10);
532 1.32 christos if (cutarg != loend && !*loend) {
533 1.32 christos hi = lo;
534 1.32 christos cuthiyear = hi;
535 1.32 christos } else if (cutarg != loend && *loend == ','
536 1.32 christos && (hi = strtoimax(loend + 1, &hiend, 10),
537 1.32 christos loend + 1 != hiend && !*hiend)) {
538 1.32 christos cutloyear = lo;
539 1.17 mlelstv cuthiyear = hi;
540 1.17 mlelstv } else {
541 1.36 christos fprintf(stderr, _("%s: wild -c argument %s\n"),
542 1.17 mlelstv progname, cutarg);
543 1.36 christos return EXIT_FAILURE;
544 1.17 mlelstv }
545 1.17 mlelstv }
546 1.29 christos if (cutarg != NULL || cuttimes == NULL) {
547 1.29 christos cutlotime = yeartot(cutloyear);
548 1.29 christos cuthitime = yeartot(cuthiyear);
549 1.29 christos }
550 1.29 christos if (cuttimes != NULL) {
551 1.32 christos lo = strtoimax(cuttimes, &loend, 10);
552 1.32 christos if (cuttimes != loend && !*loend) {
553 1.32 christos hi = lo;
554 1.29 christos if (hi < cuthitime) {
555 1.54 christos if (hi < absolute_min_time + 1)
556 1.54 christos hi = absolute_min_time + 1;
557 1.29 christos cuthitime = hi;
558 1.29 christos }
559 1.32 christos } else if (cuttimes != loend && *loend == ','
560 1.32 christos && (hi = strtoimax(loend + 1, &hiend, 10),
561 1.32 christos loend + 1 != hiend && !*hiend)) {
562 1.29 christos if (cutlotime < lo) {
563 1.29 christos if (absolute_max_time < lo)
564 1.29 christos lo = absolute_max_time;
565 1.29 christos cutlotime = lo;
566 1.29 christos }
567 1.29 christos if (hi < cuthitime) {
568 1.54 christos if (hi < absolute_min_time + 1)
569 1.54 christos hi = absolute_min_time + 1;
570 1.29 christos cuthitime = hi;
571 1.29 christos }
572 1.29 christos } else {
573 1.29 christos (void) fprintf(stderr,
574 1.29 christos _("%s: wild -t argument %s\n"),
575 1.29 christos progname, cuttimes);
576 1.36 christos return EXIT_FAILURE;
577 1.29 christos }
578 1.29 christos }
579 1.1 jtc }
580 1.36 christos gmtzinit();
581 1.54 christos if (iflag | vflag | Vflag)
582 1.54 christos now = 0;
583 1.54 christos else {
584 1.44 christos now = time(NULL);
585 1.54 christos now |= !now;
586 1.54 christos }
587 1.1 jtc longest = 0;
588 1.36 christos for (i = optind; i < argc; i++) {
589 1.36 christos size_t arglen = strlen(argv[i]);
590 1.36 christos if (longest < arglen)
591 1.57 christos longest = min(arglen, INT_MAX);
592 1.36 christos }
593 1.1 jtc
594 1.1 jtc for (i = optind; i < argc; ++i) {
595 1.36 christos timezone_t tz = tzalloc(argv[i]);
596 1.36 christos char const *ab;
597 1.44 christos time_t t;
598 1.44 christos struct tm tm, newtm;
599 1.44 christos bool tm_ok;
600 1.44 christos
601 1.36 christos if (!tz) {
602 1.62 christos err(EXIT_FAILURE, "%s", argv[i]);
603 1.36 christos }
604 1.54 christos if (now) {
605 1.36 christos show(tz, argv[i], now, false);
606 1.36 christos tzfree(tz);
607 1.1 jtc continue;
608 1.3 jtc }
609 1.36 christos warned = false;
610 1.17 mlelstv t = absolute_min_time;
611 1.44 christos if (! (iflag | Vflag)) {
612 1.36 christos show(tz, argv[i], t, true);
613 1.54 christos if (my_localtime_rz(tz, &t, &tm) == NULL
614 1.54 christos && t < cutlotime) {
615 1.54 christos time_t newt = cutlotime;
616 1.54 christos if (my_localtime_rz(tz, &newt, &newtm) != NULL)
617 1.54 christos showextrema(tz, argv[i], t, NULL, newt);
618 1.54 christos }
619 1.29 christos }
620 1.54 christos if (t + 1 < cutlotime)
621 1.54 christos t = cutlotime - 1;
622 1.44 christos tm_ok = my_localtime_rz(tz, &t, &tm) != NULL;
623 1.44 christos if (tm_ok) {
624 1.36 christos ab = saveabbr(&abbrev, &abbrevsize, &tm);
625 1.44 christos if (iflag) {
626 1.44 christos showtrans("\nTZ=%f", &tm, t, ab, argv[i]);
627 1.44 christos showtrans("-\t-\t%Q", &tm, t, ab, argv[i]);
628 1.44 christos }
629 1.44 christos } else
630 1.36 christos ab = NULL;
631 1.54 christos while (t < cuthitime - 1) {
632 1.44 christos time_t newt = ((t < absolute_max_time - SECSPERDAY / 2
633 1.54 christos && t + SECSPERDAY / 2 < cuthitime - 1)
634 1.54 christos ? t + SECSPERDAY / 2
635 1.54 christos : cuthitime - 1);
636 1.44 christos struct tm *newtmp = localtime_rz(tz, &newt, &newtm);
637 1.44 christos bool newtm_ok = newtmp != NULL;
638 1.48 christos if (tm_ok != newtm_ok
639 1.54 christos || (ab && (delta(&newtm, &tm) != newt - t
640 1.54 christos || newtm.tm_isdst != tm.tm_isdst
641 1.54 christos || strcmp(abbr(&newtm), ab) != 0))) {
642 1.59 christos newt = hunt(tz, t, newt, false);
643 1.36 christos newtmp = localtime_rz(tz, &newt, &newtm);
644 1.44 christos newtm_ok = newtmp != NULL;
645 1.44 christos if (iflag)
646 1.44 christos showtrans("%Y-%m-%d\t%L\t%Q",
647 1.44 christos newtmp, newt, newtm_ok ?
648 1.44 christos abbr(&newtm) : NULL, argv[i]);
649 1.44 christos else {
650 1.44 christos show(tz, argv[i], newt - 1, true);
651 1.44 christos show(tz, argv[i], newt, true);
652 1.44 christos }
653 1.1 jtc }
654 1.1 jtc t = newt;
655 1.44 christos tm_ok = newtm_ok;
656 1.44 christos if (newtm_ok) {
657 1.44 christos ab = saveabbr(&abbrev, &abbrevsize, &newtm);
658 1.44 christos tm = newtm;
659 1.44 christos }
660 1.1 jtc }
661 1.44 christos if (! (iflag | Vflag)) {
662 1.54 christos time_t newt = absolute_max_time;
663 1.54 christos t = cuthitime;
664 1.54 christos if (t < newt) {
665 1.54 christos struct tm *tmp = my_localtime_rz(tz, &t, &tm);
666 1.54 christos if (tmp != NULL
667 1.54 christos && my_localtime_rz(tz, &newt, &newtm) == NULL)
668 1.54 christos showextrema(tz, argv[i], t, tmp, newt);
669 1.54 christos }
670 1.54 christos show(tz, argv[i], absolute_max_time, true);
671 1.29 christos }
672 1.36 christos tzfree(tz);
673 1.1 jtc }
674 1.36 christos close_file(stdout);
675 1.36 christos if (errout && (ferror(stderr) || fclose(stderr) != 0))
676 1.36 christos return EXIT_FAILURE;
677 1.36 christos return EXIT_SUCCESS;
678 1.17 mlelstv }
679 1.1 jtc
680 1.1 jtc static time_t
681 1.42 christos yeartot(intmax_t y)
682 1.17 mlelstv {
683 1.31 christos intmax_t myy, seconds, years;
684 1.29 christos time_t t;
685 1.17 mlelstv
686 1.17 mlelstv myy = EPOCH_YEAR;
687 1.17 mlelstv t = 0;
688 1.31 christos while (myy < y) {
689 1.31 christos if (SECSPER400YEARS_FITS && 400 <= y - myy) {
690 1.31 christos intmax_t diff400 = (y - myy) / 400;
691 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
692 1.31 christos return absolute_max_time;
693 1.31 christos seconds = diff400 * SECSPER400YEARS;
694 1.31 christos years = diff400 * 400;
695 1.31 christos } else {
696 1.17 mlelstv seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
697 1.31 christos years = 1;
698 1.31 christos }
699 1.31 christos myy += years;
700 1.31 christos if (t > absolute_max_time - seconds)
701 1.31 christos return absolute_max_time;
702 1.31 christos t += seconds;
703 1.31 christos }
704 1.31 christos while (y < myy) {
705 1.31 christos if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
706 1.31 christos intmax_t diff400 = (myy - y) / 400;
707 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
708 1.31 christos return absolute_min_time;
709 1.31 christos seconds = diff400 * SECSPER400YEARS;
710 1.31 christos years = diff400 * 400;
711 1.17 mlelstv } else {
712 1.31 christos seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
713 1.31 christos years = 1;
714 1.17 mlelstv }
715 1.31 christos myy -= years;
716 1.31 christos if (t < absolute_min_time + seconds)
717 1.31 christos return absolute_min_time;
718 1.31 christos t -= seconds;
719 1.17 mlelstv }
720 1.17 mlelstv return t;
721 1.17 mlelstv }
722 1.17 mlelstv
723 1.59 christos /* Search for a discontinuity in timezone TZ, in the
724 1.54 christos timestamps ranging from LOT through HIT. LOT and HIT disagree
725 1.54 christos about some aspect of timezone. If ONLY_OK, search only for
726 1.54 christos definedness changes, i.e., localtime succeeds on one side of the
727 1.54 christos transition but fails on the other side. Return the timestamp just
728 1.54 christos before the transition from LOT's settings. */
729 1.54 christos
730 1.17 mlelstv static time_t
731 1.59 christos hunt(timezone_t tz, time_t lot, time_t hit, bool only_ok)
732 1.17 mlelstv {
733 1.36 christos static char * loab;
734 1.59 christos static ptrdiff_t loabsize;
735 1.17 mlelstv struct tm lotm;
736 1.17 mlelstv struct tm tm;
737 1.54 christos
738 1.54 christos /* Convert LOT into a broken-down time here, even though our
739 1.54 christos caller already did that. On platforms without TM_ZONE,
740 1.54 christos tzname may have been altered since our caller broke down
741 1.54 christos LOT, and tzname needs to be changed back. */
742 1.44 christos bool lotm_ok = my_localtime_rz(tz, &lot, &lotm) != NULL;
743 1.44 christos bool tm_ok;
744 1.54 christos char const *ab = lotm_ok ? saveabbr(&loab, &loabsize, &lotm) : NULL;
745 1.17 mlelstv
746 1.17 mlelstv for ( ; ; ) {
747 1.54 christos /* T = average of LOT and HIT, rounding down.
748 1.62 christos Avoid overflow. */
749 1.62 christos int rem_sum = lot % 2 + hit % 2;
750 1.62 christos time_t t = (rem_sum == 2) - (rem_sum < 0) + lot / 2 + hit / 2;
751 1.54 christos if (t == lot)
752 1.17 mlelstv break;
753 1.44 christos tm_ok = my_localtime_rz(tz, &t, &tm) != NULL;
754 1.54 christos if (lotm_ok == tm_ok
755 1.54 christos && (only_ok
756 1.54 christos || (ab && tm.tm_isdst == lotm.tm_isdst
757 1.54 christos && delta(&tm, &lotm) == t - lot
758 1.54 christos && strcmp(abbr(&tm), ab) == 0))) {
759 1.44 christos lot = t;
760 1.44 christos if (tm_ok)
761 1.44 christos lotm = tm;
762 1.1 jtc } else hit = t;
763 1.1 jtc }
764 1.1 jtc return hit;
765 1.1 jtc }
766 1.1 jtc
767 1.1 jtc /*
768 1.47 christos ** Thanks to Paul Eggert for logic used in delta_nonneg.
769 1.1 jtc */
770 1.1 jtc
771 1.29 christos static intmax_t
772 1.47 christos delta_nonneg(struct tm *newp, struct tm *oldp)
773 1.1 jtc {
774 1.54 christos intmax_t oldy = oldp->tm_year;
775 1.54 christos int cycles = (newp->tm_year - oldy) / YEARSPERREPEAT;
776 1.54 christos intmax_t sec = SECSPERREPEAT, result = cycles * sec;
777 1.54 christos int tmy = oldp->tm_year + cycles * YEARSPERREPEAT;
778 1.54 christos for ( ; tmy < newp->tm_year; ++tmy)
779 1.17 mlelstv result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
780 1.1 jtc result += newp->tm_yday - oldp->tm_yday;
781 1.1 jtc result *= HOURSPERDAY;
782 1.1 jtc result += newp->tm_hour - oldp->tm_hour;
783 1.1 jtc result *= MINSPERHOUR;
784 1.1 jtc result += newp->tm_min - oldp->tm_min;
785 1.1 jtc result *= SECSPERMIN;
786 1.1 jtc result += newp->tm_sec - oldp->tm_sec;
787 1.1 jtc return result;
788 1.1 jtc }
789 1.1 jtc
790 1.47 christos static intmax_t
791 1.47 christos delta(struct tm *newp, struct tm *oldp)
792 1.47 christos {
793 1.47 christos return (newp->tm_year < oldp->tm_year
794 1.47 christos ? -delta_nonneg(oldp, newp)
795 1.47 christos : delta_nonneg(newp, oldp));
796 1.47 christos }
797 1.47 christos
798 1.36 christos #ifndef TM_GMTOFF
799 1.36 christos /* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday.
800 1.39 christos Assume A and B differ by at most one year. */
801 1.36 christos static int
802 1.36 christos adjusted_yday(struct tm const *a, struct tm const *b)
803 1.36 christos {
804 1.36 christos int yday = a->tm_yday;
805 1.36 christos if (b->tm_year < a->tm_year)
806 1.36 christos yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE);
807 1.36 christos return yday;
808 1.36 christos }
809 1.36 christos #endif
810 1.36 christos
811 1.48 christos /* If A is the broken-down local time and B the broken-down UT for
812 1.48 christos the same instant, return A's UT offset in seconds, where positive
813 1.44 christos offsets are east of Greenwich. On failure, return LONG_MIN.
814 1.44 christos
815 1.46 christos If T is nonnull, *T is the timestamp that corresponds to A; call
816 1.44 christos my_gmtime_r and use its result instead of B. Otherwise, B is the
817 1.44 christos possibly nonnull result of an earlier call to my_gmtime_r. */
818 1.36 christos static long
819 1.59 christos gmtoff(struct tm const *a, ATTRIBUTE_MAYBE_UNUSED time_t *t,
820 1.59 christos ATTRIBUTE_MAYBE_UNUSED struct tm const *b)
821 1.36 christos {
822 1.36 christos #ifdef TM_GMTOFF
823 1.36 christos return a->TM_GMTOFF;
824 1.36 christos #else
825 1.44 christos struct tm tm;
826 1.44 christos if (t)
827 1.44 christos b = my_gmtime_r(t, &tm);
828 1.36 christos if (! b)
829 1.36 christos return LONG_MIN;
830 1.36 christos else {
831 1.36 christos int ayday = adjusted_yday(a, b);
832 1.36 christos int byday = adjusted_yday(b, a);
833 1.36 christos int days = ayday - byday;
834 1.36 christos long hours = a->tm_hour - b->tm_hour + 24 * days;
835 1.36 christos long minutes = a->tm_min - b->tm_min + 60 * hours;
836 1.36 christos long seconds = a->tm_sec - b->tm_sec + 60 * minutes;
837 1.36 christos return seconds;
838 1.36 christos }
839 1.36 christos #endif
840 1.36 christos }
841 1.36 christos
842 1.1 jtc static void
843 1.36 christos show(timezone_t tz, char *zone, time_t t, bool v)
844 1.1 jtc {
845 1.26 christos struct tm * tmp;
846 1.36 christos struct tm * gmtmp;
847 1.36 christos struct tm tm, gmtm;
848 1.1 jtc
849 1.5 jtc (void) printf("%-*s ", (int) longest, zone);
850 1.1 jtc if (v) {
851 1.36 christos gmtmp = my_gmtime_r(&t, &gmtm);
852 1.36 christos if (gmtmp == NULL) {
853 1.36 christos printf(tformat(), t);
854 1.56 christos printf(_(" (gmtime failed)"));
855 1.17 mlelstv } else {
856 1.36 christos dumptime(gmtmp);
857 1.31 christos (void) printf(" UT");
858 1.17 mlelstv }
859 1.17 mlelstv (void) printf(" = ");
860 1.17 mlelstv }
861 1.36 christos tmp = my_localtime_rz(tz, &t, &tm);
862 1.56 christos if (tmp == NULL) {
863 1.56 christos printf(tformat(), t);
864 1.56 christos printf(_(" (localtime failed)"));
865 1.56 christos } else {
866 1.56 christos dumptime(tmp);
867 1.17 mlelstv if (*abbr(tmp) != '\0')
868 1.17 mlelstv (void) printf(" %s", abbr(tmp));
869 1.17 mlelstv if (v) {
870 1.44 christos long off = gmtoff(tmp, NULL, gmtmp);
871 1.17 mlelstv (void) printf(" isdst=%d", tmp->tm_isdst);
872 1.36 christos if (off != LONG_MIN)
873 1.36 christos (void) printf(" gmtoff=%ld", off);
874 1.17 mlelstv }
875 1.1 jtc }
876 1.1 jtc (void) printf("\n");
877 1.17 mlelstv if (tmp != NULL && *abbr(tmp) != '\0')
878 1.17 mlelstv abbrok(abbr(tmp), zone);
879 1.1 jtc }
880 1.1 jtc
881 1.54 christos /* Show timestamps just before and just after a transition between
882 1.54 christos defined and undefined (or vice versa) in either localtime or
883 1.54 christos gmtime. These transitions are for timezone TZ with name ZONE, in
884 1.54 christos the range from LO (with broken-down time LOTMP if that is nonnull)
885 1.54 christos through HI. LO and HI disagree on definedness. */
886 1.54 christos
887 1.54 christos static void
888 1.54 christos showextrema(timezone_t tz, char *zone, time_t lo, struct tm *lotmp, time_t hi)
889 1.54 christos {
890 1.54 christos struct tm localtm[2], gmtm[2];
891 1.59 christos time_t t, boundary = hunt(tz, lo, hi, true);
892 1.54 christos bool old = false;
893 1.54 christos hi = (SECSPERDAY < hi - boundary
894 1.54 christos ? boundary + SECSPERDAY
895 1.54 christos : hi + (hi < TIME_T_MAX));
896 1.54 christos if (SECSPERDAY < boundary - lo) {
897 1.54 christos lo = boundary - SECSPERDAY;
898 1.54 christos lotmp = my_localtime_rz(tz, &lo, &localtm[old]);
899 1.54 christos }
900 1.54 christos if (lotmp)
901 1.54 christos localtm[old] = *lotmp;
902 1.54 christos else
903 1.54 christos localtm[old].tm_sec = -1;
904 1.54 christos if (! my_gmtime_r(&lo, &gmtm[old]))
905 1.54 christos gmtm[old].tm_sec = -1;
906 1.54 christos
907 1.54 christos /* Search sequentially for definedness transitions. Although this
908 1.54 christos could be sped up by refining 'hunt' to search for either
909 1.54 christos localtime or gmtime definedness transitions, it hardly seems
910 1.54 christos worth the trouble. */
911 1.54 christos for (t = lo + 1; t < hi; t++) {
912 1.54 christos bool new = !old;
913 1.54 christos if (! my_localtime_rz(tz, &t, &localtm[new]))
914 1.54 christos localtm[new].tm_sec = -1;
915 1.54 christos if (! my_gmtime_r(&t, &gmtm[new]))
916 1.54 christos gmtm[new].tm_sec = -1;
917 1.54 christos if (((localtm[old].tm_sec < 0) != (localtm[new].tm_sec < 0))
918 1.54 christos | ((gmtm[old].tm_sec < 0) != (gmtm[new].tm_sec < 0))) {
919 1.54 christos show(tz, zone, t - 1, true);
920 1.54 christos show(tz, zone, t, true);
921 1.54 christos }
922 1.54 christos old = new;
923 1.54 christos }
924 1.54 christos }
925 1.54 christos
926 1.48 christos #if HAVE_SNPRINTF
927 1.48 christos # define my_snprintf snprintf
928 1.48 christos #else
929 1.47 christos # include <stdarg.h>
930 1.47 christos
931 1.47 christos /* A substitute for snprintf that is good enough for zdump. */
932 1.61 christos ATTRIBUTE_FORMAT((printf, 3, 4)) static int
933 1.48 christos my_snprintf(char *s, size_t size, char const *format, ...)
934 1.47 christos {
935 1.47 christos int n;
936 1.47 christos va_list args;
937 1.47 christos char const *arg;
938 1.47 christos size_t arglen, slen;
939 1.47 christos char buf[1024];
940 1.47 christos va_start(args, format);
941 1.47 christos if (strcmp(format, "%s") == 0) {
942 1.47 christos arg = va_arg(args, char const *);
943 1.47 christos arglen = strlen(arg);
944 1.47 christos } else {
945 1.47 christos n = vsprintf(buf, format, args);
946 1.51 christos if (n < 0) {
947 1.51 christos va_end(args);
948 1.47 christos return n;
949 1.51 christos }
950 1.47 christos arg = buf;
951 1.47 christos arglen = n;
952 1.47 christos }
953 1.47 christos slen = arglen < size ? arglen : size - 1;
954 1.47 christos memcpy(s, arg, slen);
955 1.47 christos s[slen] = '\0';
956 1.47 christos n = arglen <= INT_MAX ? arglen : -1;
957 1.47 christos va_end(args);
958 1.47 christos return n;
959 1.47 christos }
960 1.47 christos #endif
961 1.47 christos
962 1.44 christos /* Store into BUF, of size SIZE, a formatted local time taken from *TM.
963 1.44 christos Use ISO 8601 format +HH:MM:SS. Omit :SS if SS is zero, and omit
964 1.44 christos :MM too if MM is also zero.
965 1.44 christos
966 1.44 christos Return the length of the resulting string. If the string does not
967 1.44 christos fit, return the length that the string would have been if it had
968 1.44 christos fit; do not overrun the output buffer. */
969 1.44 christos static int
970 1.59 christos format_local_time(char *buf, ptrdiff_t size, struct tm const *tm)
971 1.44 christos {
972 1.44 christos int ss = tm->tm_sec, mm = tm->tm_min, hh = tm->tm_hour;
973 1.44 christos return (ss
974 1.48 christos ? my_snprintf(buf, size, "%02d:%02d:%02d", hh, mm, ss)
975 1.44 christos : mm
976 1.48 christos ? my_snprintf(buf, size, "%02d:%02d", hh, mm)
977 1.48 christos : my_snprintf(buf, size, "%02d", hh));
978 1.44 christos }
979 1.44 christos
980 1.48 christos /* Store into BUF, of size SIZE, a formatted UT offset for the
981 1.44 christos localtime *TM corresponding to time T. Use ISO 8601 format
982 1.46 christos +HHMMSS, or -HHMMSS for timestamps west of Greenwich; use the
983 1.48 christos format -00 for unknown UT offsets. If the hour needs more than
984 1.46 christos two digits to represent, extend the length of HH as needed.
985 1.46 christos Otherwise, omit SS if SS is zero, and omit MM too if MM is also
986 1.46 christos zero.
987 1.44 christos
988 1.44 christos Return the length of the resulting string, or -1 if the result is
989 1.44 christos not representable as a string. If the string does not fit, return
990 1.44 christos the length that the string would have been if it had fit; do not
991 1.44 christos overrun the output buffer. */
992 1.44 christos static int
993 1.59 christos format_utc_offset(char *buf, ptrdiff_t size, struct tm const *tm, time_t t)
994 1.44 christos {
995 1.44 christos long off = gmtoff(tm, &t, NULL);
996 1.44 christos char sign = ((off < 0
997 1.44 christos || (off == 0
998 1.44 christos && (*abbr(tm) == '-' || strcmp(abbr(tm), "zzz") == 0)))
999 1.44 christos ? '-' : '+');
1000 1.44 christos long hh;
1001 1.44 christos int mm, ss;
1002 1.44 christos if (off < 0)
1003 1.44 christos {
1004 1.44 christos if (off == LONG_MIN)
1005 1.44 christos return -1;
1006 1.44 christos off = -off;
1007 1.44 christos }
1008 1.44 christos ss = off % 60;
1009 1.44 christos mm = off / 60 % 60;
1010 1.44 christos hh = off / 60 / 60;
1011 1.46 christos return (ss || 100 <= hh
1012 1.48 christos ? my_snprintf(buf, size, "%c%02ld%02d%02d", sign, hh, mm, ss)
1013 1.44 christos : mm
1014 1.48 christos ? my_snprintf(buf, size, "%c%02ld%02d", sign, hh, mm)
1015 1.48 christos : my_snprintf(buf, size, "%c%02ld", sign, hh));
1016 1.44 christos }
1017 1.44 christos
1018 1.44 christos /* Store into BUF (of size SIZE) a quoted string representation of P.
1019 1.44 christos If the representation's length is less than SIZE, return the
1020 1.44 christos length; the representation is not null terminated. Otherwise
1021 1.44 christos return SIZE, to indicate that BUF is too small. */
1022 1.59 christos static ptrdiff_t
1023 1.59 christos format_quoted_string(char *buf, ptrdiff_t size, char const *p)
1024 1.44 christos {
1025 1.44 christos char *b = buf;
1026 1.59 christos ptrdiff_t s = size;
1027 1.44 christos if (!s)
1028 1.44 christos return size;
1029 1.44 christos *b++ = '"', s--;
1030 1.44 christos for (;;) {
1031 1.44 christos char c = *p++;
1032 1.44 christos if (s <= 1)
1033 1.44 christos return size;
1034 1.44 christos switch (c) {
1035 1.44 christos default: *b++ = c, s--; continue;
1036 1.44 christos case '\0': *b++ = '"', s--; return size - s;
1037 1.44 christos case '"': case '\\': break;
1038 1.44 christos case ' ': c = 's'; break;
1039 1.44 christos case '\f': c = 'f'; break;
1040 1.44 christos case '\n': c = 'n'; break;
1041 1.44 christos case '\r': c = 'r'; break;
1042 1.44 christos case '\t': c = 't'; break;
1043 1.44 christos case '\v': c = 'v'; break;
1044 1.44 christos }
1045 1.44 christos *b++ = '\\', *b++ = c, s -= 2;
1046 1.44 christos }
1047 1.44 christos }
1048 1.44 christos
1049 1.46 christos /* Store into BUF (of size SIZE) a timestamp formatted by TIME_FMT.
1050 1.44 christos TM is the broken-down time, T the seconds count, AB the time zone
1051 1.44 christos abbreviation, and ZONE_NAME the zone name. Return true if
1052 1.44 christos successful, false if the output would require more than SIZE bytes.
1053 1.44 christos TIME_FMT uses the same format that strftime uses, with these
1054 1.44 christos additions:
1055 1.44 christos
1056 1.44 christos %f zone name
1057 1.44 christos %L local time as per format_local_time
1058 1.48 christos %Q like "U\t%Z\tD" where U is the UT offset as for format_utc_offset
1059 1.44 christos and D is the isdst flag; except omit D if it is zero, omit %Z if
1060 1.44 christos it equals U, quote and escape %Z if it contains nonalphabetics,
1061 1.44 christos and omit any trailing tabs. */
1062 1.44 christos
1063 1.44 christos static bool
1064 1.59 christos istrftime(char *buf, ptrdiff_t size, char const *time_fmt,
1065 1.44 christos struct tm const *tm, time_t t, char const *ab, char const *zone_name)
1066 1.44 christos {
1067 1.44 christos char *b = buf;
1068 1.59 christos ptrdiff_t s = size;
1069 1.44 christos char const *f = time_fmt, *p;
1070 1.44 christos
1071 1.44 christos for (p = f; ; p++)
1072 1.44 christos if (*p == '%' && p[1] == '%')
1073 1.44 christos p++;
1074 1.44 christos else if (!*p
1075 1.44 christos || (*p == '%'
1076 1.44 christos && (p[1] == 'f' || p[1] == 'L' || p[1] == 'Q'))) {
1077 1.59 christos ptrdiff_t formatted_len;
1078 1.59 christos ptrdiff_t f_prefix_len = p - f;
1079 1.59 christos ptrdiff_t f_prefix_copy_size = sumsize(f_prefix_len, 2);
1080 1.44 christos char fbuf[100];
1081 1.59 christos bool oversized = (ptrdiff_t)sizeof fbuf <= f_prefix_copy_size;
1082 1.44 christos char *f_prefix_copy = oversized ? xmalloc(f_prefix_copy_size) : fbuf;
1083 1.44 christos memcpy(f_prefix_copy, f, f_prefix_len);
1084 1.44 christos strcpy(f_prefix_copy + f_prefix_len, "X");
1085 1.44 christos formatted_len = strftime(b, s, f_prefix_copy, tm);
1086 1.44 christos if (oversized)
1087 1.44 christos free(f_prefix_copy);
1088 1.44 christos if (formatted_len == 0)
1089 1.44 christos return false;
1090 1.44 christos formatted_len--;
1091 1.44 christos b += formatted_len, s -= formatted_len;
1092 1.44 christos if (!*p++)
1093 1.44 christos break;
1094 1.44 christos switch (*p) {
1095 1.44 christos case 'f':
1096 1.44 christos formatted_len = format_quoted_string(b, s, zone_name);
1097 1.44 christos break;
1098 1.44 christos case 'L':
1099 1.44 christos formatted_len = format_local_time(b, s, tm);
1100 1.44 christos break;
1101 1.44 christos case 'Q':
1102 1.44 christos {
1103 1.44 christos bool show_abbr;
1104 1.44 christos int offlen = format_utc_offset(b, s, tm, t);
1105 1.59 christos if (! (0 <= offlen && offlen < s))
1106 1.44 christos return false;
1107 1.44 christos show_abbr = strcmp(b, ab) != 0;
1108 1.44 christos b += offlen, s -= offlen;
1109 1.44 christos if (show_abbr) {
1110 1.44 christos char const *abp;
1111 1.59 christos ptrdiff_t len;
1112 1.44 christos if (s <= 1)
1113 1.44 christos return false;
1114 1.44 christos *b++ = '\t', s--;
1115 1.44 christos for (abp = ab; is_alpha(*abp); abp++)
1116 1.44 christos continue;
1117 1.44 christos len = (!*abp && *ab
1118 1.59 christos ? my_snprintf(b, s, "%s", ab)
1119 1.44 christos : format_quoted_string(b, s, ab));
1120 1.44 christos if (s <= len)
1121 1.44 christos return false;
1122 1.44 christos b += len, s -= len;
1123 1.44 christos }
1124 1.48 christos formatted_len
1125 1.48 christos = (tm->tm_isdst
1126 1.48 christos ? my_snprintf(b, s, &"\t\t%d"[show_abbr], tm->tm_isdst)
1127 1.48 christos : 0);
1128 1.44 christos }
1129 1.44 christos break;
1130 1.44 christos }
1131 1.46 christos if (s <= formatted_len)
1132 1.44 christos return false;
1133 1.44 christos b += formatted_len, s -= formatted_len;
1134 1.44 christos f = p + 1;
1135 1.44 christos }
1136 1.44 christos *b = '\0';
1137 1.44 christos return true;
1138 1.44 christos }
1139 1.44 christos
1140 1.44 christos /* Show a time transition. */
1141 1.44 christos static void
1142 1.44 christos showtrans(char const *time_fmt, struct tm const *tm, time_t t, char const *ab,
1143 1.44 christos char const *zone_name)
1144 1.44 christos {
1145 1.44 christos if (!tm) {
1146 1.44 christos printf(tformat(), t);
1147 1.44 christos putchar('\n');
1148 1.44 christos } else {
1149 1.44 christos char stackbuf[1000];
1150 1.59 christos ptrdiff_t size = sizeof stackbuf;
1151 1.44 christos char *buf = stackbuf;
1152 1.44 christos char *bufalloc = NULL;
1153 1.44 christos while (! istrftime(buf, size, time_fmt, tm, t, ab, zone_name)) {
1154 1.44 christos size = sumsize(size, size);
1155 1.44 christos free(bufalloc);
1156 1.44 christos buf = bufalloc = xmalloc(size);
1157 1.44 christos }
1158 1.44 christos puts(buf);
1159 1.44 christos free(bufalloc);
1160 1.44 christos }
1161 1.44 christos }
1162 1.44 christos
1163 1.9 mycroft static const char *
1164 1.36 christos abbr(struct tm const *tmp)
1165 1.1 jtc {
1166 1.36 christos #ifdef TM_ZONE
1167 1.36 christos return tmp->TM_ZONE;
1168 1.36 christos #else
1169 1.47 christos # if HAVE_TZNAME
1170 1.47 christos if (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst])
1171 1.47 christos return tzname[0 < tmp->tm_isdst];
1172 1.47 christos # endif
1173 1.47 christos return "";
1174 1.36 christos #endif
1175 1.1 jtc }
1176 1.17 mlelstv
1177 1.17 mlelstv /*
1178 1.17 mlelstv ** The code below can fail on certain theoretical systems;
1179 1.56 christos ** it works on all known real-world systems as of 2022-01-25.
1180 1.17 mlelstv */
1181 1.17 mlelstv
1182 1.17 mlelstv static const char *
1183 1.17 mlelstv tformat(void)
1184 1.17 mlelstv {
1185 1.62 christos #if HAVE__GENERIC
1186 1.56 christos /* C11-style _Generic is more likely to return the correct
1187 1.56 christos format when distinct types have the same size. */
1188 1.56 christos char const *fmt =
1189 1.56 christos _Generic(+ (time_t) 0,
1190 1.56 christos int: "%d", long: "%ld", long long: "%lld",
1191 1.56 christos unsigned: "%u", unsigned long: "%lu",
1192 1.56 christos unsigned long long: "%llu",
1193 1.56 christos default: NULL);
1194 1.56 christos if (fmt)
1195 1.56 christos return fmt;
1196 1.56 christos fmt = _Generic((time_t) 0,
1197 1.56 christos intmax_t: "%"PRIdMAX, uintmax_t: "%"PRIuMAX,
1198 1.56 christos default: NULL);
1199 1.56 christos if (fmt)
1200 1.56 christos return fmt;
1201 1.56 christos #endif
1202 1.17 mlelstv if (0 > (time_t) -1) { /* signed */
1203 1.54 christos if (sizeof(time_t) == sizeof(intmax_t))
1204 1.29 christos return "%"PRIdMAX;
1205 1.54 christos if (sizeof(time_t) > sizeof(long))
1206 1.17 mlelstv return "%lld";
1207 1.54 christos if (sizeof(time_t) > sizeof(int))
1208 1.17 mlelstv return "%ld";
1209 1.17 mlelstv return "%d";
1210 1.17 mlelstv }
1211 1.29 christos #ifdef PRIuMAX
1212 1.54 christos if (sizeof(time_t) == sizeof(uintmax_t))
1213 1.29 christos return "%"PRIuMAX;
1214 1.29 christos #endif
1215 1.54 christos if (sizeof(time_t) > sizeof(unsigned long))
1216 1.17 mlelstv return "%llu";
1217 1.54 christos if (sizeof(time_t) > sizeof(unsigned int))
1218 1.17 mlelstv return "%lu";
1219 1.17 mlelstv return "%u";
1220 1.17 mlelstv }
1221 1.17 mlelstv
1222 1.17 mlelstv static void
1223 1.26 christos dumptime(const struct tm *timeptr)
1224 1.17 mlelstv {
1225 1.47 christos static const char wday_name[][4] = {
1226 1.17 mlelstv "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1227 1.17 mlelstv };
1228 1.47 christos static const char mon_name[][4] = {
1229 1.17 mlelstv "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1230 1.17 mlelstv "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1231 1.17 mlelstv };
1232 1.26 christos int lead;
1233 1.26 christos int trail;
1234 1.57 christos int DIVISOR = 10;
1235 1.17 mlelstv
1236 1.17 mlelstv /*
1237 1.36 christos ** The packaged localtime_rz and gmtime_r never put out-of-range
1238 1.17 mlelstv ** values in tm_wday or tm_mon, but since this code might be compiled
1239 1.17 mlelstv ** with other (perhaps experimental) versions, paranoia is in order.
1240 1.17 mlelstv */
1241 1.47 christos printf("%s %s%3d %.2d:%.2d:%.2d ",
1242 1.54 christos ((0 <= timeptr->tm_wday
1243 1.55 ryoon && timeptr->tm_wday < (int) (sizeof wday_name / sizeof wday_name[0]))
1244 1.54 christos ? wday_name[timeptr->tm_wday] : "???"),
1245 1.54 christos ((0 <= timeptr->tm_mon
1246 1.55 ryoon && timeptr->tm_mon < (int) (sizeof mon_name / sizeof mon_name[0]))
1247 1.54 christos ? mon_name[timeptr->tm_mon] : "???"),
1248 1.17 mlelstv timeptr->tm_mday, timeptr->tm_hour,
1249 1.17 mlelstv timeptr->tm_min, timeptr->tm_sec);
1250 1.17 mlelstv trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
1251 1.17 mlelstv lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
1252 1.17 mlelstv trail / DIVISOR;
1253 1.17 mlelstv trail %= DIVISOR;
1254 1.17 mlelstv if (trail < 0 && lead > 0) {
1255 1.17 mlelstv trail += DIVISOR;
1256 1.17 mlelstv --lead;
1257 1.17 mlelstv } else if (lead < 0 && trail > 0) {
1258 1.17 mlelstv trail -= DIVISOR;
1259 1.17 mlelstv ++lead;
1260 1.17 mlelstv }
1261 1.17 mlelstv if (lead == 0)
1262 1.36 christos printf("%d", trail);
1263 1.36 christos else printf("%d%d", lead, ((trail < 0) ? -trail : trail));
1264 1.17 mlelstv }
1265