Files
amiga-tz/zdump.c
T

403 lines
9.1 KiB
C
Raw Normal View History

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 */
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 */
1996-02-21 15:47:24 -05:00
#if HAVE_GETTEXT - 0
#include "locale.h" /* for setlocale */
#include "libintl.h"
#endif /* HAVE_GETTEXT - 0 */
1994-06-07 15:21:04 -04:00
#ifndef GNUC_or_lint
#ifdef lint
#define GNUC_or_lint
#endif /* defined lint */
1995-01-06 18:22:46 -05:00
#ifndef 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)
#endif /* defined GNUC_or_lint */
#ifndef GNUC_or_lint
#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 _
1996-02-21 15:47:24 -05:00
#if HAVE_GETTEXT - 0
#define _(msgid) gettext(msgid)
#else /* !(HAVE_GETTEXT - 0) */
#define _(msgid) msgid
#endif /* !(HAVE_GETTEXT - 0) */
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 */
1997-03-03 12:21:15 -05:00
#ifndef P
#ifdef __STDC__
#define P(x) x
#endif /* defined __STDC__ */
#ifndef __STDC__
#define P(x) ()
#endif /* !defined __STDC__ */
#endif /* !defined P */
1988-02-18 09:02:16 -05:00
extern char ** environ;
1997-03-03 12:21:15 -05:00
extern int getopt P((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
1997-03-03 12:21:15 -05:00
static char * abbr P((struct tm * tmp));
static long delta P((struct tm * newp, struct tm * oldp));
static time_t hunt P((char * name, time_t lot, time_t hit));
static size_t longest;
1989-03-17 20:26:01 -05:00
static char * progname;
1997-03-03 12:21:15 -05:00
static void show P((char * zone, time_t t, int v));
2004-08-05 10:48:37 -04:00
static void dumptime P((const struct tm * tmp));
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[];
{
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;
register char * cutoff;
register int cutyear;
register long cuttime;
char ** fakeenv;
time_t now;
1995-01-06 18:28:45 -05:00
time_t t;
time_t newt;
1994-12-09 00:05:36 -05:00
time_t hibit;
1995-01-06 18:28:45 -05:00
struct tm tm;
struct tm newtm;
1986-01-16 11:00:21 -05:00
1994-06-07 15:21:04 -04:00
INITIALIZE(cuttime);
1996-02-21 15:47:24 -05:00
#if HAVE_GETTEXT - 0
(void) setlocale(LC_MESSAGES, "");
#ifdef TZ_DOMAINDIR
(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
#endif /* defined(TEXTDOMAINDIR) */
(void) textdomain(TZ_DOMAIN);
#endif /* HAVE_GETTEXT - 0 */
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);
(void) exit(EXIT_SUCCESS);
}
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;
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)) {
(void) fprintf(stderr,
2003-07-29 13:11:09 -04:00
_("%s: usage is %s [ --version ] [ -v ] [ -c cutoff ] zonename ...\n"),
1993-11-22 12:58:28 -05:00
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
{
1995-01-06 18:28:45 -05:00
register int from;
register int 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;
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);
(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]);
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
}
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)) {
2001-03-13 12:32:28 -05:00
(void) fprintf(stderr, "%s: ", argv[0]);
(void) perror(_("Error writing standard output"));
1988-02-27 19:15:34 -05:00
(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
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
1997-03-03 12:21:15 -05:00
(void) printf("%-*s ", (int) longest, zone);
2004-08-05 10:48:37 -04:00
if (v) {
dumptime(gmtime(&t));
(void) printf(" UTC = ");
}
1986-11-15 20:19:07 -05:00
tmp = localtime(&t);
2004-08-05 10:48:37 -04:00
dumptime(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
}
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-08-09 17:20:56 -04:00
if ((unsigned) timeptr->tm_wday >= sizeof wday_name / sizeof wday_name[0])
wn = "???";
else wn = wday_name[timeptr->tm_wday];
if ((unsigned) timeptr->tm_mon >= sizeof mon_name / sizeof mon_name[0])
mn = "???";
else mn = mon_name[timeptr->tm_mon];
2004-08-05 10:48:37 -04:00
(void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d %ld",
wn, mn,
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
timeptr->tm_year + (long) TM_YEAR_BASE);
}