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