Files
amiga-tz/zdump.c
T

335 lines
7.2 KiB
C
Raw Normal View History

1986-12-25 18:29:14 -05:00
#ifndef lint
#ifndef NOID
1988-02-18 04:32:17 -05:00
static char elsieid[] = "%W%";
1988-02-23 10:03:48 -05:00
#endif /* !defined NOID */
#endif /* !defined lint */
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:08:23 -05:00
#ifndef HAVE_STDLIB_H
#define HAVE_STDLIB_H 1
1995-01-03 11:26:28 -05:00
#endif /* !defined HAVE_STDLIB_H */
1995-01-03 11:08:23 -05:00
#if HAVE_STDLIB_H
1995-01-03 11:34:03 -05:00
#include "stdlib.h" /* for exit, malloc, atoi */
1995-01-03 11:26:28 -05:00
#endif /* defined HAVE_STDLIB_H */
1995-01-03 11:08:23 -05:00
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
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
#endif /* !defined isleap */
1994-06-07 15:21:04 -04:00
#ifndef GNUC_or_lint
#ifdef lint
#define GNUC_or_lint
#endif /* defined lint */
#ifdef __GNUC__
#define GNUC_or_lint
#endif /* defined __GNUC__ */
#endif /* !defined GNUC_or_lint */
#ifndef INITIALIZE
#ifdef GNUC_or_lint
#define INITIALIZE(x) ((x) = 0)
#endif /* defined GNUC_or_lint */
#ifndef GNUC_or_lint
#define INITIALIZE(x)
#endif /* !defined GNUC_or_lint */
#endif /* !defined INITIALIZE */
1988-02-18 09:02:16 -05:00
extern char ** environ;
1989-02-23 09:01:38 -05:00
extern int getopt();
1988-02-18 09:02:16 -05:00
extern char * optarg;
extern int optind;
1989-03-21 11:36:42 -05:00
extern time_t time();
1989-03-22 19:56:23 -05:00
extern char * tzname[2];
1989-02-23 09:01:38 -05:00
1989-03-22 19:56:23 -05:00
static char * abbr();
1989-02-23 09:01:38 -05:00
static long delta();
1993-10-15 12:25:50 -04:00
static time_t hunt();
1989-03-22 19:56:23 -05:00
static int longest;
1989-03-17 20:26:01 -05:00
static char * progname;
1989-03-22 19:56:23 -05:00
static void show();
1986-08-28 11:09:07 -04:00
1989-01-28 20:47:53 -05:00
int
1986-01-16 11:00:21 -05:00
main(argc, argv)
int argc;
char * argv[];
{
1994-12-09 00:05:36 -05:00
register int i, c;
register int vflag;
register char * cutoff;
register int cutyear;
register long cuttime;
char ** fakeenv;
time_t now;
time_t t, newt;
time_t hibit;
struct tm tm, newtm;
1986-01-16 11:00:21 -05:00
1994-06-07 15:21:04 -04:00
INITIALIZE(cuttime);
1989-03-17 20:26:01 -05:00
progname = argv[0];
1986-01-16 11:00:21 -05:00
vflag = 0;
1986-11-24 16:14:50 -05:00
cutoff = NULL;
while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
if (c == 'v')
vflag = 1;
else cutoff = optarg;
1992-02-29 20:11:46 -05:00
if (c != EOF ||
1993-11-22 12:58:28 -05:00
(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
(void) fprintf(stderr,
"%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n",
argv[0], argv[0]);
(void) exit(EXIT_FAILURE);
1986-01-16 11:00:21 -05:00
}
1993-08-28 12:23:51 -04:00
if (cutoff != NULL) {
int y;
1986-11-24 16:14:50 -05:00
cutyear = atoi(cutoff);
1993-08-28 12:23:51 -04:00
cuttime = 0;
for (y = EPOCH_YEAR; y < cutyear; ++y)
cuttime += DAYSPERNYEAR + isleap(y);
cuttime *= SECSPERHOUR * HOURSPERDAY;
}
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]);
1989-02-23 09:01:38 -05:00
for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
1994-04-09 17:19:11 -04:00
continue;
1994-12-09 00:05:36 -05:00
{
register int from, to;
1986-12-04 20:32:04 -05:00
1994-12-09 00:05:36 -05:00
for (i = 0; environ[i] != NULL; ++i)
continue;
fakeenv = (char **) malloc((i + 2) * sizeof *fakeenv);
if (fakeenv == NULL ||
(fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
(void) perror(progname);
(void) 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]);
1986-03-04 10:38:47 -05:00
show(argv[i], now, FALSE);
1986-01-16 11:00:21 -05:00
if (!vflag)
continue;
1989-02-23 09:01:38 -05:00
/*
** Get lowest value of t.
*/
t = hibit;
1987-02-14 14:26:58 -05:00
if (t > 0) /* time_t is unsigned */
t = 0;
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);
1989-01-28 20:47:53 -05:00
tm = *localtime(&t);
1989-03-22 19:56:23 -05:00
(void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
1989-01-28 20:47:53 -05:00
for ( ; ; ) {
if (cutoff != NULL && t >= cuttime)
1986-11-24 16:14:50 -05:00
break;
1989-01-28 20:47:53 -05:00
newt = t + SECSPERHOUR * 12;
if (cutoff != NULL && newt >= cuttime)
1988-01-25 10:38:57 -05:00
break;
1989-01-28 20:47:53 -05:00
if (newt <= t)
break;
newtm = *localtime(&newt);
1989-02-27 09:26:06 -05:00
if (delta(&newtm, &tm) != (newt - t) ||
1989-03-17 20:26:01 -05:00
newtm.tm_isdst != tm.tm_isdst ||
1989-03-22 19:56:23 -05:00
strcmp(abbr(&newtm), buf) != 0) {
1993-10-15 12:25:50 -04:00
newt = hunt(argv[i], t, newt);
newtm = *localtime(&newt);
1989-03-22 19:56:23 -05:00
(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;
1986-01-16 11:00:21 -05:00
}
1989-02-23 09:01:38 -05:00
/*
** Get highest value of t.
*/
t = ~((time_t) 0);
1987-02-14 14:26:58 -05:00
if (t < 0) /* time_t is signed */
1989-02-23 09:01:38 -05:00
t &= ~hibit;
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)) {
1986-08-28 09:16:17 -04:00
(void) fprintf(stderr, "%s: Error writing standard output ",
1986-08-28 11:09:07 -04:00
argv[0]);
1988-02-27 19:15:34 -05:00
(void) perror("standard output");
(void) exit(EXIT_FAILURE);
}
1989-01-28 20:47:53 -05:00
exit(EXIT_SUCCESS);
1989-03-22 19:56:23 -05:00
/* gcc -Wall pacifier */
1989-01-28 20:47:53 -05:00
for ( ; ; )
1994-04-09 17:19:11 -04:00
continue;
1989-01-28 20:47:53 -05:00
}
1993-10-15 12:25:50 -04:00
static time_t
1989-01-28 20:47:53 -05:00
hunt(name, lot, hit)
1989-02-23 09:01:38 -05:00
char * name;
time_t lot;
time_t hit;
1989-01-28 20:47:53 -05:00
{
time_t t;
struct tm lotm;
struct tm tm;
1989-03-22 19:56:23 -05:00
static char loab[MAX_STRING_LENGTH];
1989-01-28 20:47:53 -05:00
lotm = *localtime(&lot);
1989-03-22 19:56:23 -05:00
(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
1989-01-28 20:47:53 -05:00
while ((hit - lot) >= 2) {
t = lot / 2 + hit / 2;
if (t <= lot)
++t;
else if (t >= hit)
--t;
tm = *localtime(&t);
1989-02-27 09:26:06 -05:00
if (delta(&tm, &lotm) == (t - lot) &&
1989-03-17 20:26:01 -05:00
tm.tm_isdst == lotm.tm_isdst &&
1989-03-22 19:56:23 -05:00
strcmp(abbr(&tm), loab) == 0) {
1989-02-27 09:26:06 -05:00
lot = t;
lotm = tm;
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
/*
1993-09-05 21:21:18 -04:00
** Thanks to Paul Eggert (eggert@twinsun.com) 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
{
long result;
1993-08-28 12:23:51 -04:00
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)
result += DAYSPERNYEAR + isleap(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
1994-06-07 15:21:04 -04:00
extern struct tm * localtime();
1988-02-18 09:02:16 -05:00
static void
1986-03-03 23:15:08 -05:00
show(zone, t, v)
1989-02-23 09:01:38 -05:00
char * zone;
time_t t;
1989-02-25 16:16:00 -05:00
int v;
1986-03-02 22:03:39 -05:00
{
1994-06-07 15:21:04 -04:00
struct tm * tmp;
1986-08-28 11:09:07 -04:00
1986-03-03 23:15:08 -05:00
(void) printf("%-*s ", longest, zone);
if (v)
(void) printf("%.24s GMT = ", asctime(gmtime(&t)));
1986-11-15 20:19:07 -05:00
tmp = localtime(&t);
1986-08-28 11:09:07 -04:00
(void) printf("%.24s", asctime(tmp));
1989-03-22 19:56:23 -05:00
if (*abbr(tmp) != '\0')
(void) printf(" %s", abbr(tmp));
1987-02-14 14:26:58 -05:00
if (v) {
1986-08-28 11:09:07 -04:00
(void) printf(" isdst=%d", tmp->tm_isdst);
#ifdef TM_GMTOFF
(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
#endif /* defined TM_GMTOFF */
1987-02-14 14:26:58 -05:00
}
1986-03-02 22:03:39 -05:00
(void) printf("\n");
}
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
}