2013-12-06 12:04:52 +01:00
|
|
|
/* $NetBSD: zdump.c,v 1.31 2013/09/20 19:06:54 christos Exp $ */
|
2011-02-14 20:36:03 +01:00
|
|
|
/*
|
|
|
|
** This file is in the public domain, so clarified as of
|
|
|
|
** 2009-05-17 by Arthur David Olson.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2013-12-06 12:04:52 +01:00
|
|
|
__RCSID("$NetBSD: zdump.c,v 1.31 2013/09/20 19:06:54 christos Exp $");
|
2011-02-14 20:36:03 +01:00
|
|
|
#endif /* !defined lint */
|
|
|
|
|
2012-11-15 12:06:41 +01:00
|
|
|
#include "version.h"
|
2011-02-14 20:36:03 +01:00
|
|
|
/*
|
|
|
|
** This code has been made independent of the rest of the time
|
|
|
|
** conversion package to increase confidence in the verification it provides.
|
|
|
|
** You can use this code to help in verifying other implementations.
|
2013-12-06 12:04:52 +01:00
|
|
|
**
|
|
|
|
** However, include private.h when debugging, so that it overrides
|
|
|
|
** time_t consistently with the rest of the package.
|
2011-02-14 20:36:03 +01:00
|
|
|
*/
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
#include "private.h"
|
|
|
|
|
2011-02-14 20:36:03 +01:00
|
|
|
#include "stdio.h" /* for stdout, stderr */
|
|
|
|
#include "string.h" /* for strcpy */
|
|
|
|
#include "sys/types.h" /* for time_t */
|
|
|
|
#include "time.h" /* for struct tm */
|
|
|
|
#include "stdlib.h" /* for exit, malloc, atoi */
|
|
|
|
#include <err.h>
|
|
|
|
#include "ctype.h" /* for isalpha et al. */
|
|
|
|
#ifndef isascii
|
|
|
|
#define isascii(x) 1
|
|
|
|
#endif /* !defined isascii */
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/*
|
|
|
|
** Substitutes for pre-C99 compilers.
|
|
|
|
** Much of this section of code is stolen from private.h.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HAVE_STDINT_H
|
|
|
|
# define HAVE_STDINT_H \
|
|
|
|
(199901 <= __STDC_VERSION__ || 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
|
|
|
|
#endif
|
|
|
|
#if HAVE_STDINT_H
|
|
|
|
# include "stdint.h"
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_INTTYPES_H
|
|
|
|
# define HAVE_INTTYPES_H HAVE_STDINT_H
|
|
|
|
#endif
|
|
|
|
#if HAVE_INTTYPES_H
|
|
|
|
# include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INT_FAST32_MAX
|
|
|
|
# if INT_MAX >> 31 == 0
|
|
|
|
typedef long int_fast32_t;
|
|
|
|
# else
|
|
|
|
typedef int int_fast32_t;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INTMAX_MAX
|
|
|
|
# if defined LLONG_MAX || defined __LONG_LONG_MAX__
|
|
|
|
typedef long long intmax_t;
|
|
|
|
# define PRIdMAX "lld"
|
|
|
|
# ifdef LLONG_MAX
|
|
|
|
# define INTMAX_MAX LLONG_MAX
|
|
|
|
# else
|
|
|
|
# define INTMAX_MAX __LONG_LONG_MAX__
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
typedef long intmax_t;
|
|
|
|
# define PRIdMAX "ld"
|
|
|
|
# define INTMAX_MAX LONG_MAX
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifndef SCNdMAX
|
|
|
|
# define SCNdMAX PRIdMAX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-02-14 20:36:03 +01:00
|
|
|
#ifndef ZDUMP_LO_YEAR
|
|
|
|
#define ZDUMP_LO_YEAR (-500)
|
|
|
|
#endif /* !defined ZDUMP_LO_YEAR */
|
|
|
|
|
|
|
|
#ifndef ZDUMP_HI_YEAR
|
|
|
|
#define ZDUMP_HI_YEAR 2500
|
|
|
|
#endif /* !defined ZDUMP_HI_YEAR */
|
|
|
|
|
|
|
|
#ifndef MAX_STRING_LENGTH
|
|
|
|
#define MAX_STRING_LENGTH 1024
|
|
|
|
#endif /* !defined MAX_STRING_LENGTH */
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif /* !defined TRUE */
|
|
|
|
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif /* !defined FALSE */
|
|
|
|
|
|
|
|
#ifndef EXIT_SUCCESS
|
|
|
|
#define EXIT_SUCCESS 0
|
|
|
|
#endif /* !defined EXIT_SUCCESS */
|
|
|
|
|
|
|
|
#ifndef EXIT_FAILURE
|
|
|
|
#define EXIT_FAILURE 1
|
|
|
|
#endif /* !defined EXIT_FAILURE */
|
|
|
|
|
|
|
|
#ifndef SECSPERMIN
|
|
|
|
#define SECSPERMIN 60
|
|
|
|
#endif /* !defined SECSPERMIN */
|
|
|
|
|
|
|
|
#ifndef MINSPERHOUR
|
|
|
|
#define MINSPERHOUR 60
|
|
|
|
#endif /* !defined MINSPERHOUR */
|
|
|
|
|
|
|
|
#ifndef SECSPERHOUR
|
|
|
|
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
|
|
|
|
#endif /* !defined SECSPERHOUR */
|
|
|
|
|
|
|
|
#ifndef HOURSPERDAY
|
|
|
|
#define HOURSPERDAY 24
|
|
|
|
#endif /* !defined HOURSPERDAY */
|
|
|
|
|
|
|
|
#ifndef EPOCH_YEAR
|
|
|
|
#define EPOCH_YEAR 1970
|
|
|
|
#endif /* !defined EPOCH_YEAR */
|
|
|
|
|
|
|
|
#ifndef TM_YEAR_BASE
|
|
|
|
#define TM_YEAR_BASE 1900
|
|
|
|
#endif /* !defined TM_YEAR_BASE */
|
|
|
|
|
|
|
|
#ifndef DAYSPERNYEAR
|
|
|
|
#define DAYSPERNYEAR 365
|
|
|
|
#endif /* !defined DAYSPERNYEAR */
|
|
|
|
|
|
|
|
#ifndef isleap
|
|
|
|
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
|
|
|
|
#endif /* !defined isleap */
|
|
|
|
|
|
|
|
#ifndef isleap_sum
|
|
|
|
/*
|
|
|
|
** See tzfile.h for details on isleap_sum.
|
|
|
|
*/
|
|
|
|
#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
|
|
|
|
#endif /* !defined isleap_sum */
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
|
2011-02-14 20:36:03 +01:00
|
|
|
#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
|
|
|
|
#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
|
2013-12-06 12:04:52 +01:00
|
|
|
#define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
|
|
|
|
+ SECSPERLYEAR * (intmax_t) (100 - 3))
|
|
|
|
|
|
|
|
/*
|
|
|
|
** True if SECSPER400YEARS is known to be representable as an
|
|
|
|
** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
|
|
|
|
** even if SECSPER400YEARS is representable, because when that happens
|
|
|
|
** the code merely runs a bit more slowly, and this slowness doesn't
|
|
|
|
** occur on any practical platform.
|
|
|
|
*/
|
|
|
|
enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
#ifndef HAVE_GETTEXT
|
|
|
|
#define HAVE_GETTEXT 0
|
|
|
|
#endif
|
|
|
|
#if HAVE_GETTEXT
|
|
|
|
#include "locale.h" /* for setlocale */
|
|
|
|
#include "libintl.h"
|
|
|
|
#endif /* HAVE_GETTEXT */
|
|
|
|
|
|
|
|
#ifndef GNUC_or_lint
|
|
|
|
#ifdef lint
|
|
|
|
#define GNUC_or_lint
|
|
|
|
#else /* !defined lint */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define GNUC_or_lint
|
|
|
|
#endif /* defined __GNUC__ */
|
|
|
|
#endif /* !defined lint */
|
|
|
|
#endif /* !defined GNUC_or_lint */
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
#ifndef ATTRIBUTE_PURE
|
|
|
|
#if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
|
|
|
|
# define ATTRIBUTE_PURE __attribute__ ((ATTRIBUTE_PURE__))
|
|
|
|
#else
|
|
|
|
# define ATTRIBUTE_PURE /* empty */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-02-14 20:36:03 +01:00
|
|
|
#ifndef INITIALIZE
|
|
|
|
#ifdef GNUC_or_lint
|
|
|
|
#define INITIALIZE(x) ((x) = 0)
|
|
|
|
#else /* !defined GNUC_or_lint */
|
|
|
|
#define INITIALIZE(x)
|
|
|
|
#endif /* !defined GNUC_or_lint */
|
|
|
|
#endif /* !defined INITIALIZE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
** For the benefit of GNU folk...
|
|
|
|
** `_(MSGID)' uses the current locale's message library string for MSGID.
|
|
|
|
** The default is to use gettext if available, and use MSGID otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _
|
|
|
|
#if HAVE_GETTEXT
|
|
|
|
#define _(msgid) gettext(msgid)
|
|
|
|
#else /* !HAVE_GETTEXT */
|
2012-11-15 12:06:41 +01:00
|
|
|
#define _(msgid) msgid
|
2011-02-14 20:36:03 +01:00
|
|
|
#endif /* !HAVE_GETTEXT */
|
|
|
|
#endif /* !defined _ */
|
|
|
|
|
|
|
|
#ifndef TZ_DOMAIN
|
|
|
|
#define TZ_DOMAIN "tz"
|
|
|
|
#endif /* !defined TZ_DOMAIN */
|
|
|
|
|
|
|
|
extern char ** environ;
|
|
|
|
extern int getopt(int argc, char * const argv[],
|
|
|
|
const char * options);
|
|
|
|
extern char * optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* The minimum and maximum finite time values. */
|
|
|
|
static time_t absolute_min_time =
|
|
|
|
((time_t) -1 < 0
|
|
|
|
? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
|
|
|
|
: 0);
|
|
|
|
static time_t absolute_max_time =
|
|
|
|
((time_t) -1 < 0
|
|
|
|
? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
|
|
|
|
: -1);
|
2011-02-14 20:36:03 +01:00
|
|
|
static size_t longest;
|
|
|
|
static char * progname;
|
|
|
|
static int warned;
|
|
|
|
|
|
|
|
static const char * abbr(struct tm * tmp);
|
|
|
|
static void abbrok(const char * abbrp, const char * zone);
|
2013-12-06 12:04:52 +01:00
|
|
|
static intmax_t delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
|
2011-02-14 20:36:03 +01:00
|
|
|
static void dumptime(const struct tm * tmp);
|
|
|
|
static time_t hunt(char * name, time_t lot, time_t hit);
|
|
|
|
static void show(char * zone, time_t t, int v);
|
|
|
|
static const char * tformat(void);
|
2013-12-06 12:04:52 +01:00
|
|
|
static time_t yeartot(long y) ATTRIBUTE_PURE;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
#ifndef TYPECHECK
|
|
|
|
#define my_localtime localtime
|
|
|
|
#else /* !defined TYPECHECK */
|
|
|
|
static struct tm *
|
2013-12-06 12:04:52 +01:00
|
|
|
my_localtime(time_t *tp)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
struct tm *tmp;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
tmp = localtime(tp);
|
|
|
|
if (tp != NULL && tmp != NULL) {
|
|
|
|
struct tm tm;
|
2013-12-06 12:04:52 +01:00
|
|
|
time_t t;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
tm = *tmp;
|
|
|
|
t = mktime(&tm);
|
2013-12-06 12:04:52 +01:00
|
|
|
if (t != *tp) {
|
2011-02-14 20:36:03 +01:00
|
|
|
(void) fflush(stdout);
|
|
|
|
(void) fprintf(stderr, "\n%s: ", progname);
|
|
|
|
(void) fprintf(stderr, tformat(), *tp);
|
|
|
|
(void) fprintf(stderr, " ->");
|
|
|
|
(void) fprintf(stderr, " year=%d", tmp->tm_year);
|
|
|
|
(void) fprintf(stderr, " mon=%d", tmp->tm_mon);
|
|
|
|
(void) fprintf(stderr, " mday=%d", tmp->tm_mday);
|
|
|
|
(void) fprintf(stderr, " hour=%d", tmp->tm_hour);
|
|
|
|
(void) fprintf(stderr, " min=%d", tmp->tm_min);
|
|
|
|
(void) fprintf(stderr, " sec=%d", tmp->tm_sec);
|
|
|
|
(void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
|
|
|
|
(void) fprintf(stderr, " -> ");
|
|
|
|
(void) fprintf(stderr, tformat(), t);
|
|
|
|
(void) fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
#endif /* !defined TYPECHECK */
|
|
|
|
|
|
|
|
static void
|
2013-12-06 12:04:52 +01:00
|
|
|
abbrok(const char *const abbrp, const char *const zone)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
const char *cp;
|
|
|
|
const char *wp;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
if (warned)
|
|
|
|
return;
|
|
|
|
cp = abbrp;
|
|
|
|
wp = NULL;
|
|
|
|
while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
|
|
|
|
++cp;
|
|
|
|
if (cp - abbrp == 0)
|
|
|
|
wp = _("lacks alphabetic at start");
|
|
|
|
else if (cp - abbrp < 3)
|
|
|
|
wp = _("has fewer than 3 alphabetics");
|
|
|
|
else if (cp - abbrp > 6)
|
|
|
|
wp = _("has more than 6 alphabetics");
|
|
|
|
if (wp == NULL && (*cp == '+' || *cp == '-')) {
|
|
|
|
++cp;
|
|
|
|
if (isascii((unsigned char) *cp) &&
|
|
|
|
isdigit((unsigned char) *cp))
|
|
|
|
if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
|
|
|
|
++cp;
|
|
|
|
if (*cp != '\0')
|
|
|
|
wp = _("differs from POSIX standard");
|
|
|
|
}
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
(void) fflush(stdout);
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
_("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
|
|
|
|
progname, zone, abbrp, wp);
|
|
|
|
warned = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-11-15 12:06:41 +01:00
|
|
|
__dead static void
|
2013-12-06 12:04:52 +01:00
|
|
|
usage(FILE *const stream, const int status)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
|
|
|
(void) fprintf(stream,
|
2013-12-06 12:04:52 +01:00
|
|
|
_("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
|
|
|
|
"\n"
|
|
|
|
"Report bugs to %s.\n"),
|
|
|
|
progname, progname, REPORT_BUGS_TO);
|
2011-02-14 20:36:03 +01:00
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-12-06 12:04:52 +01:00
|
|
|
main(int argc, char *argv[])
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
int i;
|
|
|
|
int vflag;
|
|
|
|
int Vflag;
|
|
|
|
char * cutarg;
|
|
|
|
char * cuttimes;
|
|
|
|
time_t cutlotime;
|
|
|
|
time_t cuthitime;
|
|
|
|
char ** fakeenv;
|
|
|
|
time_t now;
|
|
|
|
time_t t;
|
|
|
|
time_t newt;
|
|
|
|
struct tm tm;
|
|
|
|
struct tm newtm;
|
|
|
|
struct tm * tmp;
|
|
|
|
struct tm * newtmp;
|
|
|
|
|
|
|
|
cutlotime = absolute_min_time;
|
|
|
|
cuthitime = absolute_max_time;
|
2011-02-14 20:36:03 +01:00
|
|
|
#if HAVE_GETTEXT
|
|
|
|
(void) setlocale(LC_ALL, "");
|
|
|
|
#ifdef TZ_DOMAINDIR
|
|
|
|
(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
|
|
|
|
#endif /* defined TEXTDOMAINDIR */
|
|
|
|
(void) textdomain(TZ_DOMAIN);
|
|
|
|
#endif /* HAVE_GETTEXT */
|
|
|
|
progname = argv[0];
|
|
|
|
for (i = 1; i < argc; ++i)
|
|
|
|
if (strcmp(argv[i], "--version") == 0) {
|
2013-12-06 12:04:52 +01:00
|
|
|
(void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
|
2011-02-14 20:36:03 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (strcmp(argv[i], "--help") == 0) {
|
2012-11-15 12:06:41 +01:00
|
|
|
usage(stdout, EXIT_SUCCESS);
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
vflag = Vflag = 0;
|
|
|
|
cutarg = cuttimes = NULL;
|
|
|
|
for (;;)
|
|
|
|
switch (getopt(argc, argv, "c:t:vV")) {
|
|
|
|
case 'c': cutarg = optarg; break;
|
|
|
|
case 't': cuttimes = optarg; break;
|
|
|
|
case 'v': vflag = 1; break;
|
|
|
|
case 'V': Vflag = 1; break;
|
|
|
|
case -1:
|
|
|
|
if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
|
|
|
|
goto arg_processing_done;
|
|
|
|
/* Fall through. */
|
|
|
|
default:
|
|
|
|
usage(stderr, EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
arg_processing_done:;
|
|
|
|
|
|
|
|
if (vflag | Vflag) {
|
|
|
|
intmax_t lo;
|
|
|
|
intmax_t hi;
|
|
|
|
char dummy;
|
|
|
|
intmax_t cutloyear = ZDUMP_LO_YEAR;
|
|
|
|
intmax_t cuthiyear = ZDUMP_HI_YEAR;
|
2011-02-14 20:36:03 +01:00
|
|
|
if (cutarg != NULL) {
|
2013-12-06 12:04:52 +01:00
|
|
|
if (sscanf(cutarg, "%"SCNdMAX"%c", &hi, &dummy) == 1) {
|
2011-02-14 20:36:03 +01:00
|
|
|
cuthiyear = hi;
|
2013-12-06 12:04:52 +01:00
|
|
|
} else if (sscanf(cutarg, "%"SCNdMAX",%"SCNdMAX"%c",
|
2011-02-14 20:36:03 +01:00
|
|
|
&lo, &hi, &dummy) == 2) {
|
|
|
|
cutloyear = lo;
|
|
|
|
cuthiyear = hi;
|
|
|
|
} else {
|
|
|
|
(void) fprintf(stderr, _("%s: wild -c argument %s\n"),
|
|
|
|
progname, cutarg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
if (cutarg != NULL || cuttimes == NULL) {
|
|
|
|
cutlotime = yeartot(cutloyear);
|
|
|
|
cuthitime = yeartot(cuthiyear);
|
|
|
|
}
|
|
|
|
if (cuttimes != NULL) {
|
|
|
|
if (sscanf(cuttimes, "%"SCNdMAX"%c", &hi, &dummy) == 1) {
|
|
|
|
if (hi < cuthitime) {
|
|
|
|
if (hi < absolute_min_time)
|
|
|
|
hi = absolute_min_time;
|
|
|
|
cuthitime = hi;
|
|
|
|
}
|
|
|
|
} else if (sscanf(cuttimes, "%"SCNdMAX",%"SCNdMAX"%c",
|
|
|
|
&lo, &hi, &dummy) == 2) {
|
|
|
|
if (cutlotime < lo) {
|
|
|
|
if (absolute_max_time < lo)
|
|
|
|
lo = absolute_max_time;
|
|
|
|
cutlotime = lo;
|
|
|
|
}
|
|
|
|
if (hi < cuthitime) {
|
|
|
|
if (hi < absolute_min_time)
|
|
|
|
hi = absolute_min_time;
|
|
|
|
cuthitime = hi;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
_("%s: wild -t argument %s\n"),
|
|
|
|
progname, cuttimes);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
(void) time(&now);
|
|
|
|
longest = 0;
|
|
|
|
for (i = optind; i < argc; ++i)
|
|
|
|
if (strlen(argv[i]) > longest)
|
|
|
|
longest = strlen(argv[i]);
|
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
int from;
|
|
|
|
int to;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
for (i = 0; environ[i] != NULL; ++i)
|
|
|
|
continue;
|
2013-12-06 12:04:52 +01:00
|
|
|
fakeenv = malloc((i + 2) * sizeof *fakeenv);
|
2011-02-14 20:36:03 +01:00
|
|
|
if (fakeenv == NULL ||
|
2013-12-06 12:04:52 +01:00
|
|
|
(fakeenv[0] = malloc(longest + 4)) == NULL) {
|
2011-02-14 20:36:03 +01:00
|
|
|
err(EXIT_FAILURE, "Can't allocated %zu bytes",
|
|
|
|
longest + 4);
|
|
|
|
}
|
|
|
|
to = 0;
|
|
|
|
(void)strcpy(fakeenv[to++], "TZ="); /* XXX strcpy is safe */
|
|
|
|
for (from = 0; environ[from] != NULL; ++from)
|
|
|
|
if (strncmp(environ[from], "TZ=", 3) != 0)
|
|
|
|
fakeenv[to++] = environ[from];
|
|
|
|
fakeenv[to] = NULL;
|
|
|
|
environ = fakeenv;
|
|
|
|
}
|
|
|
|
for (i = optind; i < argc; ++i) {
|
|
|
|
static char buf[MAX_STRING_LENGTH];
|
|
|
|
|
|
|
|
(void) strcpy(&fakeenv[0][3], argv[i]); /* XXX strcpy is safe */
|
2013-12-06 12:04:52 +01:00
|
|
|
if (! (vflag | Vflag)) {
|
2011-02-14 20:36:03 +01:00
|
|
|
show(argv[i], now, FALSE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
warned = FALSE;
|
|
|
|
t = absolute_min_time;
|
2013-12-06 12:04:52 +01:00
|
|
|
if (!Vflag) {
|
|
|
|
show(argv[i], t, TRUE);
|
|
|
|
t += SECSPERDAY;
|
|
|
|
show(argv[i], t, TRUE);
|
|
|
|
}
|
2011-02-14 20:36:03 +01:00
|
|
|
if (t < cutlotime)
|
|
|
|
t = cutlotime;
|
|
|
|
tmp = my_localtime(&t);
|
|
|
|
if (tmp != NULL) {
|
|
|
|
tm = *tmp;
|
|
|
|
(void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
|
|
|
|
}
|
|
|
|
for ( ; ; ) {
|
2013-12-06 12:04:52 +01:00
|
|
|
newt = (t < absolute_max_time - SECSPERDAY / 2
|
|
|
|
? t + SECSPERDAY / 2
|
|
|
|
: absolute_max_time);
|
|
|
|
if (cuthitime <= newt)
|
2011-02-14 20:36:03 +01:00
|
|
|
break;
|
|
|
|
newtmp = localtime(&newt);
|
|
|
|
if (newtmp != NULL)
|
|
|
|
newtm = *newtmp;
|
|
|
|
if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
|
|
|
|
(delta(&newtm, &tm) != (newt - t) ||
|
|
|
|
newtm.tm_isdst != tm.tm_isdst ||
|
|
|
|
strcmp(abbr(&newtm), buf) != 0)) {
|
|
|
|
newt = hunt(argv[i], t, newt);
|
|
|
|
newtmp = localtime(&newt);
|
|
|
|
if (newtmp != NULL) {
|
|
|
|
newtm = *newtmp;
|
|
|
|
(void) strncpy(buf,
|
|
|
|
abbr(&newtm),
|
|
|
|
(sizeof buf) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t = newt;
|
|
|
|
tm = newtm;
|
|
|
|
tmp = newtmp;
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
if (!Vflag) {
|
|
|
|
t = absolute_max_time;
|
|
|
|
t -= SECSPERDAY;
|
|
|
|
show(argv[i], t, TRUE);
|
|
|
|
t += SECSPERDAY;
|
|
|
|
show(argv[i], t, TRUE);
|
|
|
|
}
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
if (fflush(stdout) || ferror(stdout)) {
|
|
|
|
err(EXIT_FAILURE, _("Error writing standard output"));
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
/* If exit fails to exit... */
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-11-15 12:06:41 +01:00
|
|
|
static time_t
|
2013-12-06 12:04:52 +01:00
|
|
|
yeartot(const long y)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
intmax_t myy, seconds, years;
|
|
|
|
time_t t;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
myy = EPOCH_YEAR;
|
|
|
|
t = 0;
|
2013-12-06 12:04:52 +01:00
|
|
|
while (myy < y) {
|
|
|
|
if (SECSPER400YEARS_FITS && 400 <= y - myy) {
|
|
|
|
intmax_t diff400 = (y - myy) / 400;
|
|
|
|
if (INTMAX_MAX / SECSPER400YEARS < diff400)
|
|
|
|
return absolute_max_time;
|
|
|
|
seconds = diff400 * SECSPER400YEARS;
|
|
|
|
years = diff400 * 400;
|
|
|
|
} else {
|
2011-02-14 20:36:03 +01:00
|
|
|
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
|
2013-12-06 12:04:52 +01:00
|
|
|
years = 1;
|
|
|
|
}
|
|
|
|
myy += years;
|
|
|
|
if (t > absolute_max_time - seconds)
|
|
|
|
return absolute_max_time;
|
|
|
|
t += seconds;
|
|
|
|
}
|
|
|
|
while (y < myy) {
|
|
|
|
if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
|
|
|
|
intmax_t diff400 = (myy - y) / 400;
|
|
|
|
if (INTMAX_MAX / SECSPER400YEARS < diff400)
|
|
|
|
return absolute_min_time;
|
|
|
|
seconds = diff400 * SECSPER400YEARS;
|
|
|
|
years = diff400 * 400;
|
2011-02-14 20:36:03 +01:00
|
|
|
} else {
|
2013-12-06 12:04:52 +01:00
|
|
|
seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
|
|
|
|
years = 1;
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
myy -= years;
|
|
|
|
if (t < absolute_min_time + seconds)
|
|
|
|
return absolute_min_time;
|
|
|
|
t -= seconds;
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static time_t
|
|
|
|
hunt(char *name, time_t lot, time_t hit)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
struct tm lotm;
|
2013-12-06 12:04:52 +01:00
|
|
|
struct tm * lotmp;
|
2011-02-14 20:36:03 +01:00
|
|
|
struct tm tm;
|
2013-12-06 12:04:52 +01:00
|
|
|
struct tm * tmp;
|
2011-02-14 20:36:03 +01:00
|
|
|
char loab[MAX_STRING_LENGTH];
|
|
|
|
|
|
|
|
lotmp = my_localtime(&lot);
|
|
|
|
if (lotmp != NULL) {
|
|
|
|
lotm = *lotmp;
|
|
|
|
(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
|
|
|
|
}
|
|
|
|
for ( ; ; ) {
|
2013-12-06 12:04:52 +01:00
|
|
|
time_t diff = hit - lot;
|
2011-02-14 20:36:03 +01:00
|
|
|
if (diff < 2)
|
|
|
|
break;
|
|
|
|
t = lot;
|
|
|
|
t += diff / 2;
|
|
|
|
if (t <= lot)
|
|
|
|
++t;
|
|
|
|
else if (t >= hit)
|
|
|
|
--t;
|
|
|
|
tmp = my_localtime(&t);
|
|
|
|
if (tmp != NULL)
|
|
|
|
tm = *tmp;
|
|
|
|
if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
|
|
|
|
(delta(&tm, &lotm) == (t - lot) &&
|
|
|
|
tm.tm_isdst == lotm.tm_isdst &&
|
|
|
|
strcmp(abbr(&tm), loab) == 0)) {
|
|
|
|
lot = t;
|
|
|
|
lotm = tm;
|
|
|
|
lotmp = tmp;
|
|
|
|
} else hit = t;
|
|
|
|
}
|
|
|
|
show(name, lot, TRUE);
|
|
|
|
show(name, hit, TRUE);
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Thanks to Paul Eggert for logic used in delta.
|
|
|
|
*/
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
static intmax_t
|
|
|
|
delta(struct tm *newp, struct tm *oldp)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
intmax_t result;
|
|
|
|
int tmy;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
if (newp->tm_year < oldp->tm_year)
|
|
|
|
return -delta(oldp, newp);
|
|
|
|
result = 0;
|
|
|
|
for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
|
|
|
|
result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
|
|
|
|
result += newp->tm_yday - oldp->tm_yday;
|
|
|
|
result *= HOURSPERDAY;
|
|
|
|
result += newp->tm_hour - oldp->tm_hour;
|
|
|
|
result *= MINSPERHOUR;
|
|
|
|
result += newp->tm_min - oldp->tm_min;
|
|
|
|
result *= SECSPERMIN;
|
|
|
|
result += newp->tm_sec - oldp->tm_sec;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show(char *zone, time_t t, int v)
|
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
struct tm * tmp;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
(void) printf("%-*s ", (int) longest, zone);
|
|
|
|
if (v) {
|
|
|
|
tmp = gmtime(&t);
|
|
|
|
if (tmp == NULL) {
|
|
|
|
(void) printf(tformat(), t);
|
|
|
|
} else {
|
|
|
|
dumptime(tmp);
|
2013-12-06 12:04:52 +01:00
|
|
|
(void) printf(" UT");
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
(void) printf(" = ");
|
|
|
|
}
|
|
|
|
tmp = my_localtime(&t);
|
|
|
|
dumptime(tmp);
|
|
|
|
if (tmp != NULL) {
|
|
|
|
if (*abbr(tmp) != '\0')
|
|
|
|
(void) printf(" %s", abbr(tmp));
|
|
|
|
if (v) {
|
|
|
|
(void) printf(" isdst=%d", tmp->tm_isdst);
|
|
|
|
#ifdef TM_GMTOFF
|
|
|
|
(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
|
|
|
|
#endif /* defined TM_GMTOFF */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void) printf("\n");
|
|
|
|
if (tmp != NULL && *abbr(tmp) != '\0')
|
|
|
|
abbrok(abbr(tmp), zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2013-12-06 12:04:52 +01:00
|
|
|
abbr(struct tm *tmp)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
const char * result;
|
2011-02-14 20:36:03 +01:00
|
|
|
static const char nada;
|
|
|
|
|
|
|
|
if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
|
|
|
|
return &nada;
|
|
|
|
result = tzname[tmp->tm_isdst];
|
|
|
|
return (result == NULL) ? &nada : result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The code below can fail on certain theoretical systems;
|
|
|
|
** it works on all known real-world systems as of 2004-12-30.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
tformat(void)
|
|
|
|
{
|
|
|
|
if (0 > (time_t) -1) { /* signed */
|
2013-12-06 12:04:52 +01:00
|
|
|
if (sizeof (time_t) == sizeof (intmax_t))
|
|
|
|
return "%"PRIdMAX;
|
2011-02-14 20:36:03 +01:00
|
|
|
if (sizeof (time_t) > sizeof (long))
|
|
|
|
return "%lld";
|
|
|
|
if (sizeof (time_t) > sizeof (int))
|
|
|
|
return "%ld";
|
|
|
|
return "%d";
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
#ifdef PRIuMAX
|
|
|
|
if (sizeof (time_t) == sizeof (uintmax_t))
|
|
|
|
return "%"PRIuMAX;
|
|
|
|
#endif
|
2011-02-14 20:36:03 +01:00
|
|
|
if (sizeof (time_t) > sizeof (unsigned long))
|
|
|
|
return "%llu";
|
|
|
|
if (sizeof (time_t) > sizeof (unsigned int))
|
|
|
|
return "%lu";
|
|
|
|
return "%u";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-06 12:04:52 +01:00
|
|
|
dumptime(const struct tm *timeptr)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
|
|
|
static const char wday_name[][3] = {
|
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
|
};
|
|
|
|
static const char mon_name[][3] = {
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
};
|
2013-12-06 12:04:52 +01:00
|
|
|
const char * wn;
|
|
|
|
const char * mn;
|
|
|
|
int lead;
|
|
|
|
int trail;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
if (timeptr == NULL) {
|
|
|
|
(void) printf("NULL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
** The packaged versions of localtime and gmtime never put out-of-range
|
|
|
|
** values in tm_wday or tm_mon, but since this code might be compiled
|
|
|
|
** with other (perhaps experimental) versions, paranoia is in order.
|
|
|
|
*/
|
|
|
|
if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
|
|
|
|
(int) (sizeof wday_name / sizeof wday_name[0]))
|
|
|
|
wn = "???";
|
|
|
|
else wn = wday_name[timeptr->tm_wday];
|
|
|
|
if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
|
|
|
|
(int) (sizeof mon_name / sizeof mon_name[0]))
|
|
|
|
mn = "???";
|
|
|
|
else mn = mon_name[timeptr->tm_mon];
|
|
|
|
(void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
|
|
|
|
wn, mn,
|
|
|
|
timeptr->tm_mday, timeptr->tm_hour,
|
|
|
|
timeptr->tm_min, timeptr->tm_sec);
|
|
|
|
#define DIVISOR 10
|
|
|
|
trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
|
|
|
|
lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
|
|
|
|
trail / DIVISOR;
|
|
|
|
trail %= DIVISOR;
|
|
|
|
if (trail < 0 && lead > 0) {
|
|
|
|
trail += DIVISOR;
|
|
|
|
--lead;
|
|
|
|
} else if (lead < 0 && trail > 0) {
|
|
|
|
trail -= DIVISOR;
|
|
|
|
++lead;
|
|
|
|
}
|
|
|
|
if (lead == 0)
|
|
|
|
(void) printf("%d", trail);
|
|
|
|
else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
|
|
|
|
}
|