zdump.c 33 KB

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