Home | History | Annotate | Line # | Download | only in time
zdump.c revision 1.37
      1  1.37  christos /*	$NetBSD: zdump.c,v 1.37 2014/10/07 22:14:46 christos Exp $	*/
      2  1.17   mlelstv /*
      3  1.17   mlelstv ** This file is in the public domain, so clarified as of
      4  1.17   mlelstv ** 2009-05-17 by Arthur David Olson.
      5  1.17   mlelstv */
      6   1.2       jtc 
      7   1.6  christos #include <sys/cdefs.h>
      8   1.1       jtc #ifndef lint
      9  1.37  christos __RCSID("$NetBSD: zdump.c,v 1.37 2014/10/07 22:14:46 christos Exp $");
     10   1.1       jtc #endif /* !defined lint */
     11   1.1       jtc 
     12   1.1       jtc /*
     13   1.1       jtc ** This code has been made independent of the rest of the time
     14   1.1       jtc ** conversion package to increase confidence in the verification it provides.
     15   1.1       jtc ** You can use this code to help in verifying other implementations.
     16  1.29  christos **
     17  1.36  christos ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
     18   1.1       jtc */
     19   1.1       jtc 
     20  1.36  christos #ifndef NETBSD_INSPIRED
     21  1.36  christos # define NETBSD_INSPIRED 1
     22  1.36  christos #endif
     23  1.36  christos #ifndef USE_LTZ
     24  1.36  christos # define USE_LTZ 1
     25  1.36  christos #endif
     26  1.36  christos 
     27  1.36  christos #if USE_LTZ
     28  1.29  christos #include "private.h"
     29  1.36  christos #endif
     30  1.36  christos 
     31  1.36  christos /* Enable tm_gmtoff and tm_zone on GNUish systems.  */
     32  1.36  christos #define _GNU_SOURCE 1
     33  1.36  christos /* Enable strtoimax on Solaris 10.  */
     34  1.36  christos #define __EXTENSIONS__ 1
     35  1.29  christos 
     36  1.15  christos #include "stdio.h"	/* for stdout, stderr */
     37   1.1       jtc #include "string.h"	/* for strcpy */
     38   1.1       jtc #include "sys/types.h"	/* for time_t */
     39   1.1       jtc #include "time.h"	/* for struct tm */
     40   1.1       jtc #include "stdlib.h"	/* for exit, malloc, atoi */
     41  1.36  christos #include "limits.h"	/* for CHAR_BIT, LLONG_MAX */
     42  1.36  christos #include <errno.h>
     43  1.15  christos #include <err.h>
     44  1.17   mlelstv 
     45  1.29  christos /*
     46  1.29  christos ** Substitutes for pre-C99 compilers.
     47  1.29  christos ** Much of this section of code is stolen from private.h.
     48  1.29  christos */
     49  1.29  christos 
     50  1.29  christos #ifndef HAVE_STDINT_H
     51  1.29  christos # define HAVE_STDINT_H \
     52  1.33  christos     (199901 <= __STDC_VERSION__ \
     53  1.33  christos      || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__)	\
     54  1.33  christos      || __CYGWIN__)
     55  1.29  christos #endif
     56  1.29  christos #if HAVE_STDINT_H
     57  1.29  christos # include "stdint.h"
     58  1.29  christos #endif
     59  1.29  christos #ifndef HAVE_INTTYPES_H
     60  1.29  christos # define HAVE_INTTYPES_H HAVE_STDINT_H
     61  1.29  christos #endif
     62  1.29  christos #if HAVE_INTTYPES_H
     63  1.29  christos # include <inttypes.h>
     64  1.29  christos #endif
     65  1.29  christos 
     66  1.29  christos #ifndef INT_FAST32_MAX
     67  1.29  christos # if INT_MAX >> 31 == 0
     68  1.29  christos typedef long int_fast32_t;
     69  1.29  christos # else
     70  1.29  christos typedef int int_fast32_t;
     71  1.29  christos # endif
     72  1.29  christos #endif
     73  1.29  christos 
     74  1.36  christos /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
     75  1.36  christos #if !defined LLONG_MAX && defined __LONG_LONG_MAX__
     76  1.36  christos # define LLONG_MAX __LONG_LONG_MAX__
     77  1.36  christos #endif
     78  1.36  christos 
     79  1.29  christos #ifndef INTMAX_MAX
     80  1.36  christos # ifdef LLONG_MAX
     81  1.29  christos typedef long long intmax_t;
     82  1.32  christos #  define strtoimax strtoll
     83  1.36  christos #  define INTMAX_MAX LLONG_MAX
     84  1.29  christos # else
     85  1.29  christos typedef long intmax_t;
     86  1.32  christos #  define strtoimax strtol
     87  1.36  christos #  define INTMAX_MAX LONG_MAX
     88  1.36  christos # endif
     89  1.36  christos #endif
     90  1.36  christos 
     91  1.36  christos #ifndef PRIdMAX
     92  1.36  christos # if INTMAX_MAX == LLONG_MAX
     93  1.36  christos #  define PRIdMAX "lld"
     94  1.36  christos # else
     95  1.29  christos #  define PRIdMAX "ld"
     96  1.29  christos # endif
     97  1.29  christos #endif
     98  1.29  christos 
     99  1.36  christos /* Infer TM_ZONE on systems where this information is known, but suppress
    100  1.36  christos    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
    101  1.36  christos #if (defined __GLIBC__ \
    102  1.36  christos      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
    103  1.36  christos      || (defined __APPLE__ && defined __MACH__))
    104  1.36  christos # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
    105  1.36  christos #  define TM_GMTOFF tm_gmtoff
    106  1.36  christos # endif
    107  1.36  christos # if !defined TM_ZONE && !defined NO_TM_ZONE
    108  1.36  christos #  define TM_ZONE tm_zone
    109  1.36  christos # endif
    110  1.36  christos #endif
    111  1.36  christos 
    112  1.36  christos #ifndef HAVE_LOCALTIME_R
    113  1.36  christos # define HAVE_LOCALTIME_R 1
    114  1.36  christos #endif
    115  1.36  christos 
    116  1.36  christos #ifndef HAVE_LOCALTIME_RZ
    117  1.36  christos # ifdef TM_ZONE
    118  1.36  christos #  define HAVE_LOCALTIME_RZ (NETBSD_INSPIRED && USE_LTZ)
    119  1.36  christos # else
    120  1.36  christos #  define HAVE_LOCALTIME_RZ 0
    121  1.36  christos # endif
    122  1.36  christos #endif
    123  1.36  christos 
    124  1.36  christos #ifndef HAVE_TZSET
    125  1.36  christos # define HAVE_TZSET 1
    126  1.36  christos #endif
    127  1.27  christos 
    128  1.17   mlelstv #ifndef ZDUMP_LO_YEAR
    129  1.17   mlelstv #define ZDUMP_LO_YEAR	(-500)
    130  1.17   mlelstv #endif /* !defined ZDUMP_LO_YEAR */
    131  1.17   mlelstv 
    132  1.17   mlelstv #ifndef ZDUMP_HI_YEAR
    133  1.17   mlelstv #define ZDUMP_HI_YEAR	2500
    134  1.17   mlelstv #endif /* !defined ZDUMP_HI_YEAR */
    135   1.1       jtc 
    136   1.1       jtc #ifndef MAX_STRING_LENGTH
    137   1.1       jtc #define MAX_STRING_LENGTH	1024
    138   1.1       jtc #endif /* !defined MAX_STRING_LENGTH */
    139   1.1       jtc 
    140  1.36  christos #if __STDC_VERSION__ < 199901
    141  1.36  christos # define true 1
    142  1.36  christos # define false 0
    143  1.36  christos # define bool int
    144  1.36  christos #else
    145  1.36  christos # include <stdbool.h>
    146  1.36  christos #endif
    147   1.1       jtc 
    148   1.1       jtc #ifndef EXIT_SUCCESS
    149   1.1       jtc #define EXIT_SUCCESS	0
    150   1.1       jtc #endif /* !defined EXIT_SUCCESS */
    151   1.1       jtc 
    152   1.1       jtc #ifndef EXIT_FAILURE
    153   1.1       jtc #define EXIT_FAILURE	1
    154   1.1       jtc #endif /* !defined EXIT_FAILURE */
    155   1.1       jtc 
    156   1.1       jtc #ifndef SECSPERMIN
    157   1.1       jtc #define SECSPERMIN	60
    158   1.1       jtc #endif /* !defined SECSPERMIN */
    159   1.1       jtc 
    160   1.1       jtc #ifndef MINSPERHOUR
    161   1.1       jtc #define MINSPERHOUR	60
    162   1.1       jtc #endif /* !defined MINSPERHOUR */
    163   1.1       jtc 
    164   1.1       jtc #ifndef SECSPERHOUR
    165   1.1       jtc #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
    166   1.1       jtc #endif /* !defined SECSPERHOUR */
    167   1.1       jtc 
    168   1.1       jtc #ifndef HOURSPERDAY
    169   1.1       jtc #define HOURSPERDAY	24
    170   1.1       jtc #endif /* !defined HOURSPERDAY */
    171   1.1       jtc 
    172   1.1       jtc #ifndef EPOCH_YEAR
    173   1.1       jtc #define EPOCH_YEAR	1970
    174   1.1       jtc #endif /* !defined EPOCH_YEAR */
    175   1.1       jtc 
    176   1.1       jtc #ifndef TM_YEAR_BASE
    177   1.1       jtc #define TM_YEAR_BASE	1900
    178   1.1       jtc #endif /* !defined TM_YEAR_BASE */
    179   1.1       jtc 
    180   1.1       jtc #ifndef DAYSPERNYEAR
    181   1.1       jtc #define DAYSPERNYEAR	365
    182   1.1       jtc #endif /* !defined DAYSPERNYEAR */
    183   1.1       jtc 
    184   1.1       jtc #ifndef isleap
    185  1.17   mlelstv #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
    186   1.1       jtc #endif /* !defined isleap */
    187   1.1       jtc 
    188  1.17   mlelstv #ifndef isleap_sum
    189  1.17   mlelstv /*
    190  1.17   mlelstv ** See tzfile.h for details on isleap_sum.
    191  1.17   mlelstv */
    192  1.17   mlelstv #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
    193  1.17   mlelstv #endif /* !defined isleap_sum */
    194  1.17   mlelstv 
    195  1.29  christos #define SECSPERDAY	((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
    196  1.17   mlelstv #define SECSPERNYEAR	(SECSPERDAY * DAYSPERNYEAR)
    197  1.17   mlelstv #define SECSPERLYEAR	(SECSPERNYEAR + SECSPERDAY)
    198  1.31  christos #define SECSPER400YEARS	(SECSPERNYEAR * (intmax_t) (300 + 3)	\
    199  1.31  christos 			 + SECSPERLYEAR * (intmax_t) (100 - 3))
    200  1.31  christos 
    201  1.31  christos /*
    202  1.31  christos ** True if SECSPER400YEARS is known to be representable as an
    203  1.31  christos ** intmax_t.  It's OK that SECSPER400YEARS_FITS can in theory be false
    204  1.31  christos ** even if SECSPER400YEARS is representable, because when that happens
    205  1.31  christos ** the code merely runs a bit more slowly, and this slowness doesn't
    206  1.31  christos ** occur on any practical platform.
    207  1.31  christos */
    208  1.31  christos enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
    209  1.17   mlelstv 
    210  1.17   mlelstv #ifndef HAVE_GETTEXT
    211  1.17   mlelstv #define HAVE_GETTEXT 0
    212  1.17   mlelstv #endif
    213  1.17   mlelstv #if HAVE_GETTEXT
    214   1.3       jtc #include "locale.h"	/* for setlocale */
    215   1.3       jtc #include "libintl.h"
    216  1.17   mlelstv #endif /* HAVE_GETTEXT */
    217   1.3       jtc 
    218  1.29  christos #ifndef ATTRIBUTE_PURE
    219  1.26  christos #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
    220  1.29  christos # define ATTRIBUTE_PURE __attribute__ ((ATTRIBUTE_PURE__))
    221  1.26  christos #else
    222  1.29  christos # define ATTRIBUTE_PURE /* empty */
    223  1.26  christos #endif
    224  1.26  christos #endif
    225  1.26  christos 
    226   1.1       jtc #ifndef INITIALIZE
    227  1.35  christos #if defined(__GNUC__) || defined(__lint__)
    228   1.1       jtc #define INITIALIZE(x)	((x) = 0)
    229  1.35  christos #else /* !defined GNUC || lint */
    230   1.1       jtc #define INITIALIZE(x)
    231  1.35  christos #endif /* !defined GNUC || lint */
    232   1.1       jtc #endif /* !defined INITIALIZE */
    233   1.1       jtc 
    234   1.3       jtc /*
    235   1.3       jtc ** For the benefit of GNU folk...
    236  1.34  christos ** '_(MSGID)' uses the current locale's message library string for MSGID.
    237   1.3       jtc ** The default is to use gettext if available, and use MSGID otherwise.
    238   1.3       jtc */
    239   1.3       jtc 
    240   1.3       jtc #ifndef _
    241  1.17   mlelstv #if HAVE_GETTEXT
    242   1.3       jtc #define _(msgid) gettext(msgid)
    243  1.17   mlelstv #else /* !HAVE_GETTEXT */
    244  1.21  christos #define _(msgid) msgid
    245  1.17   mlelstv #endif /* !HAVE_GETTEXT */
    246   1.3       jtc #endif /* !defined _ */
    247   1.3       jtc 
    248  1.34  christos #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
    249  1.34  christos # define TZ_DOMAIN "tz"
    250  1.34  christos #endif
    251   1.3       jtc 
    252  1.36  christos #if ! HAVE_LOCALTIME_RZ
    253  1.36  christos # undef  timezone_t
    254  1.36  christos # define timezone_t char **
    255  1.36  christos #endif
    256  1.36  christos 
    257   1.1       jtc extern char **	environ;
    258  1.17   mlelstv extern int	getopt(int argc, char * const argv[],
    259  1.17   mlelstv 			const char * options);
    260   1.1       jtc extern char *	optarg;
    261   1.1       jtc extern int	optind;
    262   1.1       jtc 
    263  1.29  christos /* The minimum and maximum finite time values.  */
    264  1.29  christos static time_t	absolute_min_time =
    265  1.31  christos   ((time_t) -1 < 0
    266  1.29  christos     ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
    267  1.29  christos     : 0);
    268  1.29  christos static time_t	absolute_max_time =
    269  1.31  christos   ((time_t) -1 < 0
    270  1.31  christos     ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
    271  1.29  christos    : -1);
    272   1.5       jtc static size_t	longest;
    273   1.1       jtc static char *	progname;
    274  1.36  christos static bool	warned;
    275  1.36  christos static bool	errout;
    276  1.17   mlelstv 
    277  1.36  christos static char const *abbr(struct tm const *);
    278  1.36  christos static intmax_t	delta(struct tm *, struct tm *) ATTRIBUTE_PURE;
    279  1.36  christos static void dumptime(struct tm const *);
    280  1.36  christos static time_t hunt(timezone_t, char *, time_t, time_t);
    281  1.36  christos static void show(timezone_t, char *, time_t, bool);
    282  1.36  christos static const char *tformat(void);
    283  1.36  christos static time_t yeartot(intmax_t) ATTRIBUTE_PURE;
    284  1.17   mlelstv 
    285  1.34  christos /* Is A an alphabetic character in the C locale?  */
    286  1.36  christos static bool
    287  1.34  christos is_alpha(char a)
    288  1.34  christos {
    289  1.34  christos 	switch (a) {
    290  1.34  christos 	  default:
    291  1.36  christos 		return false;
    292  1.34  christos 	  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
    293  1.34  christos 	  case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
    294  1.34  christos 	  case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
    295  1.34  christos 	  case 'V': case 'W': case 'X': case 'Y': case 'Z':
    296  1.34  christos 	  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
    297  1.34  christos 	  case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
    298  1.34  christos 	  case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
    299  1.34  christos 	  case 'v': case 'w': case 'x': case 'y': case 'z':
    300  1.36  christos 	  	return true;
    301  1.34  christos 	}
    302  1.34  christos }
    303  1.34  christos 
    304  1.36  christos /* Return A + B, exiting if the result would overflow.  */
    305  1.36  christos static size_t
    306  1.36  christos sumsize(size_t a, size_t b)
    307  1.36  christos {
    308  1.36  christos 	size_t sum = a + b;
    309  1.36  christos 	if (sum < a)
    310  1.36  christos 		errx(EXIT_FAILURE, "size overflow");
    311  1.36  christos 	return sum;
    312  1.36  christos }
    313  1.36  christos 
    314  1.36  christos #if ! HAVE_TZSET
    315  1.36  christos # undef tzset
    316  1.36  christos # define tzset zdump_tzset
    317  1.36  christos static void tzset(void) { }
    318  1.36  christos #endif
    319  1.36  christos 
    320  1.36  christos /* Assume gmtime_r works if localtime_r does.
    321  1.36  christos    A replacement localtime_r is defined below if needed.  */
    322  1.36  christos #if ! HAVE_LOCALTIME_R
    323  1.36  christos 
    324  1.36  christos # undef gmtime_r
    325  1.36  christos # define gmtime_r zdump_gmtime_r
    326  1.36  christos 
    327  1.36  christos static struct tm *
    328  1.36  christos gmtime_r(time_t *tp, struct tm *tmp)
    329  1.36  christos {
    330  1.36  christos 	struct tm *r = gmtime(tp);
    331  1.36  christos 	if (r) {
    332  1.36  christos 		*tmp = *r;
    333  1.36  christos 		r = tmp;
    334  1.36  christos 	}
    335  1.36  christos 	return r;
    336  1.36  christos }
    337  1.36  christos 
    338  1.36  christos #endif
    339  1.36  christos 
    340  1.36  christos /* Platforms with TM_ZONE don't need tzname, so they can use the
    341  1.36  christos    faster localtime_rz or localtime_r if available.  */
    342  1.36  christos 
    343  1.36  christos #if defined TM_ZONE && HAVE_LOCALTIME_RZ
    344  1.36  christos # define USE_LOCALTIME_RZ true
    345  1.36  christos #else
    346  1.36  christos # define USE_LOCALTIME_RZ false
    347  1.36  christos #endif
    348  1.36  christos 
    349  1.36  christos #if ! USE_LOCALTIME_RZ
    350  1.36  christos 
    351  1.36  christos # if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET
    352  1.36  christos #  undef localtime_r
    353  1.36  christos #  define localtime_r zdump_localtime_r
    354  1.36  christos static struct tm *
    355  1.36  christos localtime_r(time_t *tp, struct tm *tmp)
    356  1.36  christos {
    357  1.36  christos 	struct tm *r = localtime(tp);
    358  1.36  christos 	if (r) {
    359  1.36  christos 		*tmp = *r;
    360  1.36  christos 		r = tmp;
    361  1.36  christos 	}
    362  1.36  christos 	return r;
    363  1.36  christos }
    364  1.36  christos # endif
    365  1.36  christos 
    366  1.36  christos # undef localtime_rz
    367  1.36  christos # define localtime_rz zdump_localtime_rz
    368  1.36  christos static struct tm *
    369  1.36  christos localtime_rz(timezone_t rz, time_t *tp, struct tm *tmp)
    370  1.36  christos {
    371  1.36  christos 	return localtime_r(tp, tmp);
    372  1.36  christos }
    373  1.36  christos 
    374  1.36  christos # ifdef TYPECHECK
    375  1.36  christos #  undef mktime_z
    376  1.36  christos #  define mktime_z zdump_mktime_z
    377  1.36  christos static time_t
    378  1.36  christos mktime_z(timezone_t tz, struct tm *tmp)
    379  1.36  christos {
    380  1.36  christos 	return mktime(tmp);
    381  1.36  christos }
    382  1.36  christos # endif
    383  1.36  christos 
    384  1.36  christos # undef tzalloc
    385  1.36  christos # undef tzfree
    386  1.36  christos # define tzalloc zdump_tzalloc
    387  1.36  christos # define tzfree zdump_tzfree
    388  1.36  christos 
    389  1.36  christos static timezone_t
    390  1.36  christos tzalloc(char const *val)
    391  1.36  christos {
    392  1.36  christos 	static char **fakeenv;
    393  1.36  christos 	char **env = fakeenv;
    394  1.36  christos 	char *env0;
    395  1.36  christos 	if (! env) {
    396  1.36  christos 		char **e = environ;
    397  1.36  christos 		int to;
    398  1.36  christos 
    399  1.36  christos 		while (*e++)
    400  1.36  christos 			continue;
    401  1.36  christos 		env = malloc(sumsize(sizeof *environ,
    402  1.36  christos 		    (e - environ) * sizeof *environ));
    403  1.36  christos 		if (! env) {
    404  1.36  christos 			err(EXIT_FAILURE, "malloc");
    405  1.36  christos 		}
    406  1.36  christos 		to = 1;
    407  1.36  christos 		for (e = environ; (env[to] = *e); e++)
    408  1.36  christos 			to += strncmp(*e, "TZ=", 3) != 0;
    409  1.36  christos 	}
    410  1.36  christos 	env0 = malloc(sumsize(sizeof "TZ=", strlen(val)));
    411  1.36  christos 	if (! env0) {
    412  1.36  christos 		err(EXIT_FAILURE, "malloc");
    413  1.36  christos 	}
    414  1.36  christos 	env[0] = strcat(strcpy(env0, "TZ="), val);
    415  1.36  christos 	environ = fakeenv = env;
    416  1.36  christos 	tzset();
    417  1.36  christos 	return env;
    418  1.36  christos }
    419  1.36  christos 
    420  1.36  christos static void
    421  1.36  christos tzfree(timezone_t env)
    422  1.36  christos {
    423  1.36  christos 	environ = env + 1;
    424  1.36  christos 	free(env[0]);
    425  1.36  christos }
    426  1.36  christos #endif /* ! USE_LOCALTIME_RZ */
    427  1.36  christos 
    428  1.36  christos /* A UTC time zone, and its initializer.  */
    429  1.36  christos static timezone_t gmtz;
    430  1.36  christos static void
    431  1.36  christos gmtzinit(void)
    432  1.36  christos {
    433  1.36  christos 	if (USE_LOCALTIME_RZ) {
    434  1.36  christos 		static char const utc[] = "UTC0";
    435  1.36  christos 		gmtz = tzalloc(utc);
    436  1.36  christos 		if (!gmtz) {
    437  1.36  christos 		      err(EXIT_FAILURE, "Cannot create %s", utc);
    438  1.36  christos 		}
    439  1.36  christos 	}
    440  1.36  christos }
    441  1.36  christos 
    442  1.36  christos /* Convert *TP to UTC, storing the broken-down time into *TMP.
    443  1.36  christos    Return TMP if successful, NULL otherwise.  This is like gmtime_r(TP, TMP),
    444  1.36  christos    except typically faster if USE_LOCALTIME_RZ.  */
    445  1.36  christos static struct tm *
    446  1.36  christos my_gmtime_r(time_t *tp, struct tm *tmp)
    447  1.36  christos {
    448  1.36  christos 	return USE_LOCALTIME_RZ ?
    449  1.36  christos 	    localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp);
    450  1.36  christos }
    451  1.36  christos 
    452  1.17   mlelstv #ifndef TYPECHECK
    453  1.36  christos #define my_localtime_rz	localtime_rz
    454  1.17   mlelstv #else /* !defined TYPECHECK */
    455  1.17   mlelstv static struct tm *
    456  1.36  christos my_localtime_rz(timezone_t tz, const time_t *tp, struct tm *tmp)
    457  1.17   mlelstv {
    458  1.36  christos 	tmp = localtime_rz(tz, tp, tmp);
    459  1.36  christos 	if (tmp) {
    460  1.17   mlelstv 		struct tm	tm;
    461  1.26  christos 		time_t	t;
    462  1.17   mlelstv 
    463  1.17   mlelstv 		tm = *tmp;
    464  1.36  christos 		t = mktime_z(tz, &tm);
    465  1.31  christos 		if (t != *tp) {
    466  1.17   mlelstv 			(void) fflush(stdout);
    467  1.17   mlelstv 			(void) fprintf(stderr, "\n%s: ", progname);
    468  1.17   mlelstv 			(void) fprintf(stderr, tformat(), *tp);
    469  1.17   mlelstv 			(void) fprintf(stderr, " ->");
    470  1.17   mlelstv 			(void) fprintf(stderr, " year=%d", tmp->tm_year);
    471  1.17   mlelstv 			(void) fprintf(stderr, " mon=%d", tmp->tm_mon);
    472  1.17   mlelstv 			(void) fprintf(stderr, " mday=%d", tmp->tm_mday);
    473  1.17   mlelstv 			(void) fprintf(stderr, " hour=%d", tmp->tm_hour);
    474  1.17   mlelstv 			(void) fprintf(stderr, " min=%d", tmp->tm_min);
    475  1.17   mlelstv 			(void) fprintf(stderr, " sec=%d", tmp->tm_sec);
    476  1.17   mlelstv 			(void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
    477  1.17   mlelstv 			(void) fprintf(stderr, " -> ");
    478  1.17   mlelstv 			(void) fprintf(stderr, tformat(), t);
    479  1.17   mlelstv 			(void) fprintf(stderr, "\n");
    480  1.36  christos 			errout = true;
    481  1.17   mlelstv 		}
    482  1.17   mlelstv 	}
    483  1.17   mlelstv 	return tmp;
    484  1.17   mlelstv }
    485  1.17   mlelstv #endif /* !defined TYPECHECK */
    486  1.17   mlelstv 
    487  1.17   mlelstv static void
    488  1.26  christos abbrok(const char *const abbrp, const char *const zone)
    489  1.17   mlelstv {
    490  1.26  christos 	const char *cp;
    491  1.26  christos 	const char *wp;
    492  1.17   mlelstv 
    493  1.17   mlelstv 	if (warned)
    494  1.17   mlelstv 		return;
    495  1.17   mlelstv 	cp = abbrp;
    496  1.17   mlelstv 	wp = NULL;
    497  1.34  christos 	while (is_alpha(*cp))
    498  1.17   mlelstv 		++cp;
    499  1.17   mlelstv 	if (cp - abbrp == 0)
    500  1.17   mlelstv 		wp = _("lacks alphabetic at start");
    501  1.17   mlelstv 	else if (cp - abbrp < 3)
    502  1.17   mlelstv 		wp = _("has fewer than 3 alphabetics");
    503  1.17   mlelstv 	else if (cp - abbrp > 6)
    504  1.17   mlelstv 		wp = _("has more than 6 alphabetics");
    505  1.17   mlelstv 	if (wp == NULL && (*cp == '+' || *cp == '-')) {
    506  1.17   mlelstv 		++cp;
    507  1.34  christos 		if ('0' <= *cp && *cp <= '9')
    508  1.34  christos 			if (*cp++ == '1' && '0' <= *cp && *cp <= '4')
    509  1.34  christos 				cp++;
    510  1.17   mlelstv 		if (*cp != '\0')
    511  1.17   mlelstv 			wp = _("differs from POSIX standard");
    512  1.17   mlelstv 	}
    513  1.17   mlelstv 	if (wp == NULL)
    514  1.17   mlelstv 		return;
    515  1.17   mlelstv 	(void) fflush(stdout);
    516  1.17   mlelstv 	(void) fprintf(stderr,
    517  1.17   mlelstv 		_("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
    518  1.17   mlelstv 		progname, zone, abbrp, wp);
    519  1.36  christos 	warned = errout = true;
    520  1.36  christos }
    521  1.36  christos 
    522  1.36  christos /* Return a time zone abbreviation.  If the abbreviation needs to be
    523  1.36  christos    saved, use *BUF (of size *BUFALLOC) to save it, and return the
    524  1.36  christos    abbreviation in the possibly-reallocated *BUF.  Otherwise, just
    525  1.36  christos    return the abbreviation.  Get the abbreviation from TMP.
    526  1.36  christos    Exit on memory allocation failure.  */
    527  1.36  christos static char const *
    528  1.36  christos saveabbr(char **buf, size_t *bufalloc, struct tm const *tmp)
    529  1.36  christos {
    530  1.36  christos 	char const *ab = abbr(tmp);
    531  1.36  christos 	if (HAVE_LOCALTIME_RZ)
    532  1.36  christos 		return ab;
    533  1.36  christos 	else {
    534  1.36  christos 		size_t ablen = strlen(ab);
    535  1.36  christos 		if (*bufalloc <= ablen) {
    536  1.36  christos 			free(*buf);
    537  1.36  christos 
    538  1.36  christos 			/* Make the new buffer at least twice as long as the
    539  1.36  christos 			   old, to avoid O(N**2) behavior on repeated calls.  */
    540  1.36  christos 			*bufalloc = sumsize(*bufalloc, ablen + 1);
    541  1.36  christos 			*buf = malloc(*bufalloc);
    542  1.36  christos 			if (! *buf) {
    543  1.36  christos 				err(EXIT_FAILURE, "malloc");
    544  1.36  christos 			}
    545  1.36  christos 		}
    546  1.36  christos 		return strcpy(*buf, ab);
    547  1.36  christos 	}
    548  1.36  christos }
    549  1.36  christos 
    550  1.36  christos static void
    551  1.36  christos close_file(FILE *stream)
    552  1.36  christos {
    553  1.36  christos 	char const *e = (ferror(stream) ? _("I/O error")
    554  1.36  christos 	    : fclose(stream) != 0 ? strerror(errno) : NULL);
    555  1.36  christos 	if (e) {
    556  1.36  christos 		errx(EXIT_FAILURE, "%s", e);
    557  1.36  christos 	}
    558  1.17   mlelstv }
    559  1.17   mlelstv 
    560  1.24     joerg __dead static void
    561  1.26  christos usage(FILE *const stream, const int status)
    562  1.17   mlelstv {
    563  1.17   mlelstv 	(void) fprintf(stream,
    564  1.29  christos _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
    565  1.29  christos   "\n"
    566  1.29  christos   "Report bugs to %s.\n"),
    567  1.28  christos 		       progname, progname, REPORT_BUGS_TO);
    568  1.36  christos 	if (status == EXIT_SUCCESS)
    569  1.36  christos 		close_file(stream);
    570  1.17   mlelstv 	exit(status);
    571  1.17   mlelstv }
    572   1.1       jtc 
    573   1.1       jtc int
    574  1.26  christos main(int argc, char *argv[])
    575  1.26  christos {
    576  1.36  christos 	/* These are static so that they're initially zero.  */
    577  1.36  christos 	static char *		abbrev;
    578  1.36  christos 	static size_t		abbrevsize;
    579  1.36  christos 	static struct tm	newtm;
    580  1.36  christos 
    581  1.26  christos 	int		i;
    582  1.36  christos 	bool		vflag;
    583  1.36  christos 	bool		Vflag;
    584  1.26  christos 	char *		cutarg;
    585  1.29  christos 	char *		cuttimes;
    586  1.26  christos 	time_t		cutlotime;
    587  1.26  christos 	time_t		cuthitime;
    588  1.26  christos 	time_t		now;
    589  1.26  christos 	time_t		t;
    590  1.26  christos 	time_t		newt;
    591  1.26  christos 	struct tm	tm;
    592  1.26  christos 	struct tm *	tmp;
    593  1.26  christos 	struct tm *	newtmp;
    594   1.1       jtc 
    595  1.29  christos 	cutlotime = absolute_min_time;
    596  1.29  christos 	cuthitime = absolute_max_time;
    597  1.17   mlelstv #if HAVE_GETTEXT
    598  1.17   mlelstv 	(void) setlocale(LC_ALL, "");
    599   1.3       jtc #ifdef TZ_DOMAINDIR
    600   1.3       jtc 	(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
    601  1.17   mlelstv #endif /* defined TEXTDOMAINDIR */
    602   1.3       jtc 	(void) textdomain(TZ_DOMAIN);
    603  1.17   mlelstv #endif /* HAVE_GETTEXT */
    604   1.1       jtc 	progname = argv[0];
    605  1.14    kleink 	for (i = 1; i < argc; ++i)
    606  1.14    kleink 		if (strcmp(argv[i], "--version") == 0) {
    607  1.28  christos 			(void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
    608  1.36  christos 			return EXIT_SUCCESS;
    609  1.17   mlelstv 		} else if (strcmp(argv[i], "--help") == 0) {
    610  1.22  christos 			usage(stdout, EXIT_SUCCESS);
    611  1.14    kleink 		}
    612  1.36  christos 	vflag = Vflag = false;
    613  1.29  christos 	cutarg = cuttimes = NULL;
    614  1.29  christos 	for (;;)
    615  1.29  christos 	  switch (getopt(argc, argv, "c:t:vV")) {
    616  1.29  christos 	  case 'c': cutarg = optarg; break;
    617  1.29  christos 	  case 't': cuttimes = optarg; break;
    618  1.36  christos 	  case 'v': vflag = true; break;
    619  1.36  christos 	  case 'V': Vflag = true; break;
    620  1.29  christos 	  case -1:
    621  1.29  christos 	    if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
    622  1.29  christos 	      goto arg_processing_done;
    623  1.29  christos 	    /* Fall through.  */
    624  1.29  christos 	  default:
    625  1.29  christos 	    usage(stderr, EXIT_FAILURE);
    626  1.29  christos 	  }
    627  1.29  christos  arg_processing_done:;
    628  1.29  christos 
    629  1.29  christos 	if (vflag | Vflag) {
    630  1.29  christos 		intmax_t	lo;
    631  1.29  christos 		intmax_t	hi;
    632  1.32  christos 		char *loend, *hiend;
    633  1.30  christos 		intmax_t cutloyear = ZDUMP_LO_YEAR;
    634  1.30  christos 		intmax_t cuthiyear = ZDUMP_HI_YEAR;
    635  1.17   mlelstv 		if (cutarg != NULL) {
    636  1.32  christos 			lo = strtoimax(cutarg, &loend, 10);
    637  1.32  christos 			if (cutarg != loend && !*loend) {
    638  1.32  christos 				hi = lo;
    639  1.32  christos 				cuthiyear = hi;
    640  1.32  christos 			} else if (cutarg != loend && *loend == ','
    641  1.32  christos 				   && (hi = strtoimax(loend + 1, &hiend, 10),
    642  1.32  christos 				       loend + 1 != hiend && !*hiend)) {
    643  1.32  christos 				cutloyear = lo;
    644  1.17   mlelstv 				cuthiyear = hi;
    645  1.17   mlelstv 			} else {
    646  1.36  christos 				fprintf(stderr, _("%s: wild -c argument %s\n"),
    647  1.17   mlelstv 					progname, cutarg);
    648  1.36  christos 				return EXIT_FAILURE;
    649  1.17   mlelstv 			}
    650  1.17   mlelstv 		}
    651  1.29  christos 		if (cutarg != NULL || cuttimes == NULL) {
    652  1.29  christos 			cutlotime = yeartot(cutloyear);
    653  1.29  christos 			cuthitime = yeartot(cuthiyear);
    654  1.29  christos 		}
    655  1.29  christos 		if (cuttimes != NULL) {
    656  1.32  christos 			lo = strtoimax(cuttimes, &loend, 10);
    657  1.32  christos 			if (cuttimes != loend && !*loend) {
    658  1.32  christos 				hi = lo;
    659  1.29  christos 				if (hi < cuthitime) {
    660  1.29  christos 					if (hi < absolute_min_time)
    661  1.29  christos 						hi = absolute_min_time;
    662  1.29  christos 					cuthitime = hi;
    663  1.29  christos 				}
    664  1.32  christos 			} else if (cuttimes != loend && *loend == ','
    665  1.32  christos 				   && (hi = strtoimax(loend + 1, &hiend, 10),
    666  1.32  christos 				       loend + 1 != hiend && !*hiend)) {
    667  1.29  christos 				if (cutlotime < lo) {
    668  1.29  christos 					if (absolute_max_time < lo)
    669  1.29  christos 						lo = absolute_max_time;
    670  1.29  christos 					cutlotime = lo;
    671  1.29  christos 				}
    672  1.29  christos 				if (hi < cuthitime) {
    673  1.29  christos 					if (hi < absolute_min_time)
    674  1.29  christos 						hi = absolute_min_time;
    675  1.29  christos 					cuthitime = hi;
    676  1.29  christos 				}
    677  1.29  christos 			} else {
    678  1.29  christos 				(void) fprintf(stderr,
    679  1.29  christos 					_("%s: wild -t argument %s\n"),
    680  1.29  christos 					progname, cuttimes);
    681  1.36  christos 				return EXIT_FAILURE;
    682  1.29  christos 			}
    683  1.29  christos 		}
    684   1.1       jtc 	}
    685  1.36  christos 	gmtzinit();
    686  1.36  christos 	now = time(NULL);
    687   1.1       jtc 	longest = 0;
    688  1.36  christos 	for (i = optind; i < argc; i++) {
    689  1.36  christos 		size_t arglen = strlen(argv[i]);
    690  1.36  christos 		if (longest < arglen)
    691  1.36  christos 			longest = arglen < INT_MAX ? arglen : INT_MAX;
    692  1.36  christos 	}
    693   1.1       jtc 
    694   1.1       jtc 	for (i = optind; i < argc; ++i) {
    695  1.36  christos 		timezone_t tz = tzalloc(argv[i]);
    696  1.36  christos 		char const *ab;
    697  1.36  christos 		if (!tz) {
    698  1.36  christos 			errx(EXIT_FAILURE, "%s", argv[i]);
    699  1.36  christos 		}
    700  1.29  christos 		if (! (vflag | Vflag)) {
    701  1.36  christos 			show(tz, argv[i], now, false);
    702  1.36  christos 			tzfree(tz);
    703   1.1       jtc 			continue;
    704   1.3       jtc 		}
    705  1.36  christos 		warned = false;
    706  1.17   mlelstv 		t = absolute_min_time;
    707  1.29  christos 		if (!Vflag) {
    708  1.36  christos 			show(tz, argv[i], t, true);
    709  1.31  christos 			t += SECSPERDAY;
    710  1.36  christos 			show(tz, argv[i], t, true);
    711  1.29  christos 		}
    712  1.17   mlelstv 		if (t < cutlotime)
    713  1.17   mlelstv 			t = cutlotime;
    714  1.36  christos 		tmp = my_localtime_rz(tz, &t, &tm);
    715  1.36  christos 		if (tmp)
    716  1.36  christos 			ab = saveabbr(&abbrev, &abbrevsize, &tm);
    717  1.36  christos 		else
    718  1.36  christos 			ab = NULL;
    719  1.36  christos 		while (t < cuthitime) {
    720  1.36  christos 			newt = ((t < absolute_max_time - SECSPERDAY / 2
    721  1.36  christos 				&& t + SECSPERDAY / 2 < cuthitime)
    722  1.31  christos 				? t + SECSPERDAY / 2
    723  1.36  christos 				: cuthitime);
    724  1.36  christos 			newtmp = localtime_rz(tz, &newt, &newtm);
    725  1.17   mlelstv 			if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
    726  1.36  christos 			    (delta(&newtm, &tm) != (newt - t) ||
    727  1.36  christos 			    newtm.tm_isdst != tm.tm_isdst ||
    728  1.36  christos 			    strcmp(abbr(&newtm), ab) != 0)) {
    729  1.36  christos 				newt = hunt(tz, argv[i], t, newt);
    730  1.36  christos 				newtmp = localtime_rz(tz, &newt, &newtm);
    731  1.36  christos 				if (newtmp)
    732  1.36  christos 					  ab = saveabbr(&abbrev, &abbrevsize,
    733  1.36  christos 							&newtm);
    734   1.1       jtc 			}
    735   1.1       jtc 			t = newt;
    736   1.1       jtc 			tm = newtm;
    737  1.17   mlelstv 			tmp = newtmp;
    738   1.1       jtc 		}
    739  1.29  christos 		if (!Vflag) {
    740  1.29  christos 			t = absolute_max_time;
    741  1.31  christos 			t -= SECSPERDAY;
    742  1.36  christos 			show(tz, argv[i], t, true);
    743  1.31  christos 			t += SECSPERDAY;
    744  1.36  christos 			show(tz, argv[i], t, true);
    745  1.29  christos 		}
    746  1.36  christos 		tzfree(tz);
    747   1.1       jtc 	}
    748  1.36  christos 	close_file(stdout);
    749  1.36  christos 	if (errout && (ferror(stderr) || fclose(stderr) != 0))
    750  1.36  christos 		return EXIT_FAILURE;
    751  1.36  christos 	return EXIT_SUCCESS;
    752  1.17   mlelstv }
    753   1.1       jtc 
    754   1.1       jtc static time_t
    755  1.26  christos yeartot(const long y)
    756  1.17   mlelstv {
    757  1.31  christos 	intmax_t	myy, seconds, years;
    758  1.29  christos 	time_t		t;
    759  1.17   mlelstv 
    760  1.17   mlelstv 	myy = EPOCH_YEAR;
    761  1.17   mlelstv 	t = 0;
    762  1.31  christos 	while (myy < y) {
    763  1.31  christos 		if (SECSPER400YEARS_FITS && 400 <= y - myy) {
    764  1.31  christos 			intmax_t diff400 = (y - myy) / 400;
    765  1.31  christos 			if (INTMAX_MAX / SECSPER400YEARS < diff400)
    766  1.31  christos 				return absolute_max_time;
    767  1.31  christos 			seconds = diff400 * SECSPER400YEARS;
    768  1.31  christos 			years = diff400 * 400;
    769  1.31  christos                 } else {
    770  1.17   mlelstv 			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
    771  1.31  christos 			years = 1;
    772  1.31  christos 		}
    773  1.31  christos 		myy += years;
    774  1.31  christos 		if (t > absolute_max_time - seconds)
    775  1.31  christos 			return absolute_max_time;
    776  1.31  christos 		t += seconds;
    777  1.31  christos 	}
    778  1.31  christos 	while (y < myy) {
    779  1.31  christos 		if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
    780  1.31  christos 			intmax_t diff400 = (myy - y) / 400;
    781  1.31  christos 			if (INTMAX_MAX / SECSPER400YEARS < diff400)
    782  1.31  christos 				return absolute_min_time;
    783  1.31  christos 			seconds = diff400 * SECSPER400YEARS;
    784  1.31  christos 			years = diff400 * 400;
    785  1.17   mlelstv 		} else {
    786  1.31  christos 			seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
    787  1.31  christos 			years = 1;
    788  1.17   mlelstv 		}
    789  1.31  christos 		myy -= years;
    790  1.31  christos 		if (t < absolute_min_time + seconds)
    791  1.31  christos 			return absolute_min_time;
    792  1.31  christos 		t -= seconds;
    793  1.17   mlelstv 	}
    794  1.17   mlelstv 	return t;
    795  1.17   mlelstv }
    796  1.17   mlelstv 
    797  1.17   mlelstv static time_t
    798  1.36  christos hunt(timezone_t tz, char *name, time_t lot, time_t hit)
    799  1.17   mlelstv {
    800  1.36  christos 	static char *		loab;
    801  1.36  christos 	static size_t		loabsize;
    802  1.36  christos 	char const *		ab;
    803  1.17   mlelstv 	time_t			t;
    804  1.17   mlelstv 	struct tm		lotm;
    805  1.26  christos 	struct tm *	lotmp;
    806  1.17   mlelstv 	struct tm		tm;
    807  1.26  christos 	struct tm *	tmp;
    808  1.17   mlelstv 
    809  1.36  christos 	lotmp = my_localtime_rz(tz, &lot, &lotm);
    810  1.36  christos 	if (lotmp)
    811  1.36  christos 		ab = saveabbr(&loab, &loabsize, &lotm);
    812  1.36  christos 	else
    813  1.36  christos 		ab = NULL;
    814  1.17   mlelstv 	for ( ; ; ) {
    815  1.29  christos 		time_t diff = hit - lot;
    816  1.17   mlelstv 		if (diff < 2)
    817  1.17   mlelstv 			break;
    818  1.17   mlelstv 		t = lot;
    819  1.17   mlelstv 		t += diff / 2;
    820   1.1       jtc 		if (t <= lot)
    821   1.1       jtc 			++t;
    822   1.1       jtc 		else if (t >= hit)
    823   1.1       jtc 			--t;
    824  1.36  christos 		tmp = my_localtime_rz(tz, &t, &tm);
    825  1.17   mlelstv 		if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
    826  1.17   mlelstv 			(delta(&tm, &lotm) == (t - lot) &&
    827   1.1       jtc 			tm.tm_isdst == lotm.tm_isdst &&
    828  1.36  christos 			strcmp(abbr(&tm), ab) == 0)) {
    829   1.1       jtc 				lot = t;
    830   1.1       jtc 				lotm = tm;
    831  1.17   mlelstv 				lotmp = tmp;
    832   1.1       jtc 		} else	hit = t;
    833   1.1       jtc 	}
    834  1.36  christos 	show(tz, name, lot, true);
    835  1.36  christos 	show(tz, name, hit, true);
    836   1.1       jtc 	return hit;
    837   1.1       jtc }
    838   1.1       jtc 
    839   1.1       jtc /*
    840  1.17   mlelstv ** Thanks to Paul Eggert for logic used in delta.
    841   1.1       jtc */
    842   1.1       jtc 
    843  1.29  christos static intmax_t
    844  1.26  christos delta(struct tm *newp, struct tm *oldp)
    845   1.1       jtc {
    846  1.29  christos 	intmax_t	result;
    847  1.29  christos 	int		tmy;
    848   1.1       jtc 
    849   1.1       jtc 	if (newp->tm_year < oldp->tm_year)
    850   1.1       jtc 		return -delta(oldp, newp);
    851   1.1       jtc 	result = 0;
    852   1.1       jtc 	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
    853  1.17   mlelstv 		result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
    854   1.1       jtc 	result += newp->tm_yday - oldp->tm_yday;
    855   1.1       jtc 	result *= HOURSPERDAY;
    856   1.1       jtc 	result += newp->tm_hour - oldp->tm_hour;
    857   1.1       jtc 	result *= MINSPERHOUR;
    858   1.1       jtc 	result += newp->tm_min - oldp->tm_min;
    859   1.1       jtc 	result *= SECSPERMIN;
    860   1.1       jtc 	result += newp->tm_sec - oldp->tm_sec;
    861   1.1       jtc 	return result;
    862   1.1       jtc }
    863   1.1       jtc 
    864  1.36  christos #ifndef TM_GMTOFF
    865  1.36  christos /* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday.
    866  1.36  christos   Assume A and B differ by at most one year.  */
    867  1.36  christos static int
    868  1.36  christos adjusted_yday(struct tm const *a, struct tm const *b)
    869  1.36  christos {
    870  1.36  christos 	int yday = a->tm_yday;
    871  1.36  christos 	if (b->tm_year < a->tm_year)
    872  1.36  christos 		yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE);
    873  1.36  christos 	return yday;
    874  1.36  christos }
    875  1.36  christos #endif
    876  1.36  christos 
    877  1.36  christos /* If A is the broken-down local time and B the broken-down UTC for
    878  1.36  christos    the same instant, return A's UTC offset in seconds, where positive
    879  1.36  christos    offsets are east of Greenwich.  On failure, return LONG_MIN.  */
    880  1.36  christos static long
    881  1.36  christos gmtoff(struct tm const *a, struct tm const *b)
    882  1.36  christos {
    883  1.36  christos #ifdef TM_GMTOFF
    884  1.36  christos 	return a->TM_GMTOFF;
    885  1.36  christos #else
    886  1.36  christos 	if (! b)
    887  1.36  christos 		return LONG_MIN;
    888  1.36  christos 	else {
    889  1.36  christos 		int ayday = adjusted_yday(a, b);
    890  1.36  christos 		int byday = adjusted_yday(b, a);
    891  1.36  christos 		int days = ayday - byday;
    892  1.36  christos 		long hours = a->tm_hour - b->tm_hour + 24 * days;
    893  1.36  christos 		long minutes = a->tm_min - b->tm_min + 60 * hours;
    894  1.36  christos 		long seconds = a->tm_sec - b->tm_sec + 60 * minutes;
    895  1.36  christos 		return seconds;
    896  1.36  christos 	}
    897  1.36  christos #endif
    898  1.36  christos }
    899  1.36  christos 
    900   1.1       jtc static void
    901  1.36  christos show(timezone_t tz, char *zone, time_t t, bool v)
    902   1.1       jtc {
    903  1.26  christos 	struct tm *	tmp;
    904  1.36  christos 	struct tm *	gmtmp;
    905  1.36  christos 	struct tm tm, gmtm;
    906   1.1       jtc 
    907   1.5       jtc 	(void) printf("%-*s  ", (int) longest, zone);
    908   1.1       jtc 	if (v) {
    909  1.36  christos 		gmtmp = my_gmtime_r(&t, &gmtm);
    910  1.36  christos 		if (gmtmp == NULL) {
    911  1.36  christos 			printf(tformat(), t);
    912  1.17   mlelstv 		} else {
    913  1.36  christos 			dumptime(gmtmp);
    914  1.31  christos 			(void) printf(" UT");
    915  1.17   mlelstv 		}
    916  1.17   mlelstv 		(void) printf(" = ");
    917  1.17   mlelstv 	}
    918  1.36  christos 	tmp = my_localtime_rz(tz, &t, &tm);
    919  1.17   mlelstv 	dumptime(tmp);
    920  1.17   mlelstv 	if (tmp != NULL) {
    921  1.17   mlelstv 		if (*abbr(tmp) != '\0')
    922  1.17   mlelstv 			(void) printf(" %s", abbr(tmp));
    923  1.17   mlelstv 		if (v) {
    924  1.36  christos 			long off = gmtoff(tmp, gmtmp);
    925  1.17   mlelstv 			(void) printf(" isdst=%d", tmp->tm_isdst);
    926  1.36  christos 			if (off != LONG_MIN)
    927  1.36  christos 				(void) printf(" gmtoff=%ld", off);
    928  1.17   mlelstv 		}
    929   1.1       jtc 	}
    930   1.1       jtc 	(void) printf("\n");
    931  1.17   mlelstv 	if (tmp != NULL && *abbr(tmp) != '\0')
    932  1.17   mlelstv 		abbrok(abbr(tmp), zone);
    933   1.1       jtc }
    934   1.1       jtc 
    935   1.9   mycroft static const char *
    936  1.36  christos abbr(struct tm const *tmp)
    937   1.1       jtc {
    938  1.36  christos #ifdef TM_ZONE
    939  1.36  christos 	return tmp->TM_ZONE;
    940  1.36  christos #else
    941  1.36  christos 	return (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst]
    942  1.36  christos 		? tzname[0 < tmp->tm_isdst]
    943  1.36  christos 		: "");
    944  1.36  christos #endif
    945   1.1       jtc }
    946  1.17   mlelstv 
    947  1.17   mlelstv /*
    948  1.17   mlelstv ** The code below can fail on certain theoretical systems;
    949  1.17   mlelstv ** it works on all known real-world systems as of 2004-12-30.
    950  1.17   mlelstv */
    951  1.17   mlelstv 
    952  1.17   mlelstv static const char *
    953  1.17   mlelstv tformat(void)
    954  1.17   mlelstv {
    955  1.17   mlelstv 	if (0 > (time_t) -1) {		/* signed */
    956  1.29  christos 		if (sizeof (time_t) == sizeof (intmax_t))
    957  1.29  christos 			return "%"PRIdMAX;
    958  1.17   mlelstv 		if (sizeof (time_t) > sizeof (long))
    959  1.17   mlelstv 			return "%lld";
    960  1.17   mlelstv 		if (sizeof (time_t) > sizeof (int))
    961  1.17   mlelstv 			return "%ld";
    962  1.17   mlelstv 		return "%d";
    963  1.17   mlelstv 	}
    964  1.29  christos #ifdef PRIuMAX
    965  1.29  christos 	if (sizeof (time_t) == sizeof (uintmax_t))
    966  1.29  christos 		return "%"PRIuMAX;
    967  1.29  christos #endif
    968  1.17   mlelstv 	if (sizeof (time_t) > sizeof (unsigned long))
    969  1.17   mlelstv 		return "%llu";
    970  1.17   mlelstv 	if (sizeof (time_t) > sizeof (unsigned int))
    971  1.17   mlelstv 		return "%lu";
    972  1.17   mlelstv 	return "%u";
    973  1.17   mlelstv }
    974  1.17   mlelstv 
    975  1.17   mlelstv static void
    976  1.26  christos dumptime(const struct tm *timeptr)
    977  1.17   mlelstv {
    978  1.17   mlelstv 	static const char	wday_name[][3] = {
    979  1.17   mlelstv 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    980  1.17   mlelstv 	};
    981  1.17   mlelstv 	static const char	mon_name[][3] = {
    982  1.17   mlelstv 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    983  1.17   mlelstv 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    984  1.17   mlelstv 	};
    985  1.26  christos 	const char *	wn;
    986  1.26  christos 	const char *	mn;
    987  1.26  christos 	int		lead;
    988  1.26  christos 	int		trail;
    989  1.17   mlelstv 
    990  1.17   mlelstv 	if (timeptr == NULL) {
    991  1.36  christos 		printf("NULL");
    992  1.17   mlelstv 		return;
    993  1.17   mlelstv 	}
    994  1.17   mlelstv 	/*
    995  1.36  christos 	** The packaged localtime_rz and gmtime_r never put out-of-range
    996  1.17   mlelstv 	** values in tm_wday or tm_mon, but since this code might be compiled
    997  1.17   mlelstv 	** with other (perhaps experimental) versions, paranoia is in order.
    998  1.17   mlelstv 	*/
    999  1.17   mlelstv 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
   1000  1.17   mlelstv 		(int) (sizeof wday_name / sizeof wday_name[0]))
   1001  1.17   mlelstv 			wn = "???";
   1002  1.17   mlelstv 	else		wn = wday_name[timeptr->tm_wday];
   1003  1.17   mlelstv 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
   1004  1.17   mlelstv 		(int) (sizeof mon_name / sizeof mon_name[0]))
   1005  1.17   mlelstv 			mn = "???";
   1006  1.17   mlelstv 	else		mn = mon_name[timeptr->tm_mon];
   1007  1.36  christos 	printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
   1008  1.17   mlelstv 		wn, mn,
   1009  1.17   mlelstv 		timeptr->tm_mday, timeptr->tm_hour,
   1010  1.17   mlelstv 		timeptr->tm_min, timeptr->tm_sec);
   1011  1.17   mlelstv #define DIVISOR	10
   1012  1.17   mlelstv 	trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
   1013  1.17   mlelstv 	lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
   1014  1.17   mlelstv 		trail / DIVISOR;
   1015  1.17   mlelstv 	trail %= DIVISOR;
   1016  1.17   mlelstv 	if (trail < 0 && lead > 0) {
   1017  1.17   mlelstv 		trail += DIVISOR;
   1018  1.17   mlelstv 		--lead;
   1019  1.17   mlelstv 	} else if (lead < 0 && trail > 0) {
   1020  1.17   mlelstv 		trail -= DIVISOR;
   1021  1.17   mlelstv 		++lead;
   1022  1.17   mlelstv 	}
   1023  1.17   mlelstv 	if (lead == 0)
   1024  1.36  christos 		printf("%d", trail);
   1025  1.36  christos 	else	printf("%d%d", lead, ((trail < 0) ? -trail : trail));
   1026  1.17   mlelstv }
   1027