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