michael@0: static char elsieid[] = "@(#)zdump.c 8.8"; michael@0: michael@0: /* michael@0: ** This code has been made independent of the rest of the time michael@0: ** conversion package to increase confidence in the verification it provides. michael@0: ** You can use this code to help in verifying other implementations. michael@0: */ michael@0: michael@0: /* michael@0: * ICU note: Mr. Arthur David Olson (olsona@dc37a.nci.nih.gov) stated that michael@0: * "zdump.c is indeed in the public domain" in e-mail on Feb 22, 2007. michael@0: * This version of zdump.c is modified by ICU team to change output format michael@0: * and some additional options. michael@0: */ michael@0: michael@0: michael@0: #include "stdio.h" /* for stdout, stderr, perror */ michael@0: #include "string.h" /* for strcpy */ michael@0: #include "sys/types.h" /* for time_t */ michael@0: #include "time.h" /* for struct tm */ michael@0: #include "stdlib.h" /* for exit, malloc, atoi */ michael@0: #include "float.h" /* for FLT_MAX and DBL_MAX */ michael@0: #include "ctype.h" /* for isalpha et al. */ michael@0: michael@0: /* Enable extensions and modifications for ICU. */ michael@0: #define ICU michael@0: michael@0: #ifdef ICU michael@0: #include "dirent.h" michael@0: #endif michael@0: michael@0: #ifndef isascii michael@0: #define isascii(x) 1 michael@0: #endif /* !defined isascii */ michael@0: michael@0: #ifndef ZDUMP_LO_YEAR michael@0: #define ZDUMP_LO_YEAR (-500) michael@0: #endif /* !defined ZDUMP_LO_YEAR */ michael@0: michael@0: #ifndef ZDUMP_HI_YEAR michael@0: #define ZDUMP_HI_YEAR 2500 michael@0: #endif /* !defined ZDUMP_HI_YEAR */ michael@0: michael@0: #ifndef MAX_STRING_LENGTH michael@0: #define MAX_STRING_LENGTH 1024 michael@0: #endif /* !defined MAX_STRING_LENGTH */ michael@0: michael@0: #ifndef TRUE michael@0: #define TRUE 1 michael@0: #endif /* !defined TRUE */ michael@0: michael@0: #ifndef FALSE michael@0: #define FALSE 0 michael@0: #endif /* !defined FALSE */ michael@0: michael@0: #ifndef EXIT_SUCCESS michael@0: #define EXIT_SUCCESS 0 michael@0: #endif /* !defined EXIT_SUCCESS */ michael@0: michael@0: #ifndef EXIT_FAILURE michael@0: #define EXIT_FAILURE 1 michael@0: #endif /* !defined EXIT_FAILURE */ michael@0: michael@0: #ifndef SECSPERMIN michael@0: #define SECSPERMIN 60 michael@0: #endif /* !defined SECSPERMIN */ michael@0: michael@0: #ifndef MINSPERHOUR michael@0: #define MINSPERHOUR 60 michael@0: #endif /* !defined MINSPERHOUR */ michael@0: michael@0: #ifndef SECSPERHOUR michael@0: #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) michael@0: #endif /* !defined SECSPERHOUR */ michael@0: michael@0: #ifndef HOURSPERDAY michael@0: #define HOURSPERDAY 24 michael@0: #endif /* !defined HOURSPERDAY */ michael@0: michael@0: #ifndef EPOCH_YEAR michael@0: #define EPOCH_YEAR 1970 michael@0: #endif /* !defined EPOCH_YEAR */ michael@0: michael@0: #ifndef TM_YEAR_BASE michael@0: #define TM_YEAR_BASE 1900 michael@0: #endif /* !defined TM_YEAR_BASE */ michael@0: michael@0: #ifndef DAYSPERNYEAR michael@0: #define DAYSPERNYEAR 365 michael@0: #endif /* !defined DAYSPERNYEAR */ michael@0: michael@0: #ifndef isleap michael@0: #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) michael@0: #endif /* !defined isleap */ michael@0: michael@0: #ifndef isleap_sum michael@0: /* michael@0: ** See tzfile.h for details on isleap_sum. michael@0: */ michael@0: #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400) michael@0: #endif /* !defined isleap_sum */ michael@0: michael@0: #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY) michael@0: #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR) michael@0: #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY) michael@0: michael@0: #ifndef HAVE_GETTEXT michael@0: #define HAVE_GETTEXT 0 michael@0: #endif michael@0: #if HAVE_GETTEXT michael@0: #include "locale.h" /* for setlocale */ michael@0: #include "libintl.h" michael@0: #endif /* HAVE_GETTEXT */ michael@0: michael@0: #ifndef GNUC_or_lint michael@0: #ifdef lint michael@0: #define GNUC_or_lint michael@0: #else /* !defined lint */ michael@0: #ifdef __GNUC__ michael@0: #define GNUC_or_lint michael@0: #endif /* defined __GNUC__ */ michael@0: #endif /* !defined lint */ michael@0: #endif /* !defined GNUC_or_lint */ michael@0: michael@0: #ifndef INITIALIZE michael@0: #ifdef GNUC_or_lint michael@0: #define INITIALIZE(x) ((x) = 0) michael@0: #else /* !defined GNUC_or_lint */ michael@0: #define INITIALIZE(x) michael@0: #endif /* !defined GNUC_or_lint */ michael@0: #endif /* !defined INITIALIZE */ michael@0: michael@0: /* michael@0: ** For the benefit of GNU folk... michael@0: ** `_(MSGID)' uses the current locale's message library string for MSGID. michael@0: ** The default is to use gettext if available, and use MSGID otherwise. michael@0: */ michael@0: michael@0: #ifndef _ michael@0: #if HAVE_GETTEXT michael@0: #define _(msgid) gettext(msgid) michael@0: #else /* !HAVE_GETTEXT */ michael@0: #define _(msgid) msgid michael@0: #endif /* !HAVE_GETTEXT */ michael@0: #endif /* !defined _ */ michael@0: michael@0: #ifndef TZ_DOMAIN michael@0: #define TZ_DOMAIN "tz" michael@0: #endif /* !defined TZ_DOMAIN */ michael@0: michael@0: extern char ** environ; michael@0: extern int getopt(int argc, char * const argv[], michael@0: const char * options); michael@0: extern char * optarg; michael@0: extern int optind; michael@0: extern char * tzname[2]; michael@0: michael@0: static time_t absolute_min_time; michael@0: static time_t absolute_max_time; michael@0: static size_t longest; michael@0: static char * progname; michael@0: static int warned; michael@0: michael@0: static char * abbr(struct tm * tmp); michael@0: static void abbrok(const char * abbrp, const char * zone); michael@0: static long delta(struct tm * newp, struct tm * oldp); michael@0: static void dumptime(const struct tm * tmp); michael@0: static time_t hunt(char * name, time_t lot, time_t hit); michael@0: static void setabsolutes(void); michael@0: static void show(char * zone, time_t t, int v); michael@0: static const char * tformat(void); michael@0: static time_t yeartot(long y); michael@0: #ifdef ICU michael@0: typedef struct listentry { michael@0: char * name; michael@0: struct listentry * next; michael@0: } listentry; michael@0: michael@0: static time_t huntICU(char * name, time_t lot, time_t hit, FILE *fp); michael@0: static void dumptimeICU(FILE * fp, time_t t); michael@0: static void showICU(FILE * fp, char * zone, time_t t1, time_t t2); michael@0: static int getall(struct listentry ** namelist); michael@0: static void getzones(char * basedir, char * subdir, struct listentry ** last, int * count); michael@0: #endif michael@0: michael@0: #ifndef TYPECHECK michael@0: #define my_localtime localtime michael@0: #else /* !defined TYPECHECK */ michael@0: static struct tm * michael@0: my_localtime(tp) michael@0: time_t * tp; michael@0: { michael@0: register struct tm * tmp; michael@0: michael@0: tmp = localtime(tp); michael@0: if (tp != NULL && tmp != NULL) { michael@0: struct tm tm; michael@0: register time_t t; michael@0: michael@0: tm = *tmp; michael@0: t = mktime(&tm); michael@0: if (t - *tp >= 1 || *tp - t >= 1) { michael@0: (void) fflush(stdout); michael@0: (void) fprintf(stderr, "\n%s: ", progname); michael@0: (void) fprintf(stderr, tformat(), *tp); michael@0: (void) fprintf(stderr, " ->"); michael@0: (void) fprintf(stderr, " year=%d", tmp->tm_year); michael@0: (void) fprintf(stderr, " mon=%d", tmp->tm_mon); michael@0: (void) fprintf(stderr, " mday=%d", tmp->tm_mday); michael@0: (void) fprintf(stderr, " hour=%d", tmp->tm_hour); michael@0: (void) fprintf(stderr, " min=%d", tmp->tm_min); michael@0: (void) fprintf(stderr, " sec=%d", tmp->tm_sec); michael@0: (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst); michael@0: (void) fprintf(stderr, " -> "); michael@0: (void) fprintf(stderr, tformat(), t); michael@0: (void) fprintf(stderr, "\n"); michael@0: } michael@0: } michael@0: return tmp; michael@0: } michael@0: #endif /* !defined TYPECHECK */ michael@0: michael@0: static void michael@0: abbrok(abbrp, zone) michael@0: const char * const abbrp; michael@0: const char * const zone; michael@0: { michael@0: register const char * cp; michael@0: register char * wp; michael@0: michael@0: if (warned) michael@0: return; michael@0: cp = abbrp; michael@0: wp = NULL; michael@0: while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp)) michael@0: ++cp; michael@0: if (cp - abbrp == 0) michael@0: wp = _("lacks alphabetic at start"); michael@0: else if (cp - abbrp < 3) michael@0: wp = _("has fewer than 3 alphabetics"); michael@0: else if (cp - abbrp > 6) michael@0: wp = _("has more than 6 alphabetics"); michael@0: if (wp == NULL && (*cp == '+' || *cp == '-')) { michael@0: ++cp; michael@0: if (isascii((unsigned char) *cp) && michael@0: isdigit((unsigned char) *cp)) michael@0: if (*cp++ == '1' && *cp >= '0' && *cp <= '4') michael@0: ++cp; michael@0: if (*cp != '\0') michael@0: wp = _("differs from POSIX standard"); michael@0: } michael@0: if (wp == NULL) michael@0: return; michael@0: (void) fflush(stdout); michael@0: (void) fprintf(stderr, michael@0: _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"), michael@0: progname, zone, abbrp, wp); michael@0: warned = TRUE; michael@0: } michael@0: michael@0: static void michael@0: usage(const char *progname, FILE *stream, int status) michael@0: { michael@0: (void) fprintf(stream, michael@0: _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\ michael@0: \n\ michael@0: Report bugs to tz@elsie.nci.nih.gov.\n"), michael@0: progname, progname); michael@0: exit(status); michael@0: } michael@0: michael@0: int michael@0: main(argc, argv) michael@0: int argc; michael@0: char * argv[]; michael@0: { michael@0: register int i; michael@0: register int c; michael@0: register int vflag; michael@0: register char * cutarg; michael@0: register long cutloyear = ZDUMP_LO_YEAR; michael@0: register long cuthiyear = ZDUMP_HI_YEAR; michael@0: register time_t cutlotime; michael@0: register time_t cuthitime; michael@0: register char ** fakeenv; michael@0: time_t now; michael@0: time_t t; michael@0: time_t newt; michael@0: struct tm tm; michael@0: struct tm newtm; michael@0: register struct tm * tmp; michael@0: register struct tm * newtmp; michael@0: #ifdef ICU michael@0: int nextopt; michael@0: char * dirarg; michael@0: int aflag; michael@0: int iflag; michael@0: listentry * namelist = NULL; michael@0: FILE * fp = stdout; michael@0: #endif michael@0: michael@0: INITIALIZE(cutlotime); michael@0: INITIALIZE(cuthitime); michael@0: #if HAVE_GETTEXT michael@0: (void) setlocale(LC_ALL, ""); michael@0: #ifdef TZ_DOMAINDIR michael@0: (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR); michael@0: #endif /* defined TEXTDOMAINDIR */ michael@0: (void) textdomain(TZ_DOMAIN); michael@0: #endif /* HAVE_GETTEXT */ michael@0: progname = argv[0]; michael@0: for (i = 1; i < argc; ++i) michael@0: if (strcmp(argv[i], "--version") == 0) { michael@0: (void) printf("%s\n", elsieid); michael@0: exit(EXIT_SUCCESS); michael@0: } else if (strcmp(argv[i], "--help") == 0) { michael@0: usage(progname, stdout, EXIT_SUCCESS); michael@0: } michael@0: vflag = 0; michael@0: cutarg = NULL; michael@0: #ifdef ICU michael@0: aflag = 0; michael@0: iflag = 0; michael@0: dirarg = NULL; michael@0: nextopt = 1; michael@0: while(nextopt) { michael@0: c = getopt(argc, argv, "ac:d:iv"); michael@0: switch(c) { michael@0: case 'a': michael@0: aflag = 1; michael@0: break; michael@0: case 'c': michael@0: cutarg = optarg; michael@0: break; michael@0: case 'd': michael@0: dirarg = optarg; michael@0: break; michael@0: case 'i': michael@0: iflag = 1; michael@0: break; michael@0: case 'v': michael@0: vflag = 1; michael@0: break; michael@0: default: michael@0: nextopt = 0; michael@0: break; michael@0: } michael@0: } michael@0: if ((c != EOF && c != -1) || michael@0: (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) { michael@0: (void) fprintf(stderr, michael@0: _("%s: usage is %s [ --version ] [ -a ] [ -v ] [ -i ] [ -c [loyear,]hiyear ] [ -d dir ] [ zonename ... ]\n"), michael@0: progname, progname); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: michael@0: if (dirarg != NULL) { michael@0: DIR * dp; michael@0: /* create the output directory */ michael@0: mkdir(dirarg, 0777); michael@0: if ((dp = opendir(dirarg)) == NULL) { michael@0: fprintf(stderr, "cannot create the target directory"); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: closedir(dp); michael@0: } michael@0: #else michael@0: while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v') michael@0: if (c == 'v') michael@0: vflag = 1; michael@0: else cutarg = optarg; michael@0: if ((c != EOF && c != -1) || michael@0: (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) { michael@0: usage(progname, stderr, EXIT_FAILURE); michael@0: } michael@0: #endif michael@0: if (vflag) { michael@0: if (cutarg != NULL) { michael@0: long lo; michael@0: long hi; michael@0: char dummy; michael@0: michael@0: if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) { michael@0: cuthiyear = hi; michael@0: } else if (sscanf(cutarg, "%ld,%ld%c", michael@0: &lo, &hi, &dummy) == 2) { michael@0: cutloyear = lo; michael@0: cuthiyear = hi; michael@0: } else { michael@0: (void) fprintf(stderr, _("%s: wild -c argument %s\n"), michael@0: progname, cutarg); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } michael@0: setabsolutes(); michael@0: cutlotime = yeartot(cutloyear); michael@0: cuthitime = yeartot(cuthiyear); michael@0: } michael@0: michael@0: #ifdef ICU michael@0: if (aflag) { michael@0: /* get all available zones */ michael@0: char ** fakeargv; michael@0: int i; michael@0: int count; michael@0: michael@0: count = getall(&namelist); michael@0: michael@0: fakeargv = (char **) malloc((size_t) (argc + count) * sizeof *argv); michael@0: /* michael@0: if ((fakeargv = (char **) malloc((size_t) (argc + count) * sizeof *argv)) == NULL) { michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: */ michael@0: for (i = 0; i < argc; i++) { michael@0: fakeargv[i] = argv[i]; michael@0: } michael@0: for (i = 0; i < count; i++) { michael@0: fakeargv[i + argc] = namelist->name; michael@0: namelist = namelist->next; michael@0: } michael@0: argv = fakeargv; michael@0: argc += count; michael@0: } michael@0: #endif michael@0: (void) time(&now); michael@0: longest = 0; michael@0: for (i = optind; i < argc; ++i) michael@0: if (strlen(argv[i]) > longest) michael@0: longest = strlen(argv[i]); michael@0: { michael@0: register int from; michael@0: register int to; michael@0: michael@0: for (i = 0; environ[i] != NULL; ++i) michael@0: continue; michael@0: fakeenv = (char **) malloc((size_t) ((i + 2) * michael@0: sizeof *fakeenv)); michael@0: if (fakeenv == NULL || michael@0: (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) { michael@0: (void) perror(progname); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: to = 0; michael@0: (void) strcpy(fakeenv[to++], "TZ="); michael@0: for (from = 0; environ[from] != NULL; ++from) michael@0: if (strncmp(environ[from], "TZ=", 3) != 0) michael@0: fakeenv[to++] = environ[from]; michael@0: fakeenv[to] = NULL; michael@0: environ = fakeenv; michael@0: } michael@0: for (i = optind; i < argc; ++i) { michael@0: static char buf[MAX_STRING_LENGTH]; michael@0: michael@0: (void) strcpy(&fakeenv[0][3], argv[i]); michael@0: if (!vflag) { michael@0: show(argv[i], now, FALSE); michael@0: continue; michael@0: } michael@0: #ifdef ICU michael@0: fp = NULL; michael@0: if (iflag) { michael@0: if (dirarg == NULL) { michael@0: /* we want to display a zone name here */ michael@0: if (i != optind) { michael@0: printf("\n"); michael@0: } michael@0: printf("ZONE: %s\n", argv[i]); michael@0: } else { michael@0: int zstart; michael@0: char path[FILENAME_MAX + 1]; michael@0: strcpy(path, dirarg); michael@0: strcat(path, "/"); michael@0: zstart = strlen(path); michael@0: strcat(path, argv[i]); michael@0: /* replace '/' with '-' */ michael@0: while(path[++zstart] != 0) { michael@0: if (path[zstart] == '/') { michael@0: path[zstart] = '-'; michael@0: } michael@0: } michael@0: if ((fp = fopen(path, "w")) == NULL) { michael@0: fprintf(stderr, "cannot create output file %s\n", path); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: warned = FALSE; michael@0: t = absolute_min_time; michael@0: #ifdef ICU michael@0: /* skip displaying info for the lowest time, which is actually not michael@0: * a transition when -i option is set */ michael@0: if (!iflag) { michael@0: #endif michael@0: show(argv[i], t, TRUE); michael@0: t += SECSPERHOUR * HOURSPERDAY; michael@0: show(argv[i], t, TRUE); michael@0: #ifdef ICU michael@0: } michael@0: #endif michael@0: if (t < cutlotime) michael@0: t = cutlotime; michael@0: tmp = my_localtime(&t); michael@0: if (tmp != NULL) { michael@0: tm = *tmp; michael@0: (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1); michael@0: } michael@0: for ( ; ; ) { michael@0: if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12) michael@0: break; michael@0: newt = t + SECSPERHOUR * 12; michael@0: newtmp = localtime(&newt); michael@0: if (newtmp != NULL) michael@0: newtm = *newtmp; michael@0: #ifdef ICU michael@0: if (iflag) { michael@0: /* We do not want to capture transitions just for michael@0: * abbreviated zone name changes */ michael@0: if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) : michael@0: (delta(&newtm, &tm) != (newt - t) || michael@0: newtm.tm_isdst != tm.tm_isdst)) { michael@0: newt = huntICU(argv[i], t, newt, fp); michael@0: newtmp = localtime(&newt); michael@0: if (newtmp != NULL) { michael@0: newtm = *newtmp; michael@0: (void) strncpy(buf, michael@0: abbr(&newtm), michael@0: (sizeof buf) - 1); michael@0: } michael@0: } michael@0: } else { michael@0: #endif michael@0: if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) : michael@0: (delta(&newtm, &tm) != (newt - t) || michael@0: newtm.tm_isdst != tm.tm_isdst || michael@0: strcmp(abbr(&newtm), buf) != 0)) { michael@0: newt = hunt(argv[i], t, newt); michael@0: newtmp = localtime(&newt); michael@0: if (newtmp != NULL) { michael@0: newtm = *newtmp; michael@0: (void) strncpy(buf, michael@0: abbr(&newtm), michael@0: (sizeof buf) - 1); michael@0: } michael@0: } michael@0: #ifdef ICU michael@0: } michael@0: #endif michael@0: t = newt; michael@0: tm = newtm; michael@0: tmp = newtmp; michael@0: } michael@0: #ifdef ICU michael@0: if (!iflag) { michael@0: /* skip displaying info for the highest time, which is actually not michael@0: * a transition when -i option is used*/ michael@0: #endif michael@0: t = absolute_max_time; michael@0: t -= SECSPERHOUR * HOURSPERDAY; michael@0: show(argv[i], t, TRUE); michael@0: t += SECSPERHOUR * HOURSPERDAY; michael@0: show(argv[i], t, TRUE); michael@0: michael@0: #ifdef ICU michael@0: } michael@0: /* close file */ michael@0: if (fp != NULL) { michael@0: fclose(fp); michael@0: } michael@0: #endif michael@0: } michael@0: if (fflush(stdout) || ferror(stdout)) { michael@0: (void) fprintf(stderr, "%s: ", progname); michael@0: (void) perror(_("Error writing to standard output")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: #ifdef ICU michael@0: if (aflag) { michael@0: struct listentry * entry = namelist; michael@0: struct listentry * next; michael@0: while (entry != NULL) { michael@0: free(entry->name); michael@0: next = entry->next; michael@0: free(entry); michael@0: entry = next; michael@0: } michael@0: } michael@0: #endif michael@0: exit(EXIT_SUCCESS); michael@0: /* If exit fails to exit... */ michael@0: return EXIT_FAILURE; michael@0: } michael@0: michael@0: static void michael@0: setabsolutes(void) michael@0: { michael@0: if (0.5 == (time_t) 0.5) { michael@0: /* michael@0: ** time_t is floating. michael@0: */ michael@0: if (sizeof (time_t) == sizeof (float)) { michael@0: absolute_min_time = (time_t) -FLT_MAX; michael@0: absolute_max_time = (time_t) FLT_MAX; michael@0: } else if (sizeof (time_t) == sizeof (double)) { michael@0: absolute_min_time = (time_t) -DBL_MAX; michael@0: absolute_max_time = (time_t) DBL_MAX; michael@0: } else { michael@0: (void) fprintf(stderr, michael@0: _("%s: use of -v on system with floating time_t other than float or double\n"), michael@0: progname); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } else if (0 > (time_t) -1) { michael@0: /* michael@0: ** time_t is signed. Assume overflow wraps around. michael@0: */ michael@0: time_t t = 0; michael@0: time_t t1 = 1; michael@0: michael@0: while (t < t1) { michael@0: t = t1; michael@0: t1 = 2 * t1 + 1; michael@0: } michael@0: michael@0: absolute_max_time = t; michael@0: t = -t; michael@0: absolute_min_time = t - 1; michael@0: if (t < absolute_min_time) michael@0: absolute_min_time = t; michael@0: } else { michael@0: /* michael@0: ** time_t is unsigned. michael@0: */ michael@0: absolute_min_time = 0; michael@0: absolute_max_time = absolute_min_time - 1; michael@0: } michael@0: } michael@0: michael@0: static time_t michael@0: yeartot(y) michael@0: const long y; michael@0: { michael@0: register long myy; michael@0: register long seconds; michael@0: register time_t t; michael@0: michael@0: myy = EPOCH_YEAR; michael@0: t = 0; michael@0: while (myy != y) { michael@0: if (myy < y) { michael@0: seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; michael@0: ++myy; michael@0: if (t > absolute_max_time - seconds) { michael@0: t = absolute_max_time; michael@0: break; michael@0: } michael@0: t += seconds; michael@0: } else { michael@0: --myy; michael@0: seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; michael@0: if (t < absolute_min_time + seconds) { michael@0: t = absolute_min_time; michael@0: break; michael@0: } michael@0: t -= seconds; michael@0: } michael@0: } michael@0: return t; michael@0: } michael@0: michael@0: static time_t michael@0: hunt(char *name, time_t lot, time_t hit) michael@0: { michael@0: time_t t; michael@0: long diff; michael@0: struct tm lotm; michael@0: register struct tm * lotmp; michael@0: struct tm tm; michael@0: register struct tm * tmp; michael@0: char loab[MAX_STRING_LENGTH]; michael@0: michael@0: lotmp = my_localtime(&lot); michael@0: if (lotmp != NULL) { michael@0: lotm = *lotmp; michael@0: (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1); michael@0: } michael@0: for ( ; ; ) { michael@0: diff = (long) (hit - lot); michael@0: if (diff < 2) michael@0: break; michael@0: t = lot; michael@0: t += diff / 2; michael@0: if (t <= lot) michael@0: ++t; michael@0: else if (t >= hit) michael@0: --t; michael@0: tmp = my_localtime(&t); michael@0: if (tmp != NULL) michael@0: tm = *tmp; michael@0: if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) : michael@0: (delta(&tm, &lotm) == (t - lot) && michael@0: tm.tm_isdst == lotm.tm_isdst && michael@0: strcmp(abbr(&tm), loab) == 0)) { michael@0: lot = t; michael@0: lotm = tm; michael@0: lotmp = tmp; michael@0: } else hit = t; michael@0: } michael@0: show(name, lot, TRUE); michael@0: show(name, hit, TRUE); michael@0: return hit; michael@0: } michael@0: michael@0: /* michael@0: ** Thanks to Paul Eggert for logic used in delta. michael@0: */ michael@0: michael@0: static long michael@0: delta(newp, oldp) michael@0: struct tm * newp; michael@0: struct tm * oldp; michael@0: { michael@0: register long result; michael@0: register int tmy; michael@0: michael@0: if (newp->tm_year < oldp->tm_year) michael@0: return -delta(oldp, newp); michael@0: result = 0; michael@0: for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy) michael@0: result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE); michael@0: result += newp->tm_yday - oldp->tm_yday; michael@0: result *= HOURSPERDAY; michael@0: result += newp->tm_hour - oldp->tm_hour; michael@0: result *= MINSPERHOUR; michael@0: result += newp->tm_min - oldp->tm_min; michael@0: result *= SECSPERMIN; michael@0: result += newp->tm_sec - oldp->tm_sec; michael@0: return result; michael@0: } michael@0: michael@0: static void michael@0: show(char *zone, time_t t, int v) michael@0: { michael@0: register struct tm * tmp; michael@0: michael@0: (void) printf("%-*s ", (int) longest, zone); michael@0: if (v) { michael@0: tmp = gmtime(&t); michael@0: if (tmp == NULL) { michael@0: (void) printf(tformat(), t); michael@0: } else { michael@0: dumptime(tmp); michael@0: (void) printf(" UTC"); michael@0: } michael@0: (void) printf(" = "); michael@0: } michael@0: tmp = my_localtime(&t); michael@0: dumptime(tmp); michael@0: if (tmp != NULL) { michael@0: if (*abbr(tmp) != '\0') michael@0: (void) printf(" %s", abbr(tmp)); michael@0: if (v) { michael@0: (void) printf(" isdst=%d", tmp->tm_isdst); michael@0: #ifdef TM_GMTOFF michael@0: (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF); michael@0: #endif /* defined TM_GMTOFF */ michael@0: } michael@0: } michael@0: (void) printf("\n"); michael@0: if (tmp != NULL && *abbr(tmp) != '\0') michael@0: abbrok(abbr(tmp), zone); michael@0: } michael@0: michael@0: static char * michael@0: abbr(tmp) michael@0: struct tm * tmp; michael@0: { michael@0: register char * result; michael@0: static char nada; michael@0: michael@0: if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1) michael@0: return &nada; michael@0: result = tzname[tmp->tm_isdst]; michael@0: return (result == NULL) ? &nada : result; michael@0: } michael@0: michael@0: /* michael@0: ** The code below can fail on certain theoretical systems; michael@0: ** it works on all known real-world systems as of 2004-12-30. michael@0: */ michael@0: michael@0: static const char * michael@0: tformat(void) michael@0: { michael@0: if (0.5 == (time_t) 0.5) { /* floating */ michael@0: if (sizeof (time_t) > sizeof (double)) michael@0: return "%Lg"; michael@0: return "%g"; michael@0: } michael@0: if (0 > (time_t) -1) { /* signed */ michael@0: if (sizeof (time_t) > sizeof (long)) michael@0: return "%lld"; michael@0: if (sizeof (time_t) > sizeof (int)) michael@0: return "%ld"; michael@0: return "%d"; michael@0: } michael@0: if (sizeof (time_t) > sizeof (unsigned long)) michael@0: return "%llu"; michael@0: if (sizeof (time_t) > sizeof (unsigned int)) michael@0: return "%lu"; michael@0: return "%u"; michael@0: } michael@0: michael@0: static void michael@0: dumptime(timeptr) michael@0: register const struct tm * timeptr; michael@0: { michael@0: static const char wday_name[][3] = { michael@0: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" michael@0: }; michael@0: static const char mon_name[][3] = { michael@0: "Jan", "Feb", "Mar", "Apr", "May", "Jun", michael@0: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" michael@0: }; michael@0: register const char * wn; michael@0: register const char * mn; michael@0: register int lead; michael@0: register int trail; michael@0: michael@0: if (timeptr == NULL) { michael@0: (void) printf("NULL"); michael@0: return; michael@0: } michael@0: /* michael@0: ** The packaged versions of localtime and gmtime never put out-of-range michael@0: ** values in tm_wday or tm_mon, but since this code might be compiled michael@0: ** with other (perhaps experimental) versions, paranoia is in order. michael@0: */ michael@0: if (timeptr->tm_wday < 0 || timeptr->tm_wday >= michael@0: (int) (sizeof wday_name / sizeof wday_name[0])) michael@0: wn = "???"; michael@0: else wn = wday_name[timeptr->tm_wday]; michael@0: if (timeptr->tm_mon < 0 || timeptr->tm_mon >= michael@0: (int) (sizeof mon_name / sizeof mon_name[0])) michael@0: mn = "???"; michael@0: else mn = mon_name[timeptr->tm_mon]; michael@0: (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ", michael@0: wn, mn, michael@0: timeptr->tm_mday, timeptr->tm_hour, michael@0: timeptr->tm_min, timeptr->tm_sec); michael@0: #define DIVISOR 10 michael@0: trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR; michael@0: lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + michael@0: trail / DIVISOR; michael@0: trail %= DIVISOR; michael@0: if (trail < 0 && lead > 0) { michael@0: trail += DIVISOR; michael@0: --lead; michael@0: } else if (lead < 0 && trail > 0) { michael@0: trail -= DIVISOR; michael@0: ++lead; michael@0: } michael@0: if (lead == 0) michael@0: (void) printf("%d", trail); michael@0: else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail)); michael@0: } michael@0: michael@0: #ifdef ICU michael@0: static time_t michael@0: huntICU(char *name, time_t lot, time_t hit, FILE * fp) michael@0: { michael@0: time_t t; michael@0: long diff; michael@0: struct tm lotm; michael@0: register struct tm * lotmp; michael@0: struct tm tm; michael@0: register struct tm * tmp; michael@0: char loab[MAX_STRING_LENGTH]; michael@0: michael@0: lotmp = my_localtime(&lot); michael@0: if (lotmp != NULL) { michael@0: lotm = *lotmp; michael@0: (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1); michael@0: } michael@0: for ( ; ; ) { michael@0: diff = (long) (hit - lot); michael@0: if (diff < 2) michael@0: break; michael@0: t = lot; michael@0: t += diff / 2; michael@0: if (t <= lot) michael@0: ++t; michael@0: else if (t >= hit) michael@0: --t; michael@0: tmp = my_localtime(&t); michael@0: if (tmp != NULL) michael@0: tm = *tmp; michael@0: /* We do not want to capture transitions just for michael@0: * abbreviated zone name changes */ michael@0: if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) : michael@0: (delta(&tm, &lotm) == (t - lot) && michael@0: tm.tm_isdst == lotm.tm_isdst)) { michael@0: lot = t; michael@0: lotm = tm; michael@0: lotmp = tmp; michael@0: } else hit = t; michael@0: } michael@0: showICU(fp, name, lot, hit); michael@0: return hit; michael@0: } michael@0: michael@0: static void showICU(FILE * fp, char *zone, time_t t1, time_t t2) michael@0: { michael@0: if (fp == NULL) { michael@0: fp = stdout; michael@0: } michael@0: dumptimeICU(fp, t1); michael@0: fprintf(fp, " > "); michael@0: dumptimeICU(fp, t2); michael@0: fprintf(fp, "\n"); michael@0: } michael@0: michael@0: static void dumptimeICU(FILE * fp, time_t t) michael@0: { michael@0: static const char wday_name[][3] = { michael@0: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" michael@0: }; michael@0: struct tm gmt; michael@0: struct tm loc; michael@0: register int lead; michael@0: register int trail; michael@0: long offset; michael@0: long hour, min, sec; michael@0: michael@0: loc = *my_localtime(&t); michael@0: michael@0: trail = loc.tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR; michael@0: lead = loc.tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + trail / DIVISOR; michael@0: trail %= DIVISOR; michael@0: if (trail < 0 && lead > 0) { michael@0: trail += DIVISOR; michael@0: --lead; michael@0: } else if (lead < 0 && trail > 0) { michael@0: trail -= DIVISOR; michael@0: ++lead; michael@0: } michael@0: michael@0: fprintf(fp, "%04d-%02d-%02d", lead * DIVISOR + trail, loc.tm_mon + 1, loc.tm_mday); michael@0: fprintf(fp, " %.3s ", wday_name[loc.tm_wday]); michael@0: fprintf(fp, "%02d:%02d:%02d", loc.tm_hour, loc.tm_min, loc.tm_sec); michael@0: michael@0: gmt = *gmtime(&t); michael@0: offset = delta(&loc, &gmt); michael@0: if (offset < 0) { michael@0: offset = -offset; michael@0: fprintf(fp, "-"); michael@0: } else { michael@0: fprintf(fp, "+"); michael@0: } michael@0: michael@0: sec = offset % 60; michael@0: offset = (offset - sec) / 60; michael@0: min = offset % 60; michael@0: hour = offset / 60; michael@0: michael@0: fprintf(fp, "%02d", hour); michael@0: fprintf(fp, "%02d", min); michael@0: fprintf(fp, "%02d", sec); michael@0: fprintf(fp, "[DST=%d]", loc.tm_isdst); michael@0: } michael@0: michael@0: static int getall(struct listentry ** namelist) { michael@0: int count = 0; michael@0: struct listentry dummyentry; michael@0: struct listentry * last = &dummyentry; michael@0: michael@0: getzones(TZDIR, NULL, &last, &count); michael@0: if (count > 0) { michael@0: *namelist = dummyentry.next; michael@0: } michael@0: michael@0: return count; michael@0: } michael@0: michael@0: static void getzones(char * basedir, char * relpath, struct listentry ** last, int * count) { michael@0: char path[FILENAME_MAX + 1]; michael@0: struct dirent * dir; michael@0: DIR * dp; michael@0: michael@0: strcpy(path, basedir); michael@0: if (relpath != NULL) { michael@0: strcat(path, "/"); michael@0: strcat(path, relpath); michael@0: } michael@0: michael@0: if ((dp = opendir(path)) == NULL) { michael@0: /* file */ michael@0: if (strstr(relpath, ".tab") == NULL && strcmp(relpath, "Etc/Unknown") != 0) { michael@0: char * pzonename; michael@0: listentry * pentry; michael@0: michael@0: if ((pzonename = malloc(strlen(relpath) + 1)) == NULL) { michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: strcpy(pzonename, relpath); michael@0: michael@0: if ((pentry = malloc(sizeof(listentry))) == NULL) { michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: michael@0: pentry->name = pzonename; michael@0: pentry->next = NULL; michael@0: (*last)->next = pentry; michael@0: *last = pentry; michael@0: (*count)++; michael@0: } michael@0: } else { michael@0: /* directory */ michael@0: while ((dir = readdir(dp)) != NULL) { michael@0: char subpath[FILENAME_MAX + 1]; michael@0: michael@0: if (strcmp(dir->d_name, ".") == 0 michael@0: || strcmp(dir->d_name, "..") == 0) { michael@0: continue; michael@0: } michael@0: if (relpath != NULL) { michael@0: strcpy(subpath, relpath); michael@0: strcat(subpath, "/"); michael@0: strcat(subpath, dir->d_name); michael@0: } else { michael@0: strcpy(subpath, dir->d_name); michael@0: } michael@0: getzones(basedir, subpath, last, count); michael@0: } michael@0: closedir(dp); michael@0: } michael@0: } michael@0: #endif