35 kx /* Dump time zone data in a textual format. */
35 kx
35 kx /*
35 kx ** This file is in the public domain, so clarified as of
35 kx ** 2009-05-17 by Arthur David Olson.
35 kx */
35 kx
35 kx #include "version.h"
35 kx
35 kx #ifndef NETBSD_INSPIRED
35 kx # define NETBSD_INSPIRED 1
35 kx #endif
35 kx
35 kx #include "private.h"
35 kx #include <stdio.h>
35 kx
35 kx #ifndef HAVE_SNPRINTF
35 kx # define HAVE_SNPRINTF (!PORT_TO_C89 || 199901 <= __STDC_VERSION__)
35 kx #endif
35 kx
35 kx #ifndef HAVE_LOCALTIME_R
35 kx # define HAVE_LOCALTIME_R 1
35 kx #endif
35 kx
35 kx #ifndef HAVE_LOCALTIME_RZ
35 kx # ifdef TM_ZONE
35 kx # define HAVE_LOCALTIME_RZ (NETBSD_INSPIRED && USE_LTZ)
35 kx # else
35 kx # define HAVE_LOCALTIME_RZ 0
35 kx # endif
35 kx #endif
35 kx
35 kx #ifndef HAVE_TZSET
35 kx # define HAVE_TZSET 1
35 kx #endif
35 kx
35 kx #ifndef ZDUMP_LO_YEAR
35 kx # define ZDUMP_LO_YEAR (-500)
35 kx #endif /* !defined ZDUMP_LO_YEAR */
35 kx
35 kx #ifndef ZDUMP_HI_YEAR
35 kx # define ZDUMP_HI_YEAR 2500
35 kx #endif /* !defined ZDUMP_HI_YEAR */
35 kx
35 kx #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
35 kx #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
35 kx #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
35 kx + SECSPERLYEAR * (intmax_t) (100 - 3))
35 kx
35 kx /*
35 kx ** True if SECSPER400YEARS is known to be representable as an
35 kx ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
35 kx ** even if SECSPER400YEARS is representable, because when that happens
35 kx ** the code merely runs a bit more slowly, and this slowness doesn't
35 kx ** occur on any practical platform.
35 kx */
35 kx enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
35 kx
35 kx #if HAVE_GETTEXT
35 kx # include <locale.h> /* for setlocale */
35 kx #endif /* HAVE_GETTEXT */
35 kx
35 kx #if ! HAVE_LOCALTIME_RZ
35 kx # undef timezone_t
35 kx # define timezone_t char **
35 kx #endif
35 kx
35 kx #if !HAVE_POSIX_DECLS
35 kx extern int getopt(int argc, char * const argv[],
35 kx const char * options);
35 kx extern char * optarg;
35 kx extern int optind;
35 kx #endif
35 kx
35 kx /* The minimum and maximum finite time values. */
35 kx enum { atime_shift = CHAR_BIT * sizeof(time_t) - 2 };
35 kx static time_t const absolute_min_time =
35 kx ((time_t) -1 < 0
35 kx ? (- ((time_t) ~ (time_t) 0 < 0)
35 kx - (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift)))
35 kx : 0);
35 kx static time_t const absolute_max_time =
35 kx ((time_t) -1 < 0
35 kx ? (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift))
35 kx : -1);
35 kx static int longest;
35 kx static char const *progname;
35 kx static bool warned;
35 kx static bool errout;
35 kx
35 kx static char const *abbr(struct tm const *);
35 kx ATTRIBUTE_REPRODUCIBLE static intmax_t delta(struct tm *, struct tm *);
35 kx static void dumptime(struct tm const *);
35 kx static time_t hunt(timezone_t, time_t, time_t, bool);
35 kx static void show(timezone_t, char *, time_t, bool);
35 kx static void showextrema(timezone_t, char *, time_t, struct tm *, time_t);
35 kx static void showtrans(char const *, struct tm const *, time_t, char const *,
35 kx char const *);
35 kx static const char *tformat(void);
35 kx ATTRIBUTE_REPRODUCIBLE static time_t yeartot(intmax_t);
35 kx
35 kx /* Is C an ASCII digit? */
35 kx static bool
35 kx is_digit(char c)
35 kx {
35 kx return '0' <= c && c <= '9';
35 kx }
35 kx
35 kx /* Is A an alphabetic character in the C locale? */
35 kx static bool
35 kx is_alpha(char a)
35 kx {
35 kx switch (a) {
35 kx default:
35 kx return false;
35 kx case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
35 kx case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
35 kx case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
35 kx case 'V': case 'W': case 'X': case 'Y': case 'Z':
35 kx case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
35 kx case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
35 kx case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
35 kx case 'v': case 'w': case 'x': case 'y': case 'z':
35 kx return true;
35 kx }
35 kx }
35 kx
35 kx ATTRIBUTE_NORETURN static void
35 kx size_overflow(void)
35 kx {
35 kx fprintf(stderr, _("%s: size overflow\n"), progname);
35 kx exit(EXIT_FAILURE);
35 kx }
35 kx
35 kx /* Return A + B, exiting if the result would overflow either ptrdiff_t
35 kx or size_t. A and B are both nonnegative. */
35 kx ATTRIBUTE_REPRODUCIBLE static ptrdiff_t
35 kx sumsize(ptrdiff_t a, ptrdiff_t b)
35 kx {
35 kx #ifdef ckd_add
35 kx ptrdiff_t sum;
35 kx if (!ckd_add(&sum, a, b) && sum <= INDEX_MAX)
35 kx return sum;
35 kx #else
35 kx if (a <= INDEX_MAX && b <= INDEX_MAX - a)
35 kx return a + b;
35 kx #endif
35 kx size_overflow();
35 kx }
35 kx
35 kx /* Return the size of of the string STR, including its trailing NUL.
35 kx Report an error and exit if this would exceed INDEX_MAX which means
35 kx pointer subtraction wouldn't work. */
35 kx static ptrdiff_t
35 kx xstrsize(char const *str)
35 kx {
35 kx size_t len = strlen(str);
35 kx if (len < INDEX_MAX)
35 kx return len + 1;
35 kx size_overflow();
35 kx }
35 kx
35 kx /* Return a pointer to a newly allocated buffer of size SIZE, exiting
35 kx on failure. SIZE should be positive. */
35 kx ATTRIBUTE_MALLOC static void *
35 kx xmalloc(ptrdiff_t size)
35 kx {
35 kx void *p = malloc(size);
35 kx if (!p) {
35 kx fprintf(stderr, _("%s: Memory exhausted\n"), progname);
35 kx exit(EXIT_FAILURE);
35 kx }
35 kx return p;
35 kx }
35 kx
35 kx #if ! HAVE_TZSET
35 kx # undef tzset
35 kx # define tzset zdump_tzset
35 kx static void tzset(void) { }
35 kx #endif
35 kx
35 kx /* Assume gmtime_r works if localtime_r does.
35 kx A replacement localtime_r is defined below if needed. */
35 kx #if ! HAVE_LOCALTIME_R
35 kx
35 kx # undef gmtime_r
35 kx # define gmtime_r zdump_gmtime_r
35 kx
35 kx static struct tm *
35 kx gmtime_r(time_t *tp, struct tm *tmp)
35 kx {
35 kx struct tm *r = gmtime(tp);
35 kx if (r) {
35 kx *tmp = *r;
35 kx r = tmp;
35 kx }
35 kx return r;
35 kx }
35 kx
35 kx #endif
35 kx
35 kx /* Platforms with TM_ZONE don't need tzname, so they can use the
35 kx faster localtime_rz or localtime_r if available. */
35 kx
35 kx #if defined TM_ZONE && HAVE_LOCALTIME_RZ
35 kx # define USE_LOCALTIME_RZ true
35 kx #else
35 kx # define USE_LOCALTIME_RZ false
35 kx #endif
35 kx
35 kx #if ! USE_LOCALTIME_RZ
35 kx
35 kx # if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET
35 kx # undef localtime_r
35 kx # define localtime_r zdump_localtime_r
35 kx static struct tm *
35 kx localtime_r(time_t *tp, struct tm *tmp)
35 kx {
35 kx struct tm *r = localtime(tp);
35 kx if (r) {
35 kx *tmp = *r;
35 kx r = tmp;
35 kx }
35 kx return r;
35 kx }
35 kx # endif
35 kx
35 kx # undef localtime_rz
35 kx # define localtime_rz zdump_localtime_rz
35 kx static struct tm *
35 kx localtime_rz(ATTRIBUTE_MAYBE_UNUSED timezone_t rz, time_t *tp, struct tm *tmp)
35 kx {
35 kx return localtime_r(tp, tmp);
35 kx }
35 kx
35 kx # ifdef TYPECHECK
35 kx # undef mktime_z
35 kx # define mktime_z zdump_mktime_z
35 kx static time_t
35 kx mktime_z(timezone_t tz, struct tm *tmp)
35 kx {
35 kx return mktime(tmp);
35 kx }
35 kx # endif
35 kx
35 kx # undef tzalloc
35 kx # undef tzfree
35 kx # define tzalloc zdump_tzalloc
35 kx # define tzfree zdump_tzfree
35 kx
35 kx static timezone_t
35 kx tzalloc(char const *val)
35 kx {
35 kx # if HAVE_SETENV
35 kx if (setenv("TZ", val, 1) != 0) {
35 kx char const *e = strerror(errno);
35 kx fprintf(stderr, _("%s: setenv: %s\n"), progname, e);
35 kx exit(EXIT_FAILURE);
35 kx }
35 kx tzset();
35 kx return &optarg; /* Any valid non-null char ** will do. */
35 kx # else
35 kx enum { TZeqlen = 3 };
35 kx static char const TZeq[TZeqlen] = "TZ=";
35 kx static char **fakeenv;
35 kx static ptrdiff_t fakeenv0size;
35 kx void *freeable = NULL;
35 kx char **env = fakeenv, **initial_environ;
35 kx ptrdiff_t valsize = xstrsize(val);
35 kx if (fakeenv0size < valsize) {
35 kx char **e = environ, **to;
35 kx ptrdiff_t initial_nenvptrs = 1; /* Counting the trailing NULL pointer. */
35 kx
35 kx while (*e++) {
35 kx # ifdef ckd_add
35 kx if (ckd_add(&initial_nenvptrs, initial_nenvptrs, 1)
35 kx || INDEX_MAX < initial_nenvptrs)
35 kx size_overflow();
35 kx # else
35 kx if (initial_nenvptrs == INDEX_MAX / sizeof *environ)
35 kx size_overflow();
35 kx initial_nenvptrs++;
35 kx # endif
35 kx }
35 kx fakeenv0size = sumsize(valsize, valsize);
35 kx fakeenv0size = max(fakeenv0size, 64);
35 kx freeable = env;
35 kx fakeenv = env =
35 kx xmalloc(sumsize(sumsize(sizeof *environ,
35 kx initial_nenvptrs * sizeof *environ),
35 kx sumsize(TZeqlen, fakeenv0size)));
35 kx to = env + 1;
35 kx for (e = environ; (*to = *e); e++)
35 kx to += strncmp(*e, TZeq, TZeqlen) != 0;
35 kx env[0] = memcpy(to + 1, TZeq, TZeqlen);
35 kx }
35 kx memcpy(env[0] + TZeqlen, val, valsize);
35 kx initial_environ = environ;
35 kx environ = env;
35 kx tzset();
35 kx free(freeable);
35 kx return initial_environ;
35 kx # endif
35 kx }
35 kx
35 kx static void
35 kx tzfree(ATTRIBUTE_MAYBE_UNUSED timezone_t initial_environ)
35 kx {
35 kx # if !HAVE_SETENV
35 kx environ = initial_environ;
35 kx tzset();
35 kx # endif
35 kx }
35 kx #endif /* ! USE_LOCALTIME_RZ */
35 kx
35 kx /* A UT time zone, and its initializer. */
35 kx static timezone_t gmtz;
35 kx static void
35 kx gmtzinit(void)
35 kx {
35 kx if (USE_LOCALTIME_RZ) {
35 kx /* Try "GMT" first to find out whether this is one of the rare
35 kx platforms where time_t counts leap seconds; this works due to
35 kx the "Zone GMT 0 - GMT" line in the "etcetera" file. If "GMT"
35 kx fails, fall back on "GMT0" which might be similar due to the
35 kx "Link GMT GMT0" line in the "backward" file, and which
35 kx should work on all POSIX platforms. The rest of zdump does not
35 kx use the "GMT" abbreviation that comes from this setting, so it
35 kx is OK to use "GMT" here rather than the modern "UTC" which
35 kx would not work on platforms that omit the "backward" file. */
35 kx gmtz = tzalloc("GMT");
35 kx if (!gmtz) {
35 kx static char const gmt0[] = "GMT0";
35 kx gmtz = tzalloc(gmt0);
35 kx if (!gmtz) {
35 kx char const *e = strerror(errno);
35 kx fprintf(stderr, _("%s: unknown timezone '%s': %s\n"),
35 kx progname, gmt0, e);
35 kx exit(EXIT_FAILURE);
35 kx }
35 kx }
35 kx }
35 kx }
35 kx
35 kx /* Convert *TP to UT, storing the broken-down time into *TMP.
35 kx Return TMP if successful, NULL otherwise. This is like gmtime_r(TP, TMP),
35 kx except typically faster if USE_LOCALTIME_RZ. */
35 kx static struct tm *
35 kx my_gmtime_r(time_t *tp, struct tm *tmp)
35 kx {
35 kx return USE_LOCALTIME_RZ ? localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp);
35 kx }
35 kx
35 kx #ifndef TYPECHECK
35 kx # define my_localtime_rz localtime_rz
35 kx #else /* !defined TYPECHECK */
35 kx
35 kx static struct tm *
35 kx my_localtime_rz(timezone_t tz, time_t *tp, struct tm *tmp)
35 kx {
35 kx tmp = localtime_rz(tz, tp, tmp);
35 kx if (tmp) {
35 kx struct tm tm;
35 kx register time_t t;
35 kx
35 kx tm = *tmp;
35 kx t = mktime_z(tz, &tm);
35 kx if (t != *tp) {
35 kx fflush(stdout);
35 kx fprintf(stderr, "\n%s: ", progname);
35 kx fprintf(stderr, tformat(), *tp);
35 kx fprintf(stderr, " ->");
35 kx fprintf(stderr, " year=%d", tmp->tm_year);
35 kx fprintf(stderr, " mon=%d", tmp->tm_mon);
35 kx fprintf(stderr, " mday=%d", tmp->tm_mday);
35 kx fprintf(stderr, " hour=%d", tmp->tm_hour);
35 kx fprintf(stderr, " min=%d", tmp->tm_min);
35 kx fprintf(stderr, " sec=%d", tmp->tm_sec);
35 kx fprintf(stderr, " isdst=%d", tmp->tm_isdst);
35 kx fprintf(stderr, " -> ");
35 kx fprintf(stderr, tformat(), t);
35 kx fprintf(stderr, "\n");
35 kx errout = true;
35 kx }
35 kx }
35 kx return tmp;
35 kx }
35 kx #endif /* !defined TYPECHECK */
35 kx
35 kx static void
35 kx abbrok(const char *const abbrp, const char *const zone)
35 kx {
35 kx register const char * cp;
35 kx register const char * wp;
35 kx
35 kx if (warned)
35 kx return;
35 kx cp = abbrp;
35 kx while (is_alpha(*cp) || is_digit(*cp) || *cp == '-' || *cp == '+')
35 kx ++cp;
35 kx if (*cp)
35 kx wp = _("has characters other than ASCII alphanumerics, '-' or '+'");
35 kx else if (cp - abbrp < 3)
35 kx wp = _("has fewer than 3 characters");
35 kx else if (cp - abbrp > 6)
35 kx wp = _("has more than 6 characters");
35 kx else
35 kx return;
35 kx fflush(stdout);
35 kx fprintf(stderr,
35 kx _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
35 kx progname, zone, abbrp, wp);
35 kx warned = errout = true;
35 kx }
35 kx
35 kx /* Return a time zone abbreviation. If the abbreviation needs to be
35 kx saved, use *BUF (of size *BUFALLOC) to save it, and return the
35 kx abbreviation in the possibly reallocated *BUF. Otherwise, just
35 kx return the abbreviation. Get the abbreviation from TMP.
35 kx Exit on memory allocation failure. */
35 kx static char const *
35 kx saveabbr(char **buf, ptrdiff_t *bufalloc, struct tm const *tmp)
35 kx {
35 kx char const *ab = abbr(tmp);
35 kx if (HAVE_LOCALTIME_RZ)
35 kx return ab;
35 kx else {
35 kx ptrdiff_t absize = xstrsize(ab);
35 kx if (*bufalloc < absize) {
35 kx free(*buf);
35 kx
35 kx /* Make the new buffer at least twice as long as the old,
35 kx to avoid O(N**2) behavior on repeated calls. */
35 kx *bufalloc = sumsize(*bufalloc, absize);
35 kx
35 kx *buf = xmalloc(*bufalloc);
35 kx }
35 kx return strcpy(*buf, ab);
35 kx }
35 kx }
35 kx
35 kx static void
35 kx close_file(FILE *stream)
35 kx {
35 kx char const *e = (ferror(stream) ? _("I/O error")
35 kx : fclose(stream) != 0 ? strerror(errno) : NULL);
35 kx if (e) {
35 kx fprintf(stderr, "%s: %s\n", progname, e);
35 kx exit(EXIT_FAILURE);
35 kx }
35 kx }
35 kx
35 kx static void
35 kx usage(FILE * const stream, const int status)
35 kx {
35 kx fprintf(stream,
35 kx _("%s: usage: %s OPTIONS TIMEZONE ...\n"
35 kx "Options include:\n"
35 kx " -c [L,]U Start at year L (default -500), end before year U (default 2500)\n"
35 kx " -t [L,]U Start at time L, end before time U (in seconds since 1970)\n"
35 kx " -i List transitions briefly (format is experimental)\n" \
35 kx " -v List transitions verbosely\n"
35 kx " -V List transitions a bit less verbosely\n"
35 kx " --help Output this help\n"
35 kx " --version Output version info\n"
35 kx "\n"
35 kx "Report bugs to %s.\n"),
35 kx progname, progname, REPORT_BUGS_TO);
35 kx if (status == EXIT_SUCCESS)
35 kx close_file(stream);
35 kx exit(status);
35 kx }
35 kx
35 kx int
35 kx main(int argc, char *argv[])
35 kx {
35 kx /* These are static so that they're initially zero. */
35 kx static char * abbrev;
35 kx static ptrdiff_t abbrevsize;
35 kx
35 kx register int i;
35 kx register bool vflag;
35 kx register bool Vflag;
35 kx register char * cutarg;
35 kx register char * cuttimes;
35 kx register time_t cutlotime;
35 kx register time_t cuthitime;
35 kx time_t now;
35 kx bool iflag = false;
35 kx
35 kx cutlotime = absolute_min_time;
35 kx cuthitime = absolute_max_time;
35 kx #if HAVE_GETTEXT
35 kx setlocale(LC_ALL, "");
35 kx # ifdef TZ_DOMAINDIR
35 kx bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
35 kx # endif /* defined TEXTDOMAINDIR */
35 kx textdomain(TZ_DOMAIN);
35 kx #endif /* HAVE_GETTEXT */
35 kx progname = argv[0] ? argv[0] : "zdump";
35 kx for (i = 1; i < argc; ++i)
35 kx if (strcmp(argv[i], "--version") == 0) {
35 kx printf("zdump %s%s\n", PKGVERSION, TZVERSION);
35 kx return EXIT_SUCCESS;
35 kx } else if (strcmp(argv[i], "--help") == 0) {
35 kx usage(stdout, EXIT_SUCCESS);
35 kx }
35 kx vflag = Vflag = false;
35 kx cutarg = cuttimes = NULL;
35 kx for (;;)
35 kx switch (getopt(argc, argv, "c:it:vV")) {
35 kx case 'c': cutarg = optarg; break;
35 kx case 't': cuttimes = optarg; break;
35 kx case 'i': iflag = true; break;
35 kx case 'v': vflag = true; break;
35 kx case 'V': Vflag = true; break;
35 kx case -1:
35 kx if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
35 kx goto arg_processing_done;
35 kx ATTRIBUTE_FALLTHROUGH;
35 kx default:
35 kx usage(stderr, EXIT_FAILURE);
35 kx }
35 kx arg_processing_done:;
35 kx
35 kx if (iflag | vflag | Vflag) {
35 kx intmax_t lo;
35 kx intmax_t hi;
35 kx char *loend, *hiend;
35 kx register intmax_t cutloyear = ZDUMP_LO_YEAR;
35 kx register intmax_t cuthiyear = ZDUMP_HI_YEAR;
35 kx if (cutarg != NULL) {
35 kx lo = strtoimax(cutarg, &loend, 10);
35 kx if (cutarg != loend && !*loend) {
35 kx hi = lo;
35 kx cuthiyear = hi;
35 kx } else if (cutarg != loend && *loend == ','
35 kx && (hi = strtoimax(loend + 1, &hiend, 10),
35 kx loend + 1 != hiend && !*hiend)) {
35 kx cutloyear = lo;
35 kx cuthiyear = hi;
35 kx } else {
35 kx fprintf(stderr, _("%s: wild -c argument %s\n"),
35 kx progname, cutarg);
35 kx return EXIT_FAILURE;
35 kx }
35 kx }
35 kx if (cutarg != NULL || cuttimes == NULL) {
35 kx cutlotime = yeartot(cutloyear);
35 kx cuthitime = yeartot(cuthiyear);
35 kx }
35 kx if (cuttimes != NULL) {
35 kx lo = strtoimax(cuttimes, &loend, 10);
35 kx if (cuttimes != loend && !*loend) {
35 kx hi = lo;
35 kx if (hi < cuthitime) {
35 kx if (hi < absolute_min_time + 1)
35 kx hi = absolute_min_time + 1;
35 kx cuthitime = hi;
35 kx }
35 kx } else if (cuttimes != loend && *loend == ','
35 kx && (hi = strtoimax(loend + 1, &hiend, 10),
35 kx loend + 1 != hiend && !*hiend)) {
35 kx if (cutlotime < lo) {
35 kx if (absolute_max_time < lo)
35 kx lo = absolute_max_time;
35 kx cutlotime = lo;
35 kx }
35 kx if (hi < cuthitime) {
35 kx if (hi < absolute_min_time + 1)
35 kx hi = absolute_min_time + 1;
35 kx cuthitime = hi;
35 kx }
35 kx } else {
35 kx fprintf(stderr,
35 kx _("%s: wild -t argument %s\n"),
35 kx progname, cuttimes);
35 kx return EXIT_FAILURE;
35 kx }
35 kx }
35 kx }
35 kx gmtzinit();
35 kx if (iflag | vflag | Vflag)
35 kx now = 0;
35 kx else {
35 kx now = time(NULL);
35 kx now |= !now;
35 kx }
35 kx longest = 0;
35 kx for (i = optind; i < argc; i++) {
35 kx size_t arglen = strlen(argv[i]);
35 kx if (longest < arglen)
35 kx longest = min(arglen, INT_MAX);
35 kx }
35 kx
35 kx for (i = optind; i < argc; ++i) {
35 kx timezone_t tz = tzalloc(argv[i]);
35 kx char const *ab = (char const *)0;
35 kx time_t t;
35 kx struct tm tm, newtm;
35 kx bool tm_ok;
35 kx if (!tz) {
35 kx char const *e = strerror(errno);
35 kx fprintf(stderr, _("%s: unknown timezone '%s': %s\n"),
35 kx progname, argv[1], e);
35 kx return EXIT_FAILURE;
35 kx }
35 kx if (now) {
35 kx show(tz, argv[i], now, false);
35 kx tzfree(tz);
35 kx continue;
35 kx }
35 kx warned = false;
35 kx t = absolute_min_time;
35 kx if (! (iflag | Vflag)) {
35 kx show(tz, argv[i], t, true);
35 kx if (my_localtime_rz(tz, &t, &tm) == NULL
35 kx && t < cutlotime) {
35 kx time_t newt = cutlotime;
35 kx if (my_localtime_rz(tz, &newt, &newtm) != NULL)
35 kx showextrema(tz, argv[i], t, NULL, newt);
35 kx }
35 kx }
35 kx if (t + 1 < cutlotime)
35 kx t = cutlotime - 1;
35 kx tm_ok = my_localtime_rz(tz, &t, &tm) != NULL;
35 kx if (tm_ok) {
35 kx ab = saveabbr(&abbrev, &abbrevsize, &tm);
35 kx if (iflag) {
35 kx showtrans("\nTZ=%f", &tm, t, ab, argv[i]);
35 kx showtrans("-\t-\t%Q", &tm, t, ab, argv[i]);
35 kx }
35 kx } else
35 kx ab = NULL;
35 kx while (t < cuthitime - 1) {
35 kx time_t newt = ((t < absolute_max_time - SECSPERDAY / 2
35 kx && t + SECSPERDAY / 2 < cuthitime - 1)
35 kx ? t + SECSPERDAY / 2
35 kx : cuthitime - 1);
35 kx struct tm *newtmp = localtime_rz(tz, &newt, &newtm);
35 kx bool newtm_ok = newtmp != NULL;
35 kx if (tm_ok != newtm_ok
35 kx || (ab && (delta(&newtm, &tm) != newt - t
35 kx || newtm.tm_isdst != tm.tm_isdst
35 kx || strcmp(abbr(&newtm), ab) != 0))) {
35 kx newt = hunt(tz, t, newt, false);
35 kx newtmp = localtime_rz(tz, &newt, &newtm);
35 kx newtm_ok = newtmp != NULL;
35 kx if (iflag)
35 kx showtrans("%Y-%m-%d\t%L\t%Q", newtmp, newt,
35 kx newtm_ok ? abbr(&newtm) : NULL, argv[i]);
35 kx else {
35 kx show(tz, argv[i], newt - 1, true);
35 kx show(tz, argv[i], newt, true);
35 kx }
35 kx }
35 kx t = newt;
35 kx tm_ok = newtm_ok;
35 kx if (newtm_ok) {
35 kx ab = saveabbr(&abbrev, &abbrevsize, &newtm);
35 kx tm = newtm;
35 kx }
35 kx }
35 kx if (! (iflag | Vflag)) {
35 kx time_t newt = absolute_max_time;
35 kx t = cuthitime;
35 kx if (t < newt) {
35 kx struct tm *tmp = my_localtime_rz(tz, &t, &tm);
35 kx if (tmp != NULL
35 kx && my_localtime_rz(tz, &newt, &newtm) == NULL)
35 kx showextrema(tz, argv[i], t, tmp, newt);
35 kx }
35 kx show(tz, argv[i], absolute_max_time, true);
35 kx }
35 kx tzfree(tz);
35 kx }
35 kx close_file(stdout);
35 kx if (errout && (ferror(stderr) || fclose(stderr) != 0))
35 kx return EXIT_FAILURE;
35 kx return EXIT_SUCCESS;
35 kx }
35 kx
35 kx static time_t
35 kx yeartot(intmax_t y)
35 kx {
35 kx register intmax_t myy, seconds, years;
35 kx register time_t t;
35 kx
35 kx myy = EPOCH_YEAR;
35 kx t = 0;
35 kx while (myy < y) {
35 kx if (SECSPER400YEARS_FITS && 400 <= y - myy) {
35 kx intmax_t diff400 = (y - myy) / 400;
35 kx if (INTMAX_MAX / SECSPER400YEARS < diff400)
35 kx return absolute_max_time;
35 kx seconds = diff400 * SECSPER400YEARS;
35 kx years = diff400 * 400;
35 kx } else {
35 kx seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
35 kx years = 1;
35 kx }
35 kx myy += years;
35 kx if (t > absolute_max_time - seconds)
35 kx return absolute_max_time;
35 kx t += seconds;
35 kx }
35 kx while (y < myy) {
35 kx if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
35 kx intmax_t diff400 = (myy - y) / 400;
35 kx if (INTMAX_MAX / SECSPER400YEARS < diff400)
35 kx return absolute_min_time;
35 kx seconds = diff400 * SECSPER400YEARS;
35 kx years = diff400 * 400;
35 kx } else {
35 kx seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
35 kx years = 1;
35 kx }
35 kx myy -= years;
35 kx if (t < absolute_min_time + seconds)
35 kx return absolute_min_time;
35 kx t -= seconds;
35 kx }
35 kx return t;
35 kx }
35 kx
35 kx /* Search for a discontinuity in timezone TZ, in the
35 kx timestamps ranging from LOT through HIT. LOT and HIT disagree
35 kx about some aspect of timezone. If ONLY_OK, search only for
35 kx definedness changes, i.e., localtime succeeds on one side of the
35 kx transition but fails on the other side. Return the timestamp just
35 kx before the transition from LOT's settings. */
35 kx
35 kx static time_t
35 kx hunt(timezone_t tz, time_t lot, time_t hit, bool only_ok)
35 kx {
35 kx static char * loab;
35 kx static ptrdiff_t loabsize;
35 kx struct tm lotm;
35 kx struct tm tm;
35 kx
35 kx /* Convert LOT into a broken-down time here, even though our
35 kx caller already did that. On platforms without TM_ZONE,
35 kx tzname may have been altered since our caller broke down
35 kx LOT, and tzname needs to be changed back. */
35 kx bool lotm_ok = my_localtime_rz(tz, &lot, &lotm) != NULL;
35 kx bool tm_ok;
35 kx char const *ab = lotm_ok ? saveabbr(&loab, &loabsize, &lotm) : NULL;
35 kx
35 kx for ( ; ; ) {
35 kx /* T = average of LOT and HIT, rounding down.
35 kx Avoid overflow. */
35 kx int rem_sum = lot % 2 + hit % 2;
35 kx time_t t = (rem_sum == 2) - (rem_sum < 0) + lot / 2 + hit / 2;
35 kx if (t == lot)
35 kx break;
35 kx tm_ok = my_localtime_rz(tz, &t, &tm) != NULL;
35 kx if (lotm_ok == tm_ok
35 kx && (only_ok
35 kx || (ab && tm.tm_isdst == lotm.tm_isdst
35 kx && delta(&tm, &lotm) == t - lot
35 kx && strcmp(abbr(&tm), ab) == 0))) {
35 kx lot = t;
35 kx if (tm_ok)
35 kx lotm = tm;
35 kx } else hit = t;
35 kx }
35 kx return hit;
35 kx }
35 kx
35 kx /*
35 kx ** Thanks to Paul Eggert for logic used in delta_nonneg.
35 kx */
35 kx
35 kx static intmax_t
35 kx delta_nonneg(struct tm *newp, struct tm *oldp)
35 kx {
35 kx intmax_t oldy = oldp->tm_year;
35 kx int cycles = (newp->tm_year - oldy) / YEARSPERREPEAT;
35 kx intmax_t sec = SECSPERREPEAT, result = cycles * sec;
35 kx int tmy = oldp->tm_year + cycles * YEARSPERREPEAT;
35 kx for ( ; tmy < newp->tm_year; ++tmy)
35 kx result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
35 kx result += newp->tm_yday - oldp->tm_yday;
35 kx result *= HOURSPERDAY;
35 kx result += newp->tm_hour - oldp->tm_hour;
35 kx result *= MINSPERHOUR;
35 kx result += newp->tm_min - oldp->tm_min;
35 kx result *= SECSPERMIN;
35 kx result += newp->tm_sec - oldp->tm_sec;
35 kx return result;
35 kx }
35 kx
35 kx static intmax_t
35 kx delta(struct tm *newp, struct tm *oldp)
35 kx {
35 kx return (newp->tm_year < oldp->tm_year
35 kx ? -delta_nonneg(oldp, newp)
35 kx : delta_nonneg(newp, oldp));
35 kx }
35 kx
35 kx #ifndef TM_GMTOFF
35 kx /* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday.
35 kx Assume A and B differ by at most one year. */
35 kx static int
35 kx adjusted_yday(struct tm const *a, struct tm const *b)
35 kx {
35 kx int yday = a->tm_yday;
35 kx if (b->tm_year < a->tm_year)
35 kx yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE);
35 kx return yday;
35 kx }
35 kx #endif
35 kx
35 kx /* If A is the broken-down local time and B the broken-down UT for
35 kx the same instant, return A's UT offset in seconds, where positive
35 kx offsets are east of Greenwich. On failure, return LONG_MIN.
35 kx
35 kx If T is nonnull, *T is the timestamp that corresponds to A; call
35 kx my_gmtime_r and use its result instead of B. Otherwise, B is the
35 kx possibly nonnull result of an earlier call to my_gmtime_r. */
35 kx static long
35 kx gmtoff(struct tm const *a, ATTRIBUTE_MAYBE_UNUSED time_t *t,
35 kx ATTRIBUTE_MAYBE_UNUSED struct tm const *b)
35 kx {
35 kx #ifdef TM_GMTOFF
35 kx return a->TM_GMTOFF;
35 kx #else
35 kx struct tm tm;
35 kx if (t)
35 kx b = my_gmtime_r(t, &tm);
35 kx if (! b)
35 kx return LONG_MIN;
35 kx else {
35 kx int ayday = adjusted_yday(a, b);
35 kx int byday = adjusted_yday(b, a);
35 kx int days = ayday - byday;
35 kx long hours = a->tm_hour - b->tm_hour + 24 * days;
35 kx long minutes = a->tm_min - b->tm_min + 60 * hours;
35 kx long seconds = a->tm_sec - b->tm_sec + 60 * minutes;
35 kx return seconds;
35 kx }
35 kx #endif
35 kx }
35 kx
35 kx static void
35 kx show(timezone_t tz, char *zone, time_t t, bool v)
35 kx {
35 kx register struct tm * tmp;
35 kx register struct tm * gmtmp;
35 kx struct tm tm, gmtm;
35 kx
35 kx printf("%-*s ", longest, zone);
35 kx if (v) {
35 kx gmtmp = my_gmtime_r(&t, &gmtm);
35 kx if (gmtmp == NULL) {
35 kx printf(tformat(), t);
35 kx printf(_(" (gmtime failed)"));
35 kx } else {
35 kx dumptime(gmtmp);
35 kx printf(" UT");
35 kx }
35 kx printf(" = ");
35 kx }
35 kx tmp = my_localtime_rz(tz, &t, &tm);
35 kx if (tmp == NULL) {
35 kx printf(tformat(), t);
35 kx printf(_(" (localtime failed)"));
35 kx } else {
35 kx dumptime(tmp);
35 kx if (*abbr(tmp) != '\0')
35 kx printf(" %s", abbr(tmp));
35 kx if (v) {
35 kx long off = gmtoff(tmp, NULL, gmtmp);
35 kx printf(" isdst=%d", tmp->tm_isdst);
35 kx if (off != LONG_MIN)
35 kx printf(" gmtoff=%ld", off);
35 kx }
35 kx }
35 kx printf("\n");
35 kx if (tmp != NULL && *abbr(tmp) != '\0')
35 kx abbrok(abbr(tmp), zone);
35 kx }
35 kx
35 kx /* Show timestamps just before and just after a transition between
35 kx defined and undefined (or vice versa) in either localtime or
35 kx gmtime. These transitions are for timezone TZ with name ZONE, in
35 kx the range from LO (with broken-down time LOTMP if that is nonnull)
35 kx through HI. LO and HI disagree on definedness. */
35 kx
35 kx static void
35 kx showextrema(timezone_t tz, char *zone, time_t lo, struct tm *lotmp, time_t hi)
35 kx {
35 kx struct tm localtm[2], gmtm[2];
35 kx time_t t, boundary = hunt(tz, lo, hi, true);
35 kx bool old = false;
35 kx hi = (SECSPERDAY < hi - boundary
35 kx ? boundary + SECSPERDAY
35 kx : hi + (hi < TIME_T_MAX));
35 kx if (SECSPERDAY < boundary - lo) {
35 kx lo = boundary - SECSPERDAY;
35 kx lotmp = my_localtime_rz(tz, &lo, &localtm[old]);
35 kx }
35 kx if (lotmp)
35 kx localtm[old] = *lotmp;
35 kx else
35 kx localtm[old].tm_sec = -1;
35 kx if (! my_gmtime_r(&lo, &gmtm[old]))
35 kx gmtm[old].tm_sec = -1;
35 kx
35 kx /* Search sequentially for definedness transitions. Although this
35 kx could be sped up by refining 'hunt' to search for either
35 kx localtime or gmtime definedness transitions, it hardly seems
35 kx worth the trouble. */
35 kx for (t = lo + 1; t < hi; t++) {
35 kx bool new = !old;
35 kx if (! my_localtime_rz(tz, &t, &localtm[new]))
35 kx localtm[new].tm_sec = -1;
35 kx if (! my_gmtime_r(&t, &gmtm[new]))
35 kx gmtm[new].tm_sec = -1;
35 kx if (((localtm[old].tm_sec < 0) != (localtm[new].tm_sec < 0))
35 kx | ((gmtm[old].tm_sec < 0) != (gmtm[new].tm_sec < 0))) {
35 kx show(tz, zone, t - 1, true);
35 kx show(tz, zone, t, true);
35 kx }
35 kx old = new;
35 kx }
35 kx }
35 kx
35 kx #if HAVE_SNPRINTF
35 kx # define my_snprintf snprintf
35 kx #else
35 kx # include <stdarg.h>
35 kx
35 kx /* A substitute for snprintf that is good enough for zdump. */
35 kx ATTRIBUTE_FORMAT((printf, 3, 4)) static int
35 kx my_snprintf(char *s, size_t size, char const *format, ...)
35 kx {
35 kx int n;
35 kx va_list args;
35 kx char const *arg;
35 kx size_t arglen, slen;
35 kx char buf[1024];
35 kx va_start(args, format);
35 kx if (strcmp(format, "%s") == 0) {
35 kx arg = va_arg(args, char const *);
35 kx arglen = strlen(arg);
35 kx } else {
35 kx n = vsprintf(buf, format, args);
35 kx if (n < 0) {
35 kx va_end(args);
35 kx return n;
35 kx }
35 kx arg = buf;
35 kx arglen = n;
35 kx }
35 kx slen = arglen < size ? arglen : size - 1;
35 kx memcpy(s, arg, slen);
35 kx s[slen] = '\0';
35 kx n = arglen <= INT_MAX ? arglen : -1;
35 kx va_end(args);
35 kx return n;
35 kx }
35 kx #endif
35 kx
35 kx /* Store into BUF, of size SIZE, a formatted local time taken from *TM.
35 kx Use ISO 8601 format +HH:MM:SS. Omit :SS if SS is zero, and omit
35 kx :MM too if MM is also zero.
35 kx
35 kx Return the length of the resulting string. If the string does not
35 kx fit, return the length that the string would have been if it had
35 kx fit; do not overrun the output buffer. */
35 kx static int
35 kx format_local_time(char *buf, ptrdiff_t size, struct tm const *tm)
35 kx {
35 kx int ss = tm->tm_sec, mm = tm->tm_min, hh = tm->tm_hour;
35 kx return (ss
35 kx ? my_snprintf(buf, size, "%02d:%02d:%02d", hh, mm, ss)
35 kx : mm
35 kx ? my_snprintf(buf, size, "%02d:%02d", hh, mm)
35 kx : my_snprintf(buf, size, "%02d", hh));
35 kx }
35 kx
35 kx /* Store into BUF, of size SIZE, a formatted UT offset for the
35 kx localtime *TM corresponding to time T. Use ISO 8601 format
35 kx +HHMMSS, or -HHMMSS for timestamps west of Greenwich; use the
35 kx format -00 for unknown UT offsets. If the hour needs more than
35 kx two digits to represent, extend the length of HH as needed.
35 kx Otherwise, omit SS if SS is zero, and omit MM too if MM is also
35 kx zero.
35 kx
35 kx Return the length of the resulting string, or -1 if the result is
35 kx not representable as a string. If the string does not fit, return
35 kx the length that the string would have been if it had fit; do not
35 kx overrun the output buffer. */
35 kx static int
35 kx format_utc_offset(char *buf, ptrdiff_t size, struct tm const *tm, time_t t)
35 kx {
35 kx long off = gmtoff(tm, &t, NULL);
35 kx char sign = ((off < 0
35 kx || (off == 0
35 kx && (*abbr(tm) == '-' || strcmp(abbr(tm), "zzz") == 0)))
35 kx ? '-' : '+');
35 kx long hh;
35 kx int mm, ss;
35 kx if (off < 0)
35 kx {
35 kx if (off == LONG_MIN)
35 kx return -1;
35 kx off = -off;
35 kx }
35 kx ss = off % 60;
35 kx mm = off / 60 % 60;
35 kx hh = off / 60 / 60;
35 kx return (ss || 100 <= hh
35 kx ? my_snprintf(buf, size, "%c%02ld%02d%02d", sign, hh, mm, ss)
35 kx : mm
35 kx ? my_snprintf(buf, size, "%c%02ld%02d", sign, hh, mm)
35 kx : my_snprintf(buf, size, "%c%02ld", sign, hh));
35 kx }
35 kx
35 kx /* Store into BUF (of size SIZE) a quoted string representation of P.
35 kx If the representation's length is less than SIZE, return the
35 kx length; the representation is not null terminated. Otherwise
35 kx return SIZE, to indicate that BUF is too small. */
35 kx static ptrdiff_t
35 kx format_quoted_string(char *buf, ptrdiff_t size, char const *p)
35 kx {
35 kx char *b = buf;
35 kx ptrdiff_t s = size;
35 kx if (!s)
35 kx return size;
35 kx *b++ = '"', s--;
35 kx for (;;) {
35 kx char c = *p++;
35 kx if (s <= 1)
35 kx return size;
35 kx switch (c) {
35 kx default: *b++ = c, s--; continue;
35 kx case '\0': *b++ = '"', s--; return size - s;
35 kx case '"': case '\\': break;
35 kx case ' ': c = 's'; break;
35 kx case '\f': c = 'f'; break;
35 kx case '\n': c = 'n'; break;
35 kx case '\r': c = 'r'; break;
35 kx case '\t': c = 't'; break;
35 kx case '\v': c = 'v'; break;
35 kx }
35 kx *b++ = '\\', *b++ = c, s -= 2;
35 kx }
35 kx }
35 kx
35 kx /* Store into BUF (of size SIZE) a timestamp formatted by TIME_FMT.
35 kx TM is the broken-down time, T the seconds count, AB the time zone
35 kx abbreviation, and ZONE_NAME the zone name. Return true if
35 kx successful, false if the output would require more than SIZE bytes.
35 kx TIME_FMT uses the same format that strftime uses, with these
35 kx additions:
35 kx
35 kx %f zone name
35 kx %L local time as per format_local_time
35 kx %Q like "U\t%Z\tD" where U is the UT offset as for format_utc_offset
35 kx and D is the isdst flag; except omit D if it is zero, omit %Z if
35 kx it equals U, quote and escape %Z if it contains nonalphabetics,
35 kx and omit any trailing tabs. */
35 kx
35 kx static bool
35 kx istrftime(char *buf, ptrdiff_t size, char const *time_fmt,
35 kx struct tm const *tm, time_t t, char const *ab, char const *zone_name)
35 kx {
35 kx char *b = buf;
35 kx ptrdiff_t s = size;
35 kx char const *f = time_fmt, *p;
35 kx
35 kx for (p = f; ; p++)
35 kx if (*p == '%' && p[1] == '%')
35 kx p++;
35 kx else if (!*p
35 kx || (*p == '%'
35 kx && (p[1] == 'f' || p[1] == 'L' || p[1] == 'Q'))) {
35 kx ptrdiff_t formatted_len;
35 kx ptrdiff_t f_prefix_len = p - f;
35 kx ptrdiff_t f_prefix_copy_size = sumsize(f_prefix_len, 2);
35 kx char fbuf[100];
35 kx bool oversized = sizeof fbuf <= f_prefix_copy_size;
35 kx char *f_prefix_copy = oversized ? xmalloc(f_prefix_copy_size) : fbuf;
35 kx memcpy(f_prefix_copy, f, f_prefix_len);
35 kx strcpy(f_prefix_copy + f_prefix_len, "X");
35 kx formatted_len = strftime(b, s, f_prefix_copy, tm);
35 kx if (oversized)
35 kx free(f_prefix_copy);
35 kx if (formatted_len == 0)
35 kx return false;
35 kx formatted_len--;
35 kx b += formatted_len, s -= formatted_len;
35 kx if (!*p++)
35 kx break;
35 kx switch (*p) {
35 kx case 'f':
35 kx formatted_len = format_quoted_string(b, s, zone_name);
35 kx break;
35 kx case 'L':
35 kx formatted_len = format_local_time(b, s, tm);
35 kx break;
35 kx case 'Q':
35 kx {
35 kx bool show_abbr;
35 kx int offlen = format_utc_offset(b, s, tm, t);
35 kx if (! (0 <= offlen && offlen < s))
35 kx return false;
35 kx show_abbr = strcmp(b, ab) != 0;
35 kx b += offlen, s -= offlen;
35 kx if (show_abbr) {
35 kx char const *abp;
35 kx ptrdiff_t len;
35 kx if (s <= 1)
35 kx return false;
35 kx *b++ = '\t', s--;
35 kx for (abp = ab; is_alpha(*abp); abp++)
35 kx continue;
35 kx len = (!*abp && *ab
35 kx ? my_snprintf(b, s, "%s", ab)
35 kx : format_quoted_string(b, s, ab));
35 kx if (s <= len)
35 kx return false;
35 kx b += len, s -= len;
35 kx }
35 kx formatted_len
35 kx = (tm->tm_isdst
35 kx ? my_snprintf(b, s, &"\t\t%d"[show_abbr], tm->tm_isdst)
35 kx : 0);
35 kx }
35 kx break;
35 kx }
35 kx if (s <= formatted_len)
35 kx return false;
35 kx b += formatted_len, s -= formatted_len;
35 kx f = p + 1;
35 kx }
35 kx *b = '\0';
35 kx return true;
35 kx }
35 kx
35 kx /* Show a time transition. */
35 kx static void
35 kx showtrans(char const *time_fmt, struct tm const *tm, time_t t, char const *ab,
35 kx char const *zone_name)
35 kx {
35 kx if (!tm) {
35 kx printf(tformat(), t);
35 kx putchar('\n');
35 kx } else {
35 kx char stackbuf[1000];
35 kx ptrdiff_t size = sizeof stackbuf;
35 kx char *buf = stackbuf;
35 kx char *bufalloc = NULL;
35 kx while (! istrftime(buf, size, time_fmt, tm, t, ab, zone_name)) {
35 kx size = sumsize(size, size);
35 kx free(bufalloc);
35 kx buf = bufalloc = xmalloc(size);
35 kx }
35 kx puts(buf);
35 kx free(bufalloc);
35 kx }
35 kx }
35 kx
35 kx static char const *
35 kx abbr(struct tm const *tmp)
35 kx {
35 kx #ifdef TM_ZONE
35 kx return tmp->TM_ZONE;
35 kx #else
35 kx # if HAVE_TZNAME
35 kx if (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst])
35 kx return tzname[0 < tmp->tm_isdst];
35 kx # endif
35 kx return "";
35 kx #endif
35 kx }
35 kx
35 kx /*
35 kx ** The code below can fail on certain theoretical systems;
35 kx ** it works on all known real-world systems as of 2022-01-25.
35 kx */
35 kx
35 kx static const char *
35 kx tformat(void)
35 kx {
35 kx #if HAVE__GENERIC
35 kx /* C11-style _Generic is more likely to return the correct
35 kx format when distinct types have the same size. */
35 kx char const *fmt =
35 kx _Generic(+ (time_t) 0,
35 kx int: "%d", long: "%ld", long long: "%lld",
35 kx unsigned: "%u", unsigned long: "%lu",
35 kx unsigned long long: "%llu",
35 kx default: NULL);
35 kx if (fmt)
35 kx return fmt;
35 kx fmt = _Generic((time_t) 0,
35 kx intmax_t: "%"PRIdMAX, uintmax_t: "%"PRIuMAX,
35 kx default: NULL);
35 kx if (fmt)
35 kx return fmt;
35 kx #endif
35 kx if (0 > (time_t) -1) { /* signed */
35 kx if (sizeof(time_t) == sizeof(intmax_t))
35 kx return "%"PRIdMAX;
35 kx if (sizeof(time_t) > sizeof(long))
35 kx return "%lld";
35 kx if (sizeof(time_t) > sizeof(int))
35 kx return "%ld";
35 kx return "%d";
35 kx }
35 kx #ifdef PRIuMAX
35 kx if (sizeof(time_t) == sizeof(uintmax_t))
35 kx return "%"PRIuMAX;
35 kx #endif
35 kx if (sizeof(time_t) > sizeof(unsigned long))
35 kx return "%llu";
35 kx if (sizeof(time_t) > sizeof(unsigned int))
35 kx return "%lu";
35 kx return "%u";
35 kx }
35 kx
35 kx static void
35 kx dumptime(register const struct tm *timeptr)
35 kx {
35 kx static const char wday_name[][4] = {
35 kx "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
35 kx };
35 kx static const char mon_name[][4] = {
35 kx "Jan", "Feb", "Mar", "Apr", "May", "Jun",
35 kx "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
35 kx };
35 kx register int lead;
35 kx register int trail;
35 kx int DIVISOR = 10;
35 kx
35 kx /*
35 kx ** The packaged localtime_rz and gmtime_r never put out-of-range
35 kx ** values in tm_wday or tm_mon, but since this code might be compiled
35 kx ** with other (perhaps experimental) versions, paranoia is in order.
35 kx */
35 kx printf("%s %s%3d %.2d:%.2d:%.2d ",
35 kx ((0 <= timeptr->tm_wday
35 kx && timeptr->tm_wday < sizeof wday_name / sizeof wday_name[0])
35 kx ? wday_name[timeptr->tm_wday] : "???"),
35 kx ((0 <= timeptr->tm_mon
35 kx && timeptr->tm_mon < sizeof mon_name / sizeof mon_name[0])
35 kx ? mon_name[timeptr->tm_mon] : "???"),
35 kx timeptr->tm_mday, timeptr->tm_hour,
35 kx timeptr->tm_min, timeptr->tm_sec);
35 kx trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
35 kx lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
35 kx trail / DIVISOR;
35 kx trail %= DIVISOR;
35 kx if (trail < 0 && lead > 0) {
35 kx trail += DIVISOR;
35 kx --lead;
35 kx } else if (lead < 0 && trail > 0) {
35 kx trail -= DIVISOR;
35 kx ++lead;
35 kx }
35 kx if (lead == 0)
35 kx printf("%d", trail);
35 kx else printf("%d%d", lead, ((trail < 0) ? -trail : trail));
35 kx }