Files
amiga-tz/zdump.c
T

681 lines
16 KiB
C
Raw Normal View History

2009-05-17 14:28:27 -04:00
/*
** This file is in the public domain, so clarified as of
** 2009-05-17 by Arthur David Olson.
*/
1988-02-18 04:32:17 -05:00
static char elsieid[] = "%W%";
1986-01-16 11:00:21 -05:00
1989-02-23 09:01:38 -05:00
/*
1989-03-22 19:56:23 -05:00
** This code has been made independent of the rest of the time
1989-02-23 09:01:38 -05:00
** conversion package to increase confidence in the verification it provides.
** You can use this code to help in verifying other implementations.
*/
1995-01-03 11:08:23 -05:00
#include "stdio.h" /* for stdout, stderr, perror */
1989-03-22 19:56:23 -05:00
#include "string.h" /* for strcpy */
#include "sys/types.h" /* for time_t */
#include "time.h" /* for struct tm */
1995-01-03 11:34:03 -05:00
#include "stdlib.h" /* for exit, malloc, atoi */
2004-11-29 12:47:51 -05:00
#include "float.h" /* for FLT_MAX and DBL_MAX */
2005-11-28 14:56:51 -05:00
#include "ctype.h" /* for isalpha et al. */
#ifndef isascii
#define isascii(x) 1
2006-02-16 11:16:57 -05:00
#endif /* !defined isascii */
1995-01-03 11:08:23 -05:00
2004-11-18 13:04:19 -05:00
#ifndef ZDUMP_LO_YEAR
2004-11-22 10:46:46 -05:00
#define ZDUMP_LO_YEAR (-500)
2004-11-18 13:04:19 -05:00
#endif /* !defined ZDUMP_LO_YEAR */
#ifndef ZDUMP_HI_YEAR
2004-11-22 10:46:46 -05:00
#define ZDUMP_HI_YEAR 2500
2004-11-18 13:04:19 -05:00
#endif /* !defined ZDUMP_HI_YEAR */
1989-03-22 19:56:23 -05:00
#ifndef MAX_STRING_LENGTH
#define MAX_STRING_LENGTH 1024
#endif /* !defined MAX_STRING_LENGTH */
1986-01-16 11:00:21 -05:00
1986-03-04 10:38:47 -05:00
#ifndef TRUE
1986-03-06 09:49:36 -05:00
#define TRUE 1
1989-02-23 09:01:38 -05:00
#endif /* !defined TRUE */
#ifndef FALSE
1986-03-06 09:49:36 -05:00
#define FALSE 0
1989-03-22 19:56:23 -05:00
#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 */
1986-03-04 10:38:47 -05:00
1989-02-23 09:01:38 -05:00
#ifndef SECSPERMIN
#define SECSPERMIN 60
#endif /* !defined SECSPERMIN */
1993-08-28 12:13:24 -04:00
#ifndef MINSPERHOUR
#define MINSPERHOUR 60
#endif /* !defined MINSPERHOUR */
1989-02-23 09:01:38 -05:00
#ifndef SECSPERHOUR
1993-08-28 12:13:24 -04:00
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
1989-02-23 09:01:38 -05:00
#endif /* !defined SECSPERHOUR */
#ifndef HOURSPERDAY
#define HOURSPERDAY 24
#endif /* !defined HOURSPERDAY */
#ifndef EPOCH_YEAR
#define EPOCH_YEAR 1970
#endif /* !defined EPOCH_YEAR */
1993-08-28 12:13:24 -04:00
#ifndef TM_YEAR_BASE
#define TM_YEAR_BASE 1900
#endif /* !defined TM_YEAR_BASE */
1989-02-23 09:01:38 -05:00
#ifndef DAYSPERNYEAR
#define DAYSPERNYEAR 365
#endif /* !defined DAYSPERNYEAR */
1993-08-28 12:23:51 -04:00
#ifndef isleap
2004-10-05 10:17:24 -04:00
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1993-08-28 12:23:51 -04:00
#endif /* !defined isleap */
2004-10-05 10:17:24 -04:00
#ifndef isleap_sum
/*
2004-10-11 15:10:08 -04:00
** See tzfile.h for details on isleap_sum.
2004-10-05 10:17:24 -04:00
*/
2004-10-11 15:10:08 -04:00
#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
2004-10-05 10:17:24 -04:00
#endif /* !defined isleap_sum */
2004-11-18 13:04:19 -05:00
#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
2007-12-03 09:42:13 -05:00
#ifndef HAVE_GETTEXT
#define HAVE_GETTEXT 0
#endif
2004-08-09 19:44:19 -04:00
#if HAVE_GETTEXT
1996-02-21 15:47:24 -05:00
#include "locale.h" /* for setlocale */
#include "libintl.h"
2004-08-09 19:44:19 -04:00
#endif /* HAVE_GETTEXT */
1996-02-21 15:47:24 -05:00
1994-06-07 15:21:04 -04:00
#ifndef GNUC_or_lint
#ifdef lint
#define GNUC_or_lint
2004-08-09 19:44:19 -04:00
#else /* !defined lint */
1994-06-07 15:21:04 -04:00
#ifdef __GNUC__
#define GNUC_or_lint
#endif /* defined __GNUC__ */
1995-01-06 18:22:46 -05:00
#endif /* !defined lint */
1994-06-07 15:21:04 -04:00
#endif /* !defined GNUC_or_lint */
#ifndef INITIALIZE
#ifdef GNUC_or_lint
#define INITIALIZE(x) ((x) = 0)
2004-08-09 19:44:19 -04:00
#else /* !defined GNUC_or_lint */
1994-06-07 15:21:04 -04:00
#define INITIALIZE(x)
#endif /* !defined GNUC_or_lint */
#endif /* !defined INITIALIZE */
1996-02-19 19:28:54 -05:00
/*
** For the benefit of GNU folk...
1996-02-21 15:47:24 -05:00
** `_(MSGID)' uses the current locale's message library string for MSGID.
** The default is to use gettext if available, and use MSGID otherwise.
1996-02-19 19:28:54 -05:00
*/
#ifndef _
2004-08-09 19:44:19 -04:00
#if HAVE_GETTEXT
1996-02-21 15:47:24 -05:00
#define _(msgid) gettext(msgid)
2004-08-09 19:44:19 -04:00
#else /* !HAVE_GETTEXT */
1996-02-21 15:47:24 -05:00
#define _(msgid) msgid
2004-08-09 19:44:19 -04:00
#endif /* !HAVE_GETTEXT */
1996-02-19 19:28:54 -05:00
#endif /* !defined _ */
1996-02-21 15:47:24 -05:00
#ifndef TZ_DOMAIN
#define TZ_DOMAIN "tz"
#endif /* !defined TZ_DOMAIN */
1988-02-18 09:02:16 -05:00
extern char ** environ;
2007-11-08 09:21:57 -05:00
extern int getopt(int argc, char * const argv[],
const char * options);
1988-02-18 09:02:16 -05:00
extern char * optarg;
extern int optind;
1989-03-22 19:56:23 -05:00
extern char * tzname[2];
1989-02-23 09:01:38 -05:00
2004-11-18 13:04:19 -05:00
static time_t absolute_min_time;
static time_t absolute_max_time;
1997-03-03 12:21:15 -05:00
static size_t longest;
1989-03-17 20:26:01 -05:00
static char * progname;
2005-05-26 12:48:56 -04:00
static int warned;
2004-11-18 13:04:19 -05:00
2007-11-08 09:21:57 -05:00
static char * abbr(struct tm * tmp);
static void abbrok(const char * abbrp, const char * zone);
static long delta(struct tm * newp, struct tm * oldp);
static void dumptime(const struct tm * tmp);
static time_t hunt(char * name, time_t lot, time_t hit);
static void setabsolutes(void);
static void show(char * zone, time_t t, int v);
static const char * tformat(void);
static time_t yeartot(long y);
1986-08-28 11:09:07 -04:00
2005-01-11 11:53:34 -05:00
#ifndef TYPECHECK
#define my_localtime localtime
#else /* !defined TYPECHECK */
static struct tm *
my_localtime(tp)
time_t * tp;
{
register struct tm * tmp;
tmp = localtime(tp);
if (tp != NULL && tmp != NULL) {
struct tm tm;
register time_t t;
tm = *tmp;
t = mktime(&tm);
if (t - *tp >= 1 || *tp - t >= 1) {
(void) fflush(stdout);
(void) fprintf(stderr, "\n%s: ", progname);
(void) fprintf(stderr, tformat(), *tp);
(void) fprintf(stderr, " ->");
2005-03-22 08:57:18 -05:00
(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);
2005-01-11 11:53:34 -05:00
(void) fprintf(stderr, " -> ");
(void) fprintf(stderr, tformat(), t);
(void) fprintf(stderr, "\n");
}
}
return tmp;
}
#endif /* !defined TYPECHECK */
2005-05-26 12:48:56 -04:00
static void
2005-11-21 11:03:30 -05:00
abbrok(abbrp, zone)
const char * const abbrp;
2005-05-26 12:48:56 -04:00
const char * const zone;
{
register const char * cp;
register char * wp;
if (warned)
return;
2005-11-21 11:03:30 -05:00
cp = abbrp;
2005-05-26 12:48:56 -04:00
wp = NULL;
2005-12-06 11:01:24 -05:00
while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
2005-05-26 12:48:56 -04:00
++cp;
2005-11-21 11:03:30 -05:00
if (cp - abbrp == 0)
2005-12-27 09:05:26 -05:00
wp = _("lacks alphabetic at start");
2005-12-22 10:20:44 -05:00
else if (cp - abbrp < 3)
2005-12-27 09:05:26 -05:00
wp = _("has fewer than 3 alphabetics");
2005-12-22 10:20:44 -05:00
else if (cp - abbrp > 6)
2005-12-27 09:05:26 -05:00
wp = _("has more than 6 alphabetics");
2005-05-26 12:48:56 -04:00
if (wp == NULL && (*cp == '+' || *cp == '-')) {
++cp;
2005-12-06 11:01:24 -05:00
if (isascii((unsigned char) *cp) &&
isdigit((unsigned char) *cp))
if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
++cp;
2005-12-22 10:20:44 -05:00
if (*cp != '\0')
2005-12-27 09:05:26 -05:00
wp = _("differs from POSIX standard");
2005-05-26 12:48:56 -04:00
}
if (wp == NULL)
return;
(void) fflush(stdout);
(void) fprintf(stderr,
2005-11-21 10:48:21 -05:00
_("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
2005-12-27 09:05:26 -05:00
progname, zone, abbrp, wp);
2005-05-26 12:48:56 -04:00
warned = TRUE;
}
2009-01-14 10:54:45 -05:00
static void
2009-10-27 10:42:45 -04:00
usage(stream, status)
FILE * const stream;
const int status;
2009-01-14 10:54:45 -05:00
{
(void) fprintf(stream,
_("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
\n\
Report bugs to tz@elsie.nci.nih.gov.\n"),
progname, progname);
exit(status);
}
1989-01-28 20:47:53 -05:00
int
1986-01-16 11:00:21 -05:00
main(argc, argv)
int argc;
char * argv[];
{
1995-01-06 18:28:45 -05:00
register int i;
register int c;
1994-12-09 00:05:36 -05:00
register int vflag;
2004-11-18 12:21:02 -05:00
register char * cutarg;
2004-11-18 13:04:19 -05:00
register long cutloyear = ZDUMP_LO_YEAR;
register long cuthiyear = ZDUMP_HI_YEAR;
2004-11-18 12:21:02 -05:00
register time_t cutlotime;
register time_t cuthitime;
2004-11-18 13:46:26 -05:00
register char ** fakeenv;
1994-12-09 00:05:36 -05:00
time_t now;
1995-01-06 18:28:45 -05:00
time_t t;
time_t newt;
struct tm tm;
struct tm newtm;
2004-11-22 11:23:52 -05:00
register struct tm * tmp;
register struct tm * newtmp;
1986-01-16 11:00:21 -05:00
2004-11-18 12:21:02 -05:00
INITIALIZE(cutlotime);
INITIALIZE(cuthitime);
2004-08-09 19:44:19 -04:00
#if HAVE_GETTEXT
2005-01-31 14:20:42 -05:00
(void) setlocale(LC_ALL, "");
1996-02-21 15:47:24 -05:00
#ifdef TZ_DOMAINDIR
(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
2004-08-09 19:44:19 -04:00
#endif /* defined TEXTDOMAINDIR */
1996-02-21 15:47:24 -05:00
(void) textdomain(TZ_DOMAIN);
2004-08-09 19:44:19 -04:00
#endif /* HAVE_GETTEXT */
1989-03-17 20:26:01 -05:00
progname = argv[0];
2003-07-29 13:10:05 -04:00
for (i = 1; i < argc; ++i)
if (strcmp(argv[i], "--version") == 0) {
(void) printf("%s\n", elsieid);
2005-12-12 10:46:36 -05:00
exit(EXIT_SUCCESS);
2009-01-14 10:54:45 -05:00
} else if (strcmp(argv[i], "--help") == 0) {
2009-10-27 10:42:45 -04:00
usage(stdout, EXIT_SUCCESS);
2003-07-29 13:10:05 -04:00
}
1986-01-16 11:00:21 -05:00
vflag = 0;
2004-11-18 12:21:02 -05:00
cutarg = NULL;
1986-11-24 16:14:50 -05:00
while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
if (c == 'v')
vflag = 1;
2004-11-18 12:21:02 -05:00
else cutarg = optarg;
1997-01-20 19:02:22 -05:00
if ((c != EOF && c != -1) ||
1993-11-22 12:58:28 -05:00
(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
2009-10-27 10:42:45 -04:00
usage(stderr, EXIT_FAILURE);
1986-01-16 11:00:21 -05:00
}
2004-11-18 13:04:19 -05:00
if (vflag) {
if (cutarg != NULL) {
long lo;
long hi;
2005-01-13 09:52:17 -05:00
char dummy;
1993-08-28 12:23:51 -04:00
2005-01-13 09:52:17 -05:00
if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
2004-11-18 13:04:19 -05:00
cuthiyear = hi;
} else if (sscanf(cutarg, "%ld,%ld%c",
2005-01-13 09:52:17 -05:00
&lo, &hi, &dummy) == 2) {
2004-11-18 13:04:19 -05:00
cutloyear = lo;
cuthiyear = hi;
} else {
2004-11-18 12:21:02 -05:00
(void) fprintf(stderr, _("%s: wild -c argument %s\n"),
2004-11-18 13:04:19 -05:00
progname, cutarg);
2005-12-12 10:46:36 -05:00
exit(EXIT_FAILURE);
2004-11-18 13:04:19 -05:00
}
2004-11-18 12:21:02 -05:00
}
2004-11-18 13:04:19 -05:00
setabsolutes();
cutlotime = yeartot(cutloyear);
cuthitime = yeartot(cuthiyear);
1993-08-28 12:23:51 -04:00
}
1986-01-16 11:00:21 -05:00
(void) time(&now);
1986-03-03 23:15:08 -05:00
longest = 0;
for (i = optind; i < argc; ++i)
if (strlen(argv[i]) > longest)
longest = strlen(argv[i]);
1994-12-09 00:05:36 -05:00
{
1995-01-06 18:28:45 -05:00
register int from;
register int to;
1986-12-04 20:32:04 -05:00
2004-11-18 13:17:23 -05:00
for (i = 0; environ[i] != NULL; ++i)
1994-12-09 00:05:36 -05:00
continue;
1995-01-06 17:11:15 -05:00
fakeenv = (char **) malloc((size_t) ((i + 2) *
1995-01-03 12:04:47 -05:00
sizeof *fakeenv));
1994-12-09 00:05:36 -05:00
if (fakeenv == NULL ||
1997-03-03 12:21:15 -05:00
(fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
1995-01-03 12:04:47 -05:00
(void) perror(progname);
2005-12-12 10:46:36 -05:00
exit(EXIT_FAILURE);
1986-01-16 11:00:21 -05:00
}
1994-12-09 00:05:36 -05:00
to = 0;
(void) strcpy(fakeenv[to++], "TZ=");
for (from = 0; environ[from] != NULL; ++from)
if (strncmp(environ[from], "TZ=", 3) != 0)
fakeenv[to++] = environ[from];
fakeenv[to] = NULL;
1986-12-04 20:32:04 -05:00
environ = fakeenv;
1994-12-09 00:05:36 -05:00
}
for (i = optind; i < argc; ++i) {
static char buf[MAX_STRING_LENGTH];
(void) strcpy(&fakeenv[0][3], argv[i]);
1996-05-02 15:23:55 -04:00
if (!vflag) {
show(argv[i], now, FALSE);
1986-01-16 11:00:21 -05:00
continue;
1996-05-02 15:23:55 -04:00
}
2005-05-26 12:48:56 -04:00
warned = FALSE;
2004-11-18 13:04:19 -05:00
t = absolute_min_time;
1986-11-23 19:36:20 -05:00
show(argv[i], t, TRUE);
1988-02-27 19:15:34 -05:00
t += SECSPERHOUR * HOURSPERDAY;
1986-11-23 19:36:20 -05:00
show(argv[i], t, TRUE);
2004-11-22 10:46:46 -05:00
if (t < cutlotime)
t = cutlotime;
2005-01-11 11:53:34 -05:00
tmp = my_localtime(&t);
2004-11-22 11:23:52 -05:00
if (tmp != NULL) {
tm = *tmp;
(void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
}
1989-01-28 20:47:53 -05:00
for ( ; ; ) {
2009-01-14 11:01:56 -05:00
if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
1986-11-24 16:14:50 -05:00
break;
1989-01-28 20:47:53 -05:00
newt = t + SECSPERHOUR * 12;
2004-11-22 11:23:52 -05:00
newtmp = localtime(&newt);
if (newtmp != NULL)
newtm = *newtmp;
2005-01-31 14:06:05 -05:00
if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
2004-11-22 11:23:52 -05:00
(delta(&newtm, &tm) != (newt - t) ||
1989-03-17 20:26:01 -05:00
newtm.tm_isdst != tm.tm_isdst ||
2004-11-22 11:23:52 -05:00
strcmp(abbr(&newtm), buf) != 0)) {
1993-10-15 12:25:50 -04:00
newt = hunt(argv[i], t, newt);
2004-11-22 11:23:52 -05:00
newtmp = localtime(&newt);
if (newtmp != NULL) {
newtm = *newtmp;
(void) strncpy(buf,
abbr(&newtm),
(sizeof buf) - 1);
}
1989-03-17 20:26:01 -05:00
}
1989-01-28 20:47:53 -05:00
t = newt;
tm = newtm;
2004-11-22 11:23:52 -05:00
tmp = newtmp;
1986-01-16 11:00:21 -05:00
}
2004-11-18 13:04:19 -05:00
t = absolute_max_time;
1988-02-27 19:15:34 -05:00
t -= SECSPERHOUR * HOURSPERDAY;
1986-11-23 19:36:20 -05:00
show(argv[i], t, TRUE);
1988-02-27 19:15:34 -05:00
t += SECSPERHOUR * HOURSPERDAY;
1986-11-23 19:36:20 -05:00
show(argv[i], t, TRUE);
1986-01-16 11:00:21 -05:00
}
if (fflush(stdout) || ferror(stdout)) {
2004-11-18 13:04:19 -05:00
(void) fprintf(stderr, "%s: ", progname);
2006-12-05 09:39:06 -05:00
(void) perror(_("Error writing to standard output"));
2005-12-12 10:46:36 -05:00
exit(EXIT_FAILURE);
}
1989-01-28 20:47:53 -05:00
exit(EXIT_SUCCESS);
2004-11-23 16:24:43 -05:00
/* If exit fails to exit... */
return EXIT_FAILURE;
1989-01-28 20:47:53 -05:00
}
2004-11-18 13:04:19 -05:00
static void
2007-12-03 09:42:13 -05:00
setabsolutes(void)
2004-11-18 13:04:19 -05:00
{
if (0.5 == (time_t) 0.5) {
/*
** time_t is floating.
*/
2004-11-18 13:46:26 -05:00
if (sizeof (time_t) == sizeof (float)) {
2004-11-29 12:47:51 -05:00
absolute_min_time = (time_t) -FLT_MAX;
2004-11-18 13:46:26 -05:00
absolute_max_time = (time_t) FLT_MAX;
} else if (sizeof (time_t) == sizeof (double)) {
2004-11-29 12:47:51 -05:00
absolute_min_time = (time_t) -DBL_MAX;
2004-11-18 13:46:26 -05:00
absolute_max_time = (time_t) DBL_MAX;
} else {
(void) fprintf(stderr,
_("%s: use of -v on system with floating time_t other than float or double\n"),
progname);
2005-12-12 10:46:36 -05:00
exit(EXIT_FAILURE);
2004-11-18 13:46:26 -05:00
}
2004-11-18 13:04:19 -05:00
} else if (0 > (time_t) -1) {
/*
2006-02-16 11:16:57 -05:00
** time_t is signed. Assume overflow wraps around.
2004-11-18 13:04:19 -05:00
*/
2006-02-16 11:16:57 -05:00
time_t t = 0;
time_t t1 = 1;
2004-11-18 13:04:19 -05:00
2006-02-16 11:16:57 -05:00
while (t < t1) {
t = t1;
t1 = 2 * t1 + 1;
}
2007-03-15 09:55:14 -04:00
2006-02-16 11:16:57 -05:00
absolute_max_time = t;
t = -t;
absolute_min_time = t - 1;
if (t < absolute_min_time)
absolute_min_time = t;
2004-11-18 13:04:19 -05:00
} else {
/*
** time_t is unsigned.
*/
absolute_min_time = 0;
2004-11-23 16:27:04 -05:00
absolute_max_time = absolute_min_time - 1;
2004-11-18 13:04:19 -05:00
}
}
2004-11-18 12:21:02 -05:00
static time_t
yeartot(y)
const long y;
{
register long myy;
2004-11-18 13:04:19 -05:00
register long seconds;
register time_t t;
2004-11-18 12:21:02 -05:00
myy = EPOCH_YEAR;
2004-11-18 13:04:19 -05:00
t = 0;
2004-11-18 12:21:02 -05:00
while (myy != y) {
if (myy < y) {
2004-11-18 13:04:19 -05:00
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
2004-11-18 12:21:02 -05:00
++myy;
2004-11-23 17:04:31 -05:00
if (t > absolute_max_time - seconds) {
t = absolute_max_time;
break;
}
2004-11-18 13:04:19 -05:00
t += seconds;
2004-11-18 12:21:02 -05:00
} else {
--myy;
2004-11-18 13:04:19 -05:00
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
2004-11-23 17:04:31 -05:00
if (t < absolute_min_time + seconds) {
t = absolute_min_time;
break;
}
2004-11-18 13:04:19 -05:00
t -= seconds;
2004-11-18 12:21:02 -05:00
}
}
return t;
}
1993-10-15 12:25:50 -04:00
static time_t
2006-02-16 10:44:55 -05:00
hunt(char *name, time_t lot, time_t hit)
1989-01-28 20:47:53 -05:00
{
2004-11-29 12:47:51 -05:00
time_t t;
long diff;
struct tm lotm;
register struct tm * lotmp;
struct tm tm;
register struct tm * tmp;
char loab[MAX_STRING_LENGTH];
1989-01-28 20:47:53 -05:00
2005-01-11 11:53:34 -05:00
lotmp = my_localtime(&lot);
2004-11-29 12:47:51 -05:00
if (lotmp != NULL) {
lotm = *lotmp;
(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
}
for ( ; ; ) {
diff = (long) (hit - lot);
if (diff < 2)
break;
t = lot;
t += diff / 2;
1989-01-28 20:47:53 -05:00
if (t <= lot)
++t;
else if (t >= hit)
--t;
2005-01-11 11:53:34 -05:00
tmp = my_localtime(&t);
2004-11-29 12:47:51 -05:00
if (tmp != NULL)
tm = *tmp;
if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
(delta(&tm, &lotm) == (t - lot) &&
1989-03-17 20:26:01 -05:00
tm.tm_isdst == lotm.tm_isdst &&
2004-11-29 12:47:51 -05:00
strcmp(abbr(&tm), loab) == 0)) {
1989-02-27 09:26:06 -05:00
lot = t;
lotm = tm;
2004-11-29 12:47:51 -05:00
lotmp = tmp;
1989-01-28 20:47:53 -05:00
} else hit = t;
}
show(name, lot, TRUE);
show(name, hit, TRUE);
1993-10-15 12:25:50 -04:00
return hit;
1989-01-28 20:47:53 -05:00
}
1993-09-05 21:20:54 -04:00
/*
2005-11-28 14:56:51 -05:00
** Thanks to Paul Eggert for logic used in delta.
1993-09-05 21:20:54 -04:00
*/
1989-01-28 20:47:53 -05:00
static long
delta(newp, oldp)
1989-02-23 09:01:38 -05:00
struct tm * newp;
struct tm * oldp;
1989-01-28 20:47:53 -05:00
{
2004-11-18 13:46:26 -05:00
register long result;
register int tmy;
1989-01-28 20:47:53 -05:00
1993-08-28 12:23:51 -04:00
if (newp->tm_year < oldp->tm_year)
return -delta(oldp, newp);
result = 0;
for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
2004-10-05 10:17:24 -04:00
result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
1993-08-28 12:13:24 -04:00
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;
1986-01-16 11:00:21 -05:00
}
1986-03-02 22:03:39 -05:00
1988-02-18 09:02:16 -05:00
static void
2006-02-16 10:44:55 -05:00
show(char *zone, time_t t, int v)
1986-03-02 22:03:39 -05:00
{
2004-11-18 13:46:26 -05:00
register struct tm * tmp;
1986-08-28 11:09:07 -04:00
1997-03-03 12:21:15 -05:00
(void) printf("%-*s ", (int) longest, zone);
2004-08-05 10:48:37 -04:00
if (v) {
2004-11-22 11:23:52 -05:00
tmp = gmtime(&t);
if (tmp == NULL) {
2004-12-24 07:45:38 -05:00
(void) printf(tformat(), t);
2004-11-22 11:23:52 -05:00
} else {
dumptime(tmp);
(void) printf(" UTC");
}
(void) printf(" = ");
2004-08-05 10:48:37 -04:00
}
2005-01-11 11:53:34 -05:00
tmp = my_localtime(&t);
2004-08-05 10:48:37 -04:00
dumptime(tmp);
2004-11-22 11:23:52 -05:00
if (tmp != NULL) {
if (*abbr(tmp) != '\0')
(void) printf(" %s", abbr(tmp));
if (v) {
(void) printf(" isdst=%d", tmp->tm_isdst);
#ifdef TM_GMTOFF
2004-11-22 11:23:52 -05:00
(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
#endif /* defined TM_GMTOFF */
2004-11-22 11:23:52 -05:00
}
1987-02-14 14:26:58 -05:00
}
1986-03-02 22:03:39 -05:00
(void) printf("\n");
2005-05-26 12:48:56 -04:00
if (tmp != NULL && *abbr(tmp) != '\0')
abbrok(abbr(tmp), zone);
1986-03-02 22:03:39 -05:00
}
1989-03-17 20:26:01 -05:00
static char *
1989-03-22 19:56:23 -05:00
abbr(tmp)
struct tm * tmp;
1989-03-17 20:26:01 -05:00
{
register char * result;
1994-06-07 15:21:04 -04:00
static char nada;
1989-03-17 20:26:01 -05:00
1989-03-22 19:56:23 -05:00
if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
1994-06-07 15:21:04 -04:00
return &nada;
1989-03-22 19:56:23 -05:00
result = tzname[tmp->tm_isdst];
1994-06-07 15:21:04 -04:00
return (result == NULL) ? &nada : result;
1989-03-17 20:26:01 -05:00
}
2004-08-05 10:48:37 -04:00
2004-12-30 08:46:00 -05:00
/*
** 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 *
2007-12-03 09:42:13 -05:00
tformat(void)
2004-12-24 07:45:38 -05:00
{
2004-12-30 08:46:00 -05:00
if (0.5 == (time_t) 0.5) { /* floating */
if (sizeof (time_t) > sizeof (double))
return "%Lg";
2004-12-24 07:45:38 -05:00
return "%g";
2004-12-30 08:46:00 -05:00
}
2004-12-24 07:45:38 -05:00
if (0 > (time_t) -1) { /* signed */
if (sizeof (time_t) > sizeof (long))
return "%lld";
2004-12-30 08:46:00 -05:00
if (sizeof (time_t) > sizeof (int))
2004-12-24 07:45:38 -05:00
return "%ld";
return "%d";
}
if (sizeof (time_t) > sizeof (unsigned long))
return "%llu";
2004-12-30 08:46:00 -05:00
if (sizeof (time_t) > sizeof (unsigned int))
2004-12-24 07:45:38 -05:00
return "%lu";
return "%u";
}
2004-08-05 10:48:37 -04:00
static void
dumptime(timeptr)
register const struct tm * timeptr;
{
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"
};
register const char * wn;
register const char * mn;
2004-10-14 18:00:37 -04:00
register int lead;
register int trail;
2004-08-05 10:48:37 -04:00
2004-11-22 11:23:52 -05:00
if (timeptr == NULL) {
(void) printf("NULL");
return;
}
2004-08-11 12:06:12 -04:00
/*
** 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.
*/
2004-08-09 19:50:18 -04:00
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];
2004-10-14 13:00:48 -04:00
(void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
2004-08-05 10:48:37 -04:00
wn, mn,
timeptr->tm_mday, timeptr->tm_hour,
2004-10-14 13:00:48 -04:00
timeptr->tm_min, timeptr->tm_sec);
2004-10-14 13:57:18 -04:00
#define DIVISOR 10
2004-10-14 13:00:48 -04:00
trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
2004-10-18 11:37:53 -04:00
lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
trail / DIVISOR;
trail %= DIVISOR;
if (trail < 0 && lead > 0) {
2004-10-14 13:00:48 -04:00
trail += DIVISOR;
--lead;
2004-10-18 11:37:53 -04:00
} else if (lead < 0 && trail > 0) {
2004-10-14 13:00:48 -04:00
trail -= DIVISOR;
++lead;
}
if (lead == 0)
(void) printf("%d", trail);
else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
2004-08-05 10:48:37 -04:00
}