michael@0: /* michael@0: ** This file is in the public domain, so clarified as of michael@0: ** 1996-06-05 by Arthur David Olson. michael@0: */ michael@0: michael@0: #ifndef lint michael@0: #ifndef NOID michael@0: static char elsieid[] = "@(#)localtime.c 8.9"; michael@0: #endif /* !defined NOID */ michael@0: #endif /* !defined lint */ michael@0: michael@0: /* michael@0: ** Leap second handling from Bradley White. michael@0: ** POSIX-style TZ environment variable handling from Guy Harris. michael@0: */ michael@0: michael@0: /*LINTLIBRARY*/ michael@0: michael@0: #include "private.h" michael@0: #include "tzfile.h" michael@0: #include "fcntl.h" michael@0: #include "float.h" /* for FLT_MAX and DBL_MAX */ michael@0: michael@0: #ifndef TZ_ABBR_MAX_LEN michael@0: #define TZ_ABBR_MAX_LEN 16 michael@0: #endif /* !defined TZ_ABBR_MAX_LEN */ michael@0: michael@0: #ifndef TZ_ABBR_CHAR_SET michael@0: #define TZ_ABBR_CHAR_SET \ michael@0: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._" michael@0: #endif /* !defined TZ_ABBR_CHAR_SET */ michael@0: michael@0: #ifndef TZ_ABBR_ERR_CHAR michael@0: #define TZ_ABBR_ERR_CHAR '_' michael@0: #endif /* !defined TZ_ABBR_ERR_CHAR */ michael@0: michael@0: /* michael@0: ** SunOS 4.1.1 headers lack O_BINARY. michael@0: */ michael@0: michael@0: #ifdef O_BINARY michael@0: #define OPEN_MODE (O_RDONLY | O_BINARY) michael@0: #endif /* defined O_BINARY */ michael@0: #ifndef O_BINARY michael@0: #define OPEN_MODE O_RDONLY michael@0: #endif /* !defined O_BINARY */ michael@0: michael@0: #ifndef WILDABBR michael@0: /* michael@0: ** Someone might make incorrect use of a time zone abbreviation: michael@0: ** 1. They might reference tzname[0] before calling tzset (explicitly michael@0: ** or implicitly). michael@0: ** 2. They might reference tzname[1] before calling tzset (explicitly michael@0: ** or implicitly). michael@0: ** 3. They might reference tzname[1] after setting to a time zone michael@0: ** in which Daylight Saving Time is never observed. michael@0: ** 4. They might reference tzname[0] after setting to a time zone michael@0: ** in which Standard Time is never observed. michael@0: ** 5. They might reference tm.TM_ZONE after calling offtime. michael@0: ** What's best to do in the above cases is open to debate; michael@0: ** for now, we just set things up so that in any of the five cases michael@0: ** WILDABBR is used. Another possibility: initialize tzname[0] to the michael@0: ** string "tzname[0] used before set", and similarly for the other cases. michael@0: ** And another: initialize tzname[0] to "ERA", with an explanation in the michael@0: ** manual page of what this "time zone abbreviation" means (doing this so michael@0: ** that tzname[0] has the "normal" length of three characters). michael@0: */ michael@0: #define WILDABBR " " michael@0: #endif /* !defined WILDABBR */ michael@0: michael@0: static char wildabbr[] = WILDABBR; michael@0: michael@0: static const char gmt[] = "GMT"; michael@0: michael@0: /* michael@0: ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES. michael@0: ** We default to US rules as of 1999-08-17. michael@0: ** POSIX 1003.1 section 8.1.1 says that the default DST rules are michael@0: ** implementation dependent; for historical reasons, US rules are a michael@0: ** common default. michael@0: */ michael@0: #ifndef TZDEFRULESTRING michael@0: #define TZDEFRULESTRING ",M4.1.0,M10.5.0" michael@0: #endif /* !defined TZDEFDST */ michael@0: michael@0: struct ttinfo { /* time type information */ michael@0: long tt_gmtoff; /* UTC offset in seconds */ michael@0: int tt_isdst; /* used to set tm_isdst */ michael@0: int tt_abbrind; /* abbreviation list index */ michael@0: int tt_ttisstd; /* TRUE if transition is std time */ michael@0: int tt_ttisgmt; /* TRUE if transition is UTC */ michael@0: }; michael@0: michael@0: struct lsinfo { /* leap second information */ michael@0: time_t ls_trans; /* transition time */ michael@0: long ls_corr; /* correction to apply */ michael@0: }; michael@0: michael@0: #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) michael@0: michael@0: #ifdef TZNAME_MAX michael@0: #define MY_TZNAME_MAX TZNAME_MAX michael@0: #endif /* defined TZNAME_MAX */ michael@0: #ifndef TZNAME_MAX michael@0: #define MY_TZNAME_MAX 255 michael@0: #endif /* !defined TZNAME_MAX */ michael@0: michael@0: struct state { michael@0: int leapcnt; michael@0: int timecnt; michael@0: int typecnt; michael@0: int charcnt; michael@0: int goback; michael@0: int goahead; michael@0: time_t ats[TZ_MAX_TIMES]; michael@0: unsigned char types[TZ_MAX_TIMES]; michael@0: struct ttinfo ttis[TZ_MAX_TYPES]; michael@0: char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), michael@0: (2 * (MY_TZNAME_MAX + 1)))]; michael@0: struct lsinfo lsis[TZ_MAX_LEAPS]; michael@0: }; michael@0: michael@0: struct rule { michael@0: int r_type; /* type of rule--see below */ michael@0: int r_day; /* day number of rule */ michael@0: int r_week; /* week number of rule */ michael@0: int r_mon; /* month number of rule */ michael@0: long r_time; /* transition time of rule */ michael@0: }; michael@0: michael@0: #define JULIAN_DAY 0 /* Jn - Julian day */ michael@0: #define DAY_OF_YEAR 1 /* n - day of year */ michael@0: #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ michael@0: michael@0: /* michael@0: ** Prototypes for static functions. michael@0: */ michael@0: michael@0: static long detzcode(const char * codep); michael@0: static time_t detzcode64(const char * codep); michael@0: static int differ_by_repeat(time_t t1, time_t t0); michael@0: static const char * getzname(const char * strp); michael@0: static const char * getqzname(const char * strp, const int delim); michael@0: static const char * getnum(const char * strp, int * nump, int min, michael@0: int max); michael@0: static const char * getsecs(const char * strp, long * secsp); michael@0: static const char * getoffset(const char * strp, long * offsetp); michael@0: static const char * getrule(const char * strp, struct rule * rulep); michael@0: static void gmtload(struct state * sp); michael@0: static struct tm * gmtsub(const time_t * timep, long offset, michael@0: struct tm * tmp); michael@0: static struct tm * localsub(const time_t * timep, long offset, michael@0: struct tm * tmp); michael@0: static int increment_overflow(int * number, int delta); michael@0: static int leaps_thru_end_of(int y); michael@0: static int long_increment_overflow(long * number, int delta); michael@0: static int long_normalize_overflow(long * tensptr, michael@0: int * unitsptr, int base); michael@0: static int normalize_overflow(int * tensptr, int * unitsptr, michael@0: int base); michael@0: static void settzname(void); michael@0: static time_t time1(struct tm * tmp, michael@0: struct tm * (*funcp)(const time_t *, michael@0: long, struct tm *), michael@0: long offset); michael@0: static time_t time2(struct tm *tmp, michael@0: struct tm * (*funcp)(const time_t *, michael@0: long, struct tm*), michael@0: long offset, int * okayp); michael@0: static time_t time2sub(struct tm *tmp, michael@0: struct tm * (*funcp)(const time_t *, michael@0: long, struct tm*), michael@0: long offset, int * okayp, int do_norm_secs); michael@0: static struct tm * timesub(const time_t * timep, long offset, michael@0: const struct state * sp, struct tm * tmp); michael@0: static int tmcomp(const struct tm * atmp, michael@0: const struct tm * btmp); michael@0: static time_t transtime(time_t janfirst, int year, michael@0: const struct rule * rulep, long offset); michael@0: static int typesequiv(const struct state * sp, int a, int b); michael@0: static int tzload(const char * name, struct state * sp, michael@0: int doextend); michael@0: static int tzparse(const char * name, struct state * sp, michael@0: int lastditch); michael@0: michael@0: #ifdef ALL_STATE michael@0: static struct state * lclptr; michael@0: static struct state * gmtptr; michael@0: #endif /* defined ALL_STATE */ michael@0: michael@0: #ifndef ALL_STATE michael@0: static struct state lclmem; michael@0: static struct state gmtmem; michael@0: #define lclptr (&lclmem) michael@0: #define gmtptr (&gmtmem) michael@0: #endif /* State Farm */ michael@0: michael@0: #ifndef TZ_STRLEN_MAX michael@0: #define TZ_STRLEN_MAX 255 michael@0: #endif /* !defined TZ_STRLEN_MAX */ michael@0: michael@0: static char lcl_TZname[TZ_STRLEN_MAX + 1]; michael@0: static int lcl_is_set; michael@0: static int gmt_is_set; michael@0: michael@0: char * tzname[2] = { michael@0: wildabbr, michael@0: wildabbr michael@0: }; michael@0: michael@0: /* michael@0: ** Section 4.12.3 of X3.159-1989 requires that michael@0: ** Except for the strftime function, these functions [asctime, michael@0: ** ctime, gmtime, localtime] return values in one of two static michael@0: ** objects: a broken-down time structure and an array of char. michael@0: ** Thanks to Paul Eggert for noting this. michael@0: */ michael@0: michael@0: static struct tm tm; michael@0: michael@0: #ifdef USG_COMPAT michael@0: time_t timezone = 0; michael@0: int daylight = 0; michael@0: #endif /* defined USG_COMPAT */ michael@0: michael@0: #ifdef ALTZONE michael@0: time_t altzone = 0; michael@0: #endif /* defined ALTZONE */ michael@0: michael@0: static long michael@0: detzcode(codep) michael@0: const char * const codep; michael@0: { michael@0: register long result; michael@0: register int i; michael@0: michael@0: result = (codep[0] & 0x80) ? ~0L : 0; michael@0: for (i = 0; i < 4; ++i) michael@0: result = (result << 8) | (codep[i] & 0xff); michael@0: return result; michael@0: } michael@0: michael@0: static time_t michael@0: detzcode64(codep) michael@0: const char * const codep; michael@0: { michael@0: register time_t result; michael@0: register int i; michael@0: michael@0: result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0; michael@0: for (i = 0; i < 8; ++i) michael@0: result = result * 256 + (codep[i] & 0xff); michael@0: return result; michael@0: } michael@0: michael@0: static void michael@0: settzname(void) michael@0: { michael@0: register struct state * const sp = lclptr; michael@0: register int i; michael@0: michael@0: tzname[0] = wildabbr; michael@0: tzname[1] = wildabbr; michael@0: #ifdef USG_COMPAT michael@0: daylight = 0; michael@0: timezone = 0; michael@0: #endif /* defined USG_COMPAT */ michael@0: #ifdef ALTZONE michael@0: altzone = 0; michael@0: #endif /* defined ALTZONE */ michael@0: #ifdef ALL_STATE michael@0: if (sp == NULL) { michael@0: tzname[0] = tzname[1] = gmt; michael@0: return; michael@0: } michael@0: #endif /* defined ALL_STATE */ michael@0: for (i = 0; i < sp->typecnt; ++i) { michael@0: register const struct ttinfo * const ttisp = &sp->ttis[i]; michael@0: michael@0: tzname[ttisp->tt_isdst] = michael@0: &sp->chars[ttisp->tt_abbrind]; michael@0: #ifdef USG_COMPAT michael@0: if (ttisp->tt_isdst) michael@0: daylight = 1; michael@0: if (i == 0 || !ttisp->tt_isdst) michael@0: timezone = -(ttisp->tt_gmtoff); michael@0: #endif /* defined USG_COMPAT */ michael@0: #ifdef ALTZONE michael@0: if (i == 0 || ttisp->tt_isdst) michael@0: altzone = -(ttisp->tt_gmtoff); michael@0: #endif /* defined ALTZONE */ michael@0: } michael@0: /* michael@0: ** And to get the latest zone names into tzname. . . michael@0: */ michael@0: for (i = 0; i < sp->timecnt; ++i) { michael@0: register const struct ttinfo * const ttisp = michael@0: &sp->ttis[ michael@0: sp->types[i]]; michael@0: michael@0: tzname[ttisp->tt_isdst] = michael@0: &sp->chars[ttisp->tt_abbrind]; michael@0: } michael@0: /* michael@0: ** Finally, scrub the abbreviations. michael@0: ** First, replace bogus characters. michael@0: */ michael@0: for (i = 0; i < sp->charcnt; ++i) michael@0: if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL) michael@0: sp->chars[i] = TZ_ABBR_ERR_CHAR; michael@0: /* michael@0: ** Second, truncate long abbreviations. michael@0: */ michael@0: for (i = 0; i < sp->typecnt; ++i) { michael@0: register const struct ttinfo * const ttisp = &sp->ttis[i]; michael@0: register char * cp = &sp->chars[ttisp->tt_abbrind]; michael@0: michael@0: if (strlen(cp) > TZ_ABBR_MAX_LEN && michael@0: strcmp(cp, GRANDPARENTED) != 0) michael@0: *(cp + TZ_ABBR_MAX_LEN) = '\0'; michael@0: } michael@0: } michael@0: michael@0: static int michael@0: differ_by_repeat(t1, t0) michael@0: const time_t t1; michael@0: const time_t t0; michael@0: { michael@0: if (TYPE_INTEGRAL(time_t) && michael@0: TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) michael@0: return 0; michael@0: return t1 - t0 == SECSPERREPEAT; michael@0: } michael@0: michael@0: static int michael@0: tzload(name, sp, doextend) michael@0: register const char * name; michael@0: register struct state * const sp; michael@0: register const int doextend; michael@0: { michael@0: register const char * p; michael@0: register int i; michael@0: register int fid; michael@0: register int stored; michael@0: register int nread; michael@0: union { michael@0: struct tzhead tzhead; michael@0: char buf[2 * sizeof(struct tzhead) + michael@0: 2 * sizeof *sp + michael@0: 4 * TZ_MAX_TIMES]; michael@0: } u; michael@0: michael@0: if (name == NULL && (name = TZDEFAULT) == NULL) michael@0: return -1; michael@0: { michael@0: register int doaccess; michael@0: /* michael@0: ** Section 4.9.1 of the C standard says that michael@0: ** "FILENAME_MAX expands to an integral constant expression michael@0: ** that is the size needed for an array of char large enough michael@0: ** to hold the longest file name string that the implementation michael@0: ** guarantees can be opened." michael@0: */ michael@0: char fullname[FILENAME_MAX + 1]; michael@0: michael@0: if (name[0] == ':') michael@0: ++name; michael@0: doaccess = name[0] == '/'; michael@0: if (!doaccess) { michael@0: if ((p = TZDIR) == NULL) michael@0: return -1; michael@0: if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) michael@0: return -1; michael@0: (void) strcpy(fullname, p); michael@0: (void) strcat(fullname, "/"); michael@0: (void) strcat(fullname, name); michael@0: /* michael@0: ** Set doaccess if '.' (as in "../") shows up in name. michael@0: */ michael@0: if (strchr(name, '.') != NULL) michael@0: doaccess = TRUE; michael@0: name = fullname; michael@0: } michael@0: if (doaccess && access(name, R_OK) != 0) michael@0: return -1; michael@0: if ((fid = open(name, OPEN_MODE)) == -1) michael@0: return -1; michael@0: } michael@0: nread = read(fid, u.buf, sizeof u.buf); michael@0: if (close(fid) < 0 || nread <= 0) michael@0: return -1; michael@0: for (stored = 4; stored <= 8; stored *= 2) { michael@0: int ttisstdcnt; michael@0: int ttisgmtcnt; michael@0: michael@0: ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt); michael@0: ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt); michael@0: sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt); michael@0: sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt); michael@0: sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt); michael@0: sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt); michael@0: p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt; michael@0: if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || michael@0: sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || michael@0: sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || michael@0: sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || michael@0: (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || michael@0: (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) michael@0: return -1; michael@0: if (nread - (p - u.buf) < michael@0: sp->timecnt * stored + /* ats */ michael@0: sp->timecnt + /* types */ michael@0: sp->typecnt * 6 + /* ttinfos */ michael@0: sp->charcnt + /* chars */ michael@0: sp->leapcnt * (stored + 4) + /* lsinfos */ michael@0: ttisstdcnt + /* ttisstds */ michael@0: ttisgmtcnt) /* ttisgmts */ michael@0: return -1; michael@0: for (i = 0; i < sp->timecnt; ++i) { michael@0: sp->ats[i] = (stored == 4) ? michael@0: detzcode(p) : detzcode64(p); michael@0: p += stored; michael@0: } michael@0: for (i = 0; i < sp->timecnt; ++i) { michael@0: sp->types[i] = (unsigned char) *p++; michael@0: if (sp->types[i] >= sp->typecnt) michael@0: return -1; michael@0: } michael@0: for (i = 0; i < sp->typecnt; ++i) { michael@0: register struct ttinfo * ttisp; michael@0: michael@0: ttisp = &sp->ttis[i]; michael@0: ttisp->tt_gmtoff = detzcode(p); michael@0: p += 4; michael@0: ttisp->tt_isdst = (unsigned char) *p++; michael@0: if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) michael@0: return -1; michael@0: ttisp->tt_abbrind = (unsigned char) *p++; michael@0: if (ttisp->tt_abbrind < 0 || michael@0: ttisp->tt_abbrind > sp->charcnt) michael@0: return -1; michael@0: } michael@0: for (i = 0; i < sp->charcnt; ++i) michael@0: sp->chars[i] = *p++; michael@0: sp->chars[i] = '\0'; /* ensure '\0' at end */ michael@0: for (i = 0; i < sp->leapcnt; ++i) { michael@0: register struct lsinfo * lsisp; michael@0: michael@0: lsisp = &sp->lsis[i]; michael@0: lsisp->ls_trans = (stored == 4) ? michael@0: detzcode(p) : detzcode64(p); michael@0: p += stored; michael@0: lsisp->ls_corr = detzcode(p); michael@0: p += 4; michael@0: } michael@0: for (i = 0; i < sp->typecnt; ++i) { michael@0: register struct ttinfo * ttisp; michael@0: michael@0: ttisp = &sp->ttis[i]; michael@0: if (ttisstdcnt == 0) michael@0: ttisp->tt_ttisstd = FALSE; michael@0: else { michael@0: ttisp->tt_ttisstd = *p++; michael@0: if (ttisp->tt_ttisstd != TRUE && michael@0: ttisp->tt_ttisstd != FALSE) michael@0: return -1; michael@0: } michael@0: } michael@0: for (i = 0; i < sp->typecnt; ++i) { michael@0: register struct ttinfo * ttisp; michael@0: michael@0: ttisp = &sp->ttis[i]; michael@0: if (ttisgmtcnt == 0) michael@0: ttisp->tt_ttisgmt = FALSE; michael@0: else { michael@0: ttisp->tt_ttisgmt = *p++; michael@0: if (ttisp->tt_ttisgmt != TRUE && michael@0: ttisp->tt_ttisgmt != FALSE) michael@0: return -1; michael@0: } michael@0: } michael@0: /* michael@0: ** Out-of-sort ats should mean we're running on a michael@0: ** signed time_t system but using a data file with michael@0: ** unsigned values (or vice versa). michael@0: */ michael@0: for (i = 0; i < sp->timecnt - 2; ++i) michael@0: if (sp->ats[i] > sp->ats[i + 1]) { michael@0: ++i; michael@0: if (TYPE_SIGNED(time_t)) { michael@0: /* michael@0: ** Ignore the end (easy). michael@0: */ michael@0: sp->timecnt = i; michael@0: } else { michael@0: /* michael@0: ** Ignore the beginning (harder). michael@0: */ michael@0: register int j; michael@0: michael@0: for (j = 0; j + i < sp->timecnt; ++j) { michael@0: sp->ats[j] = sp->ats[j + i]; michael@0: sp->types[j] = sp->types[j + i]; michael@0: } michael@0: sp->timecnt = j; michael@0: } michael@0: break; michael@0: } michael@0: /* michael@0: ** If this is an old file, we're done. michael@0: */ michael@0: if (u.tzhead.tzh_version[0] == '\0') michael@0: break; michael@0: nread -= p - u.buf; michael@0: for (i = 0; i < nread; ++i) michael@0: u.buf[i] = p[i]; michael@0: /* michael@0: ** If this is a narrow integer time_t system, we're done. michael@0: */ michael@0: if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t)) michael@0: break; michael@0: } michael@0: if (doextend && nread > 2 && michael@0: u.buf[0] == '\n' && u.buf[nread - 1] == '\n' && michael@0: sp->typecnt + 2 <= TZ_MAX_TYPES) { michael@0: struct state ts; michael@0: register int result; michael@0: michael@0: u.buf[nread - 1] = '\0'; michael@0: result = tzparse(&u.buf[1], &ts, FALSE); michael@0: if (result == 0 && ts.typecnt == 2 && michael@0: sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) { michael@0: for (i = 0; i < 2; ++i) michael@0: ts.ttis[i].tt_abbrind += michael@0: sp->charcnt; michael@0: for (i = 0; i < ts.charcnt; ++i) michael@0: sp->chars[sp->charcnt++] = michael@0: ts.chars[i]; michael@0: i = 0; michael@0: while (i < ts.timecnt && michael@0: ts.ats[i] <= michael@0: sp->ats[sp->timecnt - 1]) michael@0: ++i; michael@0: while (i < ts.timecnt && michael@0: sp->timecnt < TZ_MAX_TIMES) { michael@0: sp->ats[sp->timecnt] = michael@0: ts.ats[i]; michael@0: sp->types[sp->timecnt] = michael@0: sp->typecnt + michael@0: ts.types[i]; michael@0: ++sp->timecnt; michael@0: ++i; michael@0: } michael@0: sp->ttis[sp->typecnt++] = ts.ttis[0]; michael@0: sp->ttis[sp->typecnt++] = ts.ttis[1]; michael@0: } michael@0: } michael@0: sp->goback = sp->goahead = FALSE; michael@0: if (sp->timecnt > 1) { michael@0: for (i = 1; i < sp->timecnt; ++i) michael@0: if (typesequiv(sp, sp->types[i], sp->types[0]) && michael@0: differ_by_repeat(sp->ats[i], sp->ats[0])) { michael@0: sp->goback = TRUE; michael@0: break; michael@0: } michael@0: for (i = sp->timecnt - 2; i >= 0; --i) michael@0: if (typesequiv(sp, sp->types[sp->timecnt - 1], michael@0: sp->types[i]) && michael@0: differ_by_repeat(sp->ats[sp->timecnt - 1], michael@0: sp->ats[i])) { michael@0: sp->goahead = TRUE; michael@0: break; michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static int michael@0: typesequiv(sp, a, b) michael@0: const struct state * const sp; michael@0: const int a; michael@0: const int b; michael@0: { michael@0: register int result; michael@0: michael@0: if (sp == NULL || michael@0: a < 0 || a >= sp->typecnt || michael@0: b < 0 || b >= sp->typecnt) michael@0: result = FALSE; michael@0: else { michael@0: register const struct ttinfo * ap = &sp->ttis[a]; michael@0: register const struct ttinfo * bp = &sp->ttis[b]; michael@0: result = ap->tt_gmtoff == bp->tt_gmtoff && michael@0: ap->tt_isdst == bp->tt_isdst && michael@0: ap->tt_ttisstd == bp->tt_ttisstd && michael@0: ap->tt_ttisgmt == bp->tt_ttisgmt && michael@0: strcmp(&sp->chars[ap->tt_abbrind], michael@0: &sp->chars[bp->tt_abbrind]) == 0; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static const int mon_lengths[2][MONSPERYEAR] = { michael@0: { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, michael@0: { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } michael@0: }; michael@0: michael@0: static const int year_lengths[2] = { michael@0: DAYSPERNYEAR, DAYSPERLYEAR michael@0: }; michael@0: michael@0: /* michael@0: ** Given a pointer into a time zone string, scan until a character that is not michael@0: ** a valid character in a zone name is found. Return a pointer to that michael@0: ** character. michael@0: */ michael@0: michael@0: static const char * michael@0: getzname(strp) michael@0: register const char * strp; michael@0: { michael@0: register char c; michael@0: michael@0: while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && michael@0: c != '+') michael@0: ++strp; michael@0: return strp; michael@0: } michael@0: michael@0: /* michael@0: ** Given a pointer into an extended time zone string, scan until the ending michael@0: ** delimiter of the zone name is located. Return a pointer to the delimiter. michael@0: ** michael@0: ** As with getzname above, the legal character set is actually quite michael@0: ** restricted, with other characters producing undefined results. michael@0: ** We don't do any checking here; checking is done later in common-case code. michael@0: */ michael@0: michael@0: static const char * michael@0: getqzname(register const char *strp, const int delim) michael@0: { michael@0: register int c; michael@0: michael@0: while ((c = *strp) != '\0' && c != delim) michael@0: ++strp; michael@0: return strp; michael@0: } michael@0: michael@0: /* michael@0: ** Given a pointer into a time zone string, extract a number from that string. michael@0: ** Check that the number is within a specified range; if it is not, return michael@0: ** NULL. michael@0: ** Otherwise, return a pointer to the first character not part of the number. michael@0: */ michael@0: michael@0: static const char * michael@0: getnum(strp, nump, min, max) michael@0: register const char * strp; michael@0: int * const nump; michael@0: const int min; michael@0: const int max; michael@0: { michael@0: register char c; michael@0: register int num; michael@0: michael@0: if (strp == NULL || !is_digit(c = *strp)) michael@0: return NULL; michael@0: num = 0; michael@0: do { michael@0: num = num * 10 + (c - '0'); michael@0: if (num > max) michael@0: return NULL; /* illegal value */ michael@0: c = *++strp; michael@0: } while (is_digit(c)); michael@0: if (num < min) michael@0: return NULL; /* illegal value */ michael@0: *nump = num; michael@0: return strp; michael@0: } michael@0: michael@0: /* michael@0: ** Given a pointer into a time zone string, extract a number of seconds, michael@0: ** in hh[:mm[:ss]] form, from the string. michael@0: ** If any error occurs, return NULL. michael@0: ** Otherwise, return a pointer to the first character not part of the number michael@0: ** of seconds. michael@0: */ michael@0: michael@0: static const char * michael@0: getsecs(strp, secsp) michael@0: register const char * strp; michael@0: long * const secsp; michael@0: { michael@0: int num; michael@0: michael@0: /* michael@0: ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like michael@0: ** "M10.4.6/26", which does not conform to Posix, michael@0: ** but which specifies the equivalent of michael@0: ** ``02:00 on the first Sunday on or after 23 Oct''. michael@0: */ michael@0: strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); michael@0: if (strp == NULL) michael@0: return NULL; michael@0: *secsp = num * (long) SECSPERHOUR; michael@0: if (*strp == ':') { michael@0: ++strp; michael@0: strp = getnum(strp, &num, 0, MINSPERHOUR - 1); michael@0: if (strp == NULL) michael@0: return NULL; michael@0: *secsp += num * SECSPERMIN; michael@0: if (*strp == ':') { michael@0: ++strp; michael@0: /* `SECSPERMIN' allows for leap seconds. */ michael@0: strp = getnum(strp, &num, 0, SECSPERMIN); michael@0: if (strp == NULL) michael@0: return NULL; michael@0: *secsp += num; michael@0: } michael@0: } michael@0: return strp; michael@0: } michael@0: michael@0: /* michael@0: ** Given a pointer into a time zone string, extract an offset, in michael@0: ** [+-]hh[:mm[:ss]] form, from the string. michael@0: ** If any error occurs, return NULL. michael@0: ** Otherwise, return a pointer to the first character not part of the time. michael@0: */ michael@0: michael@0: static const char * michael@0: getoffset(strp, offsetp) michael@0: register const char * strp; michael@0: long * const offsetp; michael@0: { michael@0: register int neg = 0; michael@0: michael@0: if (*strp == '-') { michael@0: neg = 1; michael@0: ++strp; michael@0: } else if (*strp == '+') michael@0: ++strp; michael@0: strp = getsecs(strp, offsetp); michael@0: if (strp == NULL) michael@0: return NULL; /* illegal time */ michael@0: if (neg) michael@0: *offsetp = -*offsetp; michael@0: return strp; michael@0: } michael@0: michael@0: /* michael@0: ** Given a pointer into a time zone string, extract a rule in the form michael@0: ** date[/time]. See POSIX section 8 for the format of "date" and "time". michael@0: ** If a valid rule is not found, return NULL. michael@0: ** Otherwise, return a pointer to the first character not part of the rule. michael@0: */ michael@0: michael@0: static const char * michael@0: getrule(strp, rulep) michael@0: const char * strp; michael@0: register struct rule * const rulep; michael@0: { michael@0: if (*strp == 'J') { michael@0: /* michael@0: ** Julian day. michael@0: */ michael@0: rulep->r_type = JULIAN_DAY; michael@0: ++strp; michael@0: strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); michael@0: } else if (*strp == 'M') { michael@0: /* michael@0: ** Month, week, day. michael@0: */ michael@0: rulep->r_type = MONTH_NTH_DAY_OF_WEEK; michael@0: ++strp; michael@0: strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); michael@0: if (strp == NULL) michael@0: return NULL; michael@0: if (*strp++ != '.') michael@0: return NULL; michael@0: strp = getnum(strp, &rulep->r_week, 1, 5); michael@0: if (strp == NULL) michael@0: return NULL; michael@0: if (*strp++ != '.') michael@0: return NULL; michael@0: strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); michael@0: } else if (is_digit(*strp)) { michael@0: /* michael@0: ** Day of year. michael@0: */ michael@0: rulep->r_type = DAY_OF_YEAR; michael@0: strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); michael@0: } else return NULL; /* invalid format */ michael@0: if (strp == NULL) michael@0: return NULL; michael@0: if (*strp == '/') { michael@0: /* michael@0: ** Time specified. michael@0: */ michael@0: ++strp; michael@0: strp = getsecs(strp, &rulep->r_time); michael@0: } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ michael@0: return strp; michael@0: } michael@0: michael@0: /* michael@0: ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the michael@0: ** year, a rule, and the offset from UTC at the time that rule takes effect, michael@0: ** calculate the Epoch-relative time that rule takes effect. michael@0: */ michael@0: michael@0: static time_t michael@0: transtime(janfirst, year, rulep, offset) michael@0: const time_t janfirst; michael@0: const int year; michael@0: register const struct rule * const rulep; michael@0: const long offset; michael@0: { michael@0: register int leapyear; michael@0: register time_t value; michael@0: register int i; michael@0: int d, m1, yy0, yy1, yy2, dow; michael@0: michael@0: INITIALIZE(value); michael@0: leapyear = isleap(year); michael@0: switch (rulep->r_type) { michael@0: michael@0: case JULIAN_DAY: michael@0: /* michael@0: ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap michael@0: ** years. michael@0: ** In non-leap years, or if the day number is 59 or less, just michael@0: ** add SECSPERDAY times the day number-1 to the time of michael@0: ** January 1, midnight, to get the day. michael@0: */ michael@0: value = janfirst + (rulep->r_day - 1) * SECSPERDAY; michael@0: if (leapyear && rulep->r_day >= 60) michael@0: value += SECSPERDAY; michael@0: break; michael@0: michael@0: case DAY_OF_YEAR: michael@0: /* michael@0: ** n - day of year. michael@0: ** Just add SECSPERDAY times the day number to the time of michael@0: ** January 1, midnight, to get the day. michael@0: */ michael@0: value = janfirst + rulep->r_day * SECSPERDAY; michael@0: break; michael@0: michael@0: case MONTH_NTH_DAY_OF_WEEK: michael@0: /* michael@0: ** Mm.n.d - nth "dth day" of month m. michael@0: */ michael@0: value = janfirst; michael@0: for (i = 0; i < rulep->r_mon - 1; ++i) michael@0: value += mon_lengths[leapyear][i] * SECSPERDAY; michael@0: michael@0: /* michael@0: ** Use Zeller's Congruence to get day-of-week of first day of michael@0: ** month. michael@0: */ michael@0: m1 = (rulep->r_mon + 9) % 12 + 1; michael@0: yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; michael@0: yy1 = yy0 / 100; michael@0: yy2 = yy0 % 100; michael@0: dow = ((26 * m1 - 2) / 10 + michael@0: 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; michael@0: if (dow < 0) michael@0: dow += DAYSPERWEEK; michael@0: michael@0: /* michael@0: ** "dow" is the day-of-week of the first day of the month. Get michael@0: ** the day-of-month (zero-origin) of the first "dow" day of the michael@0: ** month. michael@0: */ michael@0: d = rulep->r_day - dow; michael@0: if (d < 0) michael@0: d += DAYSPERWEEK; michael@0: for (i = 1; i < rulep->r_week; ++i) { michael@0: if (d + DAYSPERWEEK >= michael@0: mon_lengths[leapyear][rulep->r_mon - 1]) michael@0: break; michael@0: d += DAYSPERWEEK; michael@0: } michael@0: michael@0: /* michael@0: ** "d" is the day-of-month (zero-origin) of the day we want. michael@0: */ michael@0: value += d * SECSPERDAY; michael@0: break; michael@0: } michael@0: michael@0: /* michael@0: ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in michael@0: ** question. To get the Epoch-relative time of the specified local michael@0: ** time on that day, add the transition time and the current offset michael@0: ** from UTC. michael@0: */ michael@0: return value + rulep->r_time + offset; michael@0: } michael@0: michael@0: /* michael@0: ** Given a POSIX section 8-style TZ string, fill in the rule tables as michael@0: ** appropriate. michael@0: */ michael@0: michael@0: static int michael@0: tzparse(name, sp, lastditch) michael@0: const char * name; michael@0: register struct state * const sp; michael@0: const int lastditch; michael@0: { michael@0: const char * stdname; michael@0: const char * dstname; michael@0: size_t stdlen; michael@0: size_t dstlen; michael@0: long stdoffset; michael@0: long dstoffset; michael@0: register time_t * atp; michael@0: register unsigned char * typep; michael@0: register char * cp; michael@0: register int load_result; michael@0: michael@0: INITIALIZE(dstname); michael@0: stdname = name; michael@0: if (lastditch) { michael@0: stdlen = strlen(name); /* length of standard zone name */ michael@0: name += stdlen; michael@0: if (stdlen >= sizeof sp->chars) michael@0: stdlen = (sizeof sp->chars) - 1; michael@0: stdoffset = 0; michael@0: } else { michael@0: if (*name == '<') { michael@0: name++; michael@0: stdname = name; michael@0: name = getqzname(name, '>'); michael@0: if (*name != '>') michael@0: return (-1); michael@0: stdlen = name - stdname; michael@0: name++; michael@0: } else { michael@0: name = getzname(name); michael@0: stdlen = name - stdname; michael@0: } michael@0: if (*name == '\0') michael@0: return -1; michael@0: name = getoffset(name, &stdoffset); michael@0: if (name == NULL) michael@0: return -1; michael@0: } michael@0: load_result = tzload(TZDEFRULES, sp, FALSE); michael@0: if (load_result != 0) michael@0: sp->leapcnt = 0; /* so, we're off a little */ michael@0: if (*name != '\0') { michael@0: if (*name == '<') { michael@0: dstname = ++name; michael@0: name = getqzname(name, '>'); michael@0: if (*name != '>') michael@0: return -1; michael@0: dstlen = name - dstname; michael@0: name++; michael@0: } else { michael@0: dstname = name; michael@0: name = getzname(name); michael@0: dstlen = name - dstname; /* length of DST zone name */ michael@0: } michael@0: if (*name != '\0' && *name != ',' && *name != ';') { michael@0: name = getoffset(name, &dstoffset); michael@0: if (name == NULL) michael@0: return -1; michael@0: } else dstoffset = stdoffset - SECSPERHOUR; michael@0: if (*name == '\0' && load_result != 0) michael@0: name = TZDEFRULESTRING; michael@0: if (*name == ',' || *name == ';') { michael@0: struct rule start; michael@0: struct rule end; michael@0: register int year; michael@0: register time_t janfirst; michael@0: time_t starttime; michael@0: time_t endtime; michael@0: michael@0: ++name; michael@0: if ((name = getrule(name, &start)) == NULL) michael@0: return -1; michael@0: if (*name++ != ',') michael@0: return -1; michael@0: if ((name = getrule(name, &end)) == NULL) michael@0: return -1; michael@0: if (*name != '\0') michael@0: return -1; michael@0: sp->typecnt = 2; /* standard time and DST */ michael@0: /* michael@0: ** Two transitions per year, from EPOCH_YEAR forward. michael@0: */ michael@0: sp->ttis[0].tt_gmtoff = -dstoffset; michael@0: sp->ttis[0].tt_isdst = 1; michael@0: sp->ttis[0].tt_abbrind = stdlen + 1; michael@0: sp->ttis[1].tt_gmtoff = -stdoffset; michael@0: sp->ttis[1].tt_isdst = 0; michael@0: sp->ttis[1].tt_abbrind = 0; michael@0: atp = sp->ats; michael@0: typep = sp->types; michael@0: janfirst = 0; michael@0: sp->timecnt = 0; michael@0: for (year = EPOCH_YEAR; michael@0: sp->timecnt + 2 <= TZ_MAX_TIMES; michael@0: ++year) { michael@0: time_t newfirst; michael@0: michael@0: starttime = transtime(janfirst, year, &start, michael@0: stdoffset); michael@0: endtime = transtime(janfirst, year, &end, michael@0: dstoffset); michael@0: if (starttime > endtime) { michael@0: *atp++ = endtime; michael@0: *typep++ = 1; /* DST ends */ michael@0: *atp++ = starttime; michael@0: *typep++ = 0; /* DST begins */ michael@0: } else { michael@0: *atp++ = starttime; michael@0: *typep++ = 0; /* DST begins */ michael@0: *atp++ = endtime; michael@0: *typep++ = 1; /* DST ends */ michael@0: } michael@0: sp->timecnt += 2; michael@0: newfirst = janfirst; michael@0: newfirst += year_lengths[isleap(year)] * michael@0: SECSPERDAY; michael@0: if (newfirst <= janfirst) michael@0: break; michael@0: janfirst = newfirst; michael@0: } michael@0: } else { michael@0: register long theirstdoffset; michael@0: register long theirdstoffset; michael@0: register long theiroffset; michael@0: register int isdst; michael@0: register int i; michael@0: register int j; michael@0: michael@0: if (*name != '\0') michael@0: return -1; michael@0: /* michael@0: ** Initial values of theirstdoffset and theirdstoffset. michael@0: */ michael@0: theirstdoffset = 0; michael@0: for (i = 0; i < sp->timecnt; ++i) { michael@0: j = sp->types[i]; michael@0: if (!sp->ttis[j].tt_isdst) { michael@0: theirstdoffset = michael@0: -sp->ttis[j].tt_gmtoff; michael@0: break; michael@0: } michael@0: } michael@0: theirdstoffset = 0; michael@0: for (i = 0; i < sp->timecnt; ++i) { michael@0: j = sp->types[i]; michael@0: if (sp->ttis[j].tt_isdst) { michael@0: theirdstoffset = michael@0: -sp->ttis[j].tt_gmtoff; michael@0: break; michael@0: } michael@0: } michael@0: /* michael@0: ** Initially we're assumed to be in standard time. michael@0: */ michael@0: isdst = FALSE; michael@0: theiroffset = theirstdoffset; michael@0: /* michael@0: ** Now juggle transition times and types michael@0: ** tracking offsets as you do. michael@0: */ michael@0: for (i = 0; i < sp->timecnt; ++i) { michael@0: j = sp->types[i]; michael@0: sp->types[i] = sp->ttis[j].tt_isdst; michael@0: if (sp->ttis[j].tt_ttisgmt) { michael@0: /* No adjustment to transition time */ michael@0: } else { michael@0: /* michael@0: ** If summer time is in effect, and the michael@0: ** transition time was not specified as michael@0: ** standard time, add the summer time michael@0: ** offset to the transition time; michael@0: ** otherwise, add the standard time michael@0: ** offset to the transition time. michael@0: */ michael@0: /* michael@0: ** Transitions from DST to DDST michael@0: ** will effectively disappear since michael@0: ** POSIX provides for only one DST michael@0: ** offset. michael@0: */ michael@0: if (isdst && !sp->ttis[j].tt_ttisstd) { michael@0: sp->ats[i] += dstoffset - michael@0: theirdstoffset; michael@0: } else { michael@0: sp->ats[i] += stdoffset - michael@0: theirstdoffset; michael@0: } michael@0: } michael@0: theiroffset = -sp->ttis[j].tt_gmtoff; michael@0: if (sp->ttis[j].tt_isdst) michael@0: theirdstoffset = theiroffset; michael@0: else theirstdoffset = theiroffset; michael@0: } michael@0: /* michael@0: ** Finally, fill in ttis. michael@0: ** ttisstd and ttisgmt need not be handled. michael@0: */ michael@0: sp->ttis[0].tt_gmtoff = -stdoffset; michael@0: sp->ttis[0].tt_isdst = FALSE; michael@0: sp->ttis[0].tt_abbrind = 0; michael@0: sp->ttis[1].tt_gmtoff = -dstoffset; michael@0: sp->ttis[1].tt_isdst = TRUE; michael@0: sp->ttis[1].tt_abbrind = stdlen + 1; michael@0: sp->typecnt = 2; michael@0: } michael@0: } else { michael@0: dstlen = 0; michael@0: sp->typecnt = 1; /* only standard time */ michael@0: sp->timecnt = 0; michael@0: sp->ttis[0].tt_gmtoff = -stdoffset; michael@0: sp->ttis[0].tt_isdst = 0; michael@0: sp->ttis[0].tt_abbrind = 0; michael@0: } michael@0: sp->charcnt = stdlen + 1; michael@0: if (dstlen != 0) michael@0: sp->charcnt += dstlen + 1; michael@0: if ((size_t) sp->charcnt > sizeof sp->chars) michael@0: return -1; michael@0: cp = sp->chars; michael@0: (void) strncpy(cp, stdname, stdlen); michael@0: cp += stdlen; michael@0: *cp++ = '\0'; michael@0: if (dstlen != 0) { michael@0: (void) strncpy(cp, dstname, dstlen); michael@0: *(cp + dstlen) = '\0'; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: gmtload(sp) michael@0: struct state * const sp; michael@0: { michael@0: if (tzload(gmt, sp, TRUE) != 0) michael@0: (void) tzparse(gmt, sp, TRUE); michael@0: } michael@0: michael@0: #ifndef STD_INSPIRED michael@0: /* michael@0: ** A non-static declaration of tzsetwall in a system header file michael@0: ** may cause a warning about this upcoming static declaration... michael@0: */ michael@0: static michael@0: #endif /* !defined STD_INSPIRED */ michael@0: void michael@0: tzsetwall(void) michael@0: { michael@0: if (lcl_is_set < 0) michael@0: return; michael@0: lcl_is_set = -1; michael@0: michael@0: #ifdef ALL_STATE michael@0: if (lclptr == NULL) { michael@0: lclptr = (struct state *) malloc(sizeof *lclptr); michael@0: if (lclptr == NULL) { michael@0: settzname(); /* all we can do */ michael@0: return; michael@0: } michael@0: } michael@0: #endif /* defined ALL_STATE */ michael@0: if (tzload((char *) NULL, lclptr, TRUE) != 0) michael@0: gmtload(lclptr); michael@0: settzname(); michael@0: } michael@0: michael@0: void michael@0: tzset(void) michael@0: { michael@0: register const char * name; michael@0: michael@0: name = getenv("TZ"); michael@0: if (name == NULL) { michael@0: tzsetwall(); michael@0: return; michael@0: } michael@0: michael@0: if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) michael@0: return; michael@0: lcl_is_set = strlen(name) < sizeof lcl_TZname; michael@0: if (lcl_is_set) michael@0: (void) strcpy(lcl_TZname, name); michael@0: michael@0: #ifdef ALL_STATE michael@0: if (lclptr == NULL) { michael@0: lclptr = (struct state *) malloc(sizeof *lclptr); michael@0: if (lclptr == NULL) { michael@0: settzname(); /* all we can do */ michael@0: return; michael@0: } michael@0: } michael@0: #endif /* defined ALL_STATE */ michael@0: if (*name == '\0') { michael@0: /* michael@0: ** User wants it fast rather than right. michael@0: */ michael@0: lclptr->leapcnt = 0; /* so, we're off a little */ michael@0: lclptr->timecnt = 0; michael@0: lclptr->typecnt = 0; michael@0: lclptr->ttis[0].tt_isdst = 0; michael@0: lclptr->ttis[0].tt_gmtoff = 0; michael@0: lclptr->ttis[0].tt_abbrind = 0; michael@0: (void) strcpy(lclptr->chars, gmt); michael@0: } else if (tzload(name, lclptr, TRUE) != 0) michael@0: if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0) michael@0: (void) gmtload(lclptr); michael@0: settzname(); michael@0: } michael@0: michael@0: /* michael@0: ** The easy way to behave "as if no library function calls" localtime michael@0: ** is to not call it--so we drop its guts into "localsub", which can be michael@0: ** freely called. (And no, the PANS doesn't require the above behavior-- michael@0: ** but it *is* desirable.) michael@0: ** michael@0: ** The unused offset argument is for the benefit of mktime variants. michael@0: */ michael@0: michael@0: /*ARGSUSED*/ michael@0: static struct tm * michael@0: localsub(timep, offset, tmp) michael@0: const time_t * const timep; michael@0: const long offset; michael@0: struct tm * const tmp; michael@0: { michael@0: register struct state * sp; michael@0: register const struct ttinfo * ttisp; michael@0: register int i; michael@0: register struct tm * result; michael@0: const time_t t = *timep; michael@0: michael@0: sp = lclptr; michael@0: #ifdef ALL_STATE michael@0: if (sp == NULL) michael@0: return gmtsub(timep, offset, tmp); michael@0: #endif /* defined ALL_STATE */ michael@0: if ((sp->goback && t < sp->ats[0]) || michael@0: (sp->goahead && t > sp->ats[sp->timecnt - 1])) { michael@0: time_t newt = t; michael@0: register time_t seconds; michael@0: register time_t tcycles; michael@0: register int_fast64_t icycles; michael@0: michael@0: if (t < sp->ats[0]) michael@0: seconds = sp->ats[0] - t; michael@0: else seconds = t - sp->ats[sp->timecnt - 1]; michael@0: --seconds; michael@0: tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR; michael@0: ++tcycles; michael@0: icycles = tcycles; michael@0: if (tcycles - icycles >= 1 || icycles - tcycles >= 1) michael@0: return NULL; michael@0: seconds = icycles; michael@0: seconds *= YEARSPERREPEAT; michael@0: seconds *= AVGSECSPERYEAR; michael@0: if (t < sp->ats[0]) michael@0: newt += seconds; michael@0: else newt -= seconds; michael@0: if (newt < sp->ats[0] || michael@0: newt > sp->ats[sp->timecnt - 1]) michael@0: return NULL; /* "cannot happen" */ michael@0: result = localsub(&newt, offset, tmp); michael@0: if (result == tmp) { michael@0: register time_t newy; michael@0: michael@0: newy = tmp->tm_year; michael@0: if (t < sp->ats[0]) michael@0: newy -= icycles * YEARSPERREPEAT; michael@0: else newy += icycles * YEARSPERREPEAT; michael@0: tmp->tm_year = newy; michael@0: if (tmp->tm_year != newy) michael@0: return NULL; michael@0: } michael@0: return result; michael@0: } michael@0: if (sp->timecnt == 0 || t < sp->ats[0]) { michael@0: i = 0; michael@0: while (sp->ttis[i].tt_isdst) michael@0: if (++i >= sp->typecnt) { michael@0: i = 0; michael@0: break; michael@0: } michael@0: } else { michael@0: register int lo = 1; michael@0: register int hi = sp->timecnt; michael@0: michael@0: while (lo < hi) { michael@0: register int mid = (lo + hi) >> 1; michael@0: michael@0: if (t < sp->ats[mid]) michael@0: hi = mid; michael@0: else lo = mid + 1; michael@0: } michael@0: i = (int) sp->types[lo - 1]; michael@0: } michael@0: ttisp = &sp->ttis[i]; michael@0: /* michael@0: ** To get (wrong) behavior that's compatible with System V Release 2.0 michael@0: ** you'd replace the statement below with michael@0: ** t += ttisp->tt_gmtoff; michael@0: ** timesub(&t, 0L, sp, tmp); michael@0: */ michael@0: result = timesub(&t, ttisp->tt_gmtoff, sp, tmp); michael@0: tmp->tm_isdst = ttisp->tt_isdst; michael@0: tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; michael@0: #ifdef TM_ZONE michael@0: tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; michael@0: #endif /* defined TM_ZONE */ michael@0: return result; michael@0: } michael@0: michael@0: struct tm * michael@0: localtime(timep) michael@0: const time_t * const timep; michael@0: { michael@0: tzset(); michael@0: return localsub(timep, 0L, &tm); michael@0: } michael@0: michael@0: /* michael@0: ** Re-entrant version of localtime. michael@0: */ michael@0: michael@0: struct tm * michael@0: localtime_r(timep, tmp) michael@0: const time_t * const timep; michael@0: struct tm * tmp; michael@0: { michael@0: return localsub(timep, 0L, tmp); michael@0: } michael@0: michael@0: /* michael@0: ** gmtsub is to gmtime as localsub is to localtime. michael@0: */ michael@0: michael@0: static struct tm * michael@0: gmtsub(timep, offset, tmp) michael@0: const time_t * const timep; michael@0: const long offset; michael@0: struct tm * const tmp; michael@0: { michael@0: register struct tm * result; michael@0: michael@0: if (!gmt_is_set) { michael@0: gmt_is_set = TRUE; michael@0: #ifdef ALL_STATE michael@0: gmtptr = (struct state *) malloc(sizeof *gmtptr); michael@0: if (gmtptr != NULL) michael@0: #endif /* defined ALL_STATE */ michael@0: gmtload(gmtptr); michael@0: } michael@0: result = timesub(timep, offset, gmtptr, tmp); michael@0: #ifdef TM_ZONE michael@0: /* michael@0: ** Could get fancy here and deliver something such as michael@0: ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero, michael@0: ** but this is no time for a treasure hunt. michael@0: */ michael@0: if (offset != 0) michael@0: tmp->TM_ZONE = wildabbr; michael@0: else { michael@0: #ifdef ALL_STATE michael@0: if (gmtptr == NULL) michael@0: tmp->TM_ZONE = gmt; michael@0: else tmp->TM_ZONE = gmtptr->chars; michael@0: #endif /* defined ALL_STATE */ michael@0: #ifndef ALL_STATE michael@0: tmp->TM_ZONE = gmtptr->chars; michael@0: #endif /* State Farm */ michael@0: } michael@0: #endif /* defined TM_ZONE */ michael@0: return result; michael@0: } michael@0: michael@0: struct tm * michael@0: gmtime(timep) michael@0: const time_t * const timep; michael@0: { michael@0: return gmtsub(timep, 0L, &tm); michael@0: } michael@0: michael@0: /* michael@0: * Re-entrant version of gmtime. michael@0: */ michael@0: michael@0: struct tm * michael@0: gmtime_r(timep, tmp) michael@0: const time_t * const timep; michael@0: struct tm * tmp; michael@0: { michael@0: return gmtsub(timep, 0L, tmp); michael@0: } michael@0: michael@0: #ifdef STD_INSPIRED michael@0: michael@0: struct tm * michael@0: offtime(timep, offset) michael@0: const time_t * const timep; michael@0: const long offset; michael@0: { michael@0: return gmtsub(timep, offset, &tm); michael@0: } michael@0: michael@0: #endif /* defined STD_INSPIRED */ michael@0: michael@0: /* michael@0: ** Return the number of leap years through the end of the given year michael@0: ** where, to make the math easy, the answer for year zero is defined as zero. michael@0: */ michael@0: michael@0: static int michael@0: leaps_thru_end_of(y) michael@0: register const int y; michael@0: { michael@0: return (y >= 0) ? (y / 4 - y / 100 + y / 400) : michael@0: -(leaps_thru_end_of(-(y + 1)) + 1); michael@0: } michael@0: michael@0: static struct tm * michael@0: timesub(timep, offset, sp, tmp) michael@0: const time_t * const timep; michael@0: const long offset; michael@0: register const struct state * const sp; michael@0: register struct tm * const tmp; michael@0: { michael@0: register const struct lsinfo * lp; michael@0: register time_t tdays; michael@0: register int idays; /* unsigned would be so 2003 */ michael@0: register long rem; michael@0: int y; michael@0: register const int * ip; michael@0: register long corr; michael@0: register int hit; michael@0: register int i; michael@0: michael@0: corr = 0; michael@0: hit = 0; michael@0: #ifdef ALL_STATE michael@0: i = (sp == NULL) ? 0 : sp->leapcnt; michael@0: #endif /* defined ALL_STATE */ michael@0: #ifndef ALL_STATE michael@0: i = sp->leapcnt; michael@0: #endif /* State Farm */ michael@0: while (--i >= 0) { michael@0: lp = &sp->lsis[i]; michael@0: if (*timep >= lp->ls_trans) { michael@0: if (*timep == lp->ls_trans) { michael@0: hit = ((i == 0 && lp->ls_corr > 0) || michael@0: lp->ls_corr > sp->lsis[i - 1].ls_corr); michael@0: if (hit) michael@0: while (i > 0 && michael@0: sp->lsis[i].ls_trans == michael@0: sp->lsis[i - 1].ls_trans + 1 && michael@0: sp->lsis[i].ls_corr == michael@0: sp->lsis[i - 1].ls_corr + 1) { michael@0: ++hit; michael@0: --i; michael@0: } michael@0: } michael@0: corr = lp->ls_corr; michael@0: break; michael@0: } michael@0: } michael@0: y = EPOCH_YEAR; michael@0: tdays = *timep / SECSPERDAY; michael@0: rem = *timep - tdays * SECSPERDAY; michael@0: while (tdays < 0 || tdays >= year_lengths[isleap(y)]) { michael@0: int newy; michael@0: register time_t tdelta; michael@0: register int idelta; michael@0: register int leapdays; michael@0: michael@0: tdelta = tdays / DAYSPERLYEAR; michael@0: idelta = tdelta; michael@0: if (tdelta - idelta >= 1 || idelta - tdelta >= 1) michael@0: return NULL; michael@0: if (idelta == 0) michael@0: idelta = (tdays < 0) ? -1 : 1; michael@0: newy = y; michael@0: if (increment_overflow(&newy, idelta)) michael@0: return NULL; michael@0: leapdays = leaps_thru_end_of(newy - 1) - michael@0: leaps_thru_end_of(y - 1); michael@0: tdays -= ((time_t) newy - y) * DAYSPERNYEAR; michael@0: tdays -= leapdays; michael@0: y = newy; michael@0: } michael@0: { michael@0: register long seconds; michael@0: michael@0: seconds = tdays * SECSPERDAY + 0.5; michael@0: tdays = seconds / SECSPERDAY; michael@0: rem += seconds - tdays * SECSPERDAY; michael@0: } michael@0: /* michael@0: ** Given the range, we can now fearlessly cast... michael@0: */ michael@0: idays = tdays; michael@0: rem += offset - corr; michael@0: while (rem < 0) { michael@0: rem += SECSPERDAY; michael@0: --idays; michael@0: } michael@0: while (rem >= SECSPERDAY) { michael@0: rem -= SECSPERDAY; michael@0: ++idays; michael@0: } michael@0: while (idays < 0) { michael@0: if (increment_overflow(&y, -1)) michael@0: return NULL; michael@0: idays += year_lengths[isleap(y)]; michael@0: } michael@0: while (idays >= year_lengths[isleap(y)]) { michael@0: idays -= year_lengths[isleap(y)]; michael@0: if (increment_overflow(&y, 1)) michael@0: return NULL; michael@0: } michael@0: tmp->tm_year = y; michael@0: if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE)) michael@0: return NULL; michael@0: tmp->tm_yday = idays; michael@0: /* michael@0: ** The "extra" mods below avoid overflow problems. michael@0: */ michael@0: tmp->tm_wday = EPOCH_WDAY + michael@0: ((y - EPOCH_YEAR) % DAYSPERWEEK) * michael@0: (DAYSPERNYEAR % DAYSPERWEEK) + michael@0: leaps_thru_end_of(y - 1) - michael@0: leaps_thru_end_of(EPOCH_YEAR - 1) + michael@0: idays; michael@0: tmp->tm_wday %= DAYSPERWEEK; michael@0: if (tmp->tm_wday < 0) michael@0: tmp->tm_wday += DAYSPERWEEK; michael@0: tmp->tm_hour = (int) (rem / SECSPERHOUR); michael@0: rem %= SECSPERHOUR; michael@0: tmp->tm_min = (int) (rem / SECSPERMIN); michael@0: /* michael@0: ** A positive leap second requires a special michael@0: ** representation. This uses "... ??:59:60" et seq. michael@0: */ michael@0: tmp->tm_sec = (int) (rem % SECSPERMIN) + hit; michael@0: ip = mon_lengths[isleap(y)]; michael@0: for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon)) michael@0: idays -= ip[tmp->tm_mon]; michael@0: tmp->tm_mday = (int) (idays + 1); michael@0: tmp->tm_isdst = 0; michael@0: #ifdef TM_GMTOFF michael@0: tmp->TM_GMTOFF = offset; michael@0: #endif /* defined TM_GMTOFF */ michael@0: return tmp; michael@0: } michael@0: michael@0: char * michael@0: ctime(timep) michael@0: const time_t * const timep; michael@0: { michael@0: /* michael@0: ** Section 4.12.3.2 of X3.159-1989 requires that michael@0: ** The ctime function converts the calendar time pointed to by timer michael@0: ** to local time in the form of a string. It is equivalent to michael@0: ** asctime(localtime(timer)) michael@0: */ michael@0: return asctime(localtime(timep)); michael@0: } michael@0: michael@0: char * michael@0: ctime_r(timep, buf) michael@0: const time_t * const timep; michael@0: char * buf; michael@0: { michael@0: struct tm mytm; michael@0: michael@0: return asctime_r(localtime_r(timep, &mytm), buf); michael@0: } michael@0: michael@0: /* michael@0: ** Adapted from code provided by Robert Elz, who writes: michael@0: ** The "best" way to do mktime I think is based on an idea of Bob michael@0: ** Kridle's (so its said...) from a long time ago. michael@0: ** It does a binary search of the time_t space. Since time_t's are michael@0: ** just 32 bits, its a max of 32 iterations (even at 64 bits it michael@0: ** would still be very reasonable). michael@0: */ michael@0: michael@0: #ifndef WRONG michael@0: #define WRONG (-1) michael@0: #endif /* !defined WRONG */ michael@0: michael@0: /* michael@0: ** Simplified normalize logic courtesy Paul Eggert. michael@0: */ michael@0: michael@0: static int michael@0: increment_overflow(number, delta) michael@0: int * number; michael@0: int delta; michael@0: { michael@0: int number0; michael@0: michael@0: number0 = *number; michael@0: *number += delta; michael@0: return (*number < number0) != (delta < 0); michael@0: } michael@0: michael@0: static int michael@0: long_increment_overflow(number, delta) michael@0: long * number; michael@0: int delta; michael@0: { michael@0: long number0; michael@0: michael@0: number0 = *number; michael@0: *number += delta; michael@0: return (*number < number0) != (delta < 0); michael@0: } michael@0: michael@0: static int michael@0: normalize_overflow(tensptr, unitsptr, base) michael@0: int * const tensptr; michael@0: int * const unitsptr; michael@0: const int base; michael@0: { michael@0: register int tensdelta; michael@0: michael@0: tensdelta = (*unitsptr >= 0) ? michael@0: (*unitsptr / base) : michael@0: (-1 - (-1 - *unitsptr) / base); michael@0: *unitsptr -= tensdelta * base; michael@0: return increment_overflow(tensptr, tensdelta); michael@0: } michael@0: michael@0: static int michael@0: long_normalize_overflow(tensptr, unitsptr, base) michael@0: long * const tensptr; michael@0: int * const unitsptr; michael@0: const int base; michael@0: { michael@0: register int tensdelta; michael@0: michael@0: tensdelta = (*unitsptr >= 0) ? michael@0: (*unitsptr / base) : michael@0: (-1 - (-1 - *unitsptr) / base); michael@0: *unitsptr -= tensdelta * base; michael@0: return long_increment_overflow(tensptr, tensdelta); michael@0: } michael@0: michael@0: static int michael@0: tmcomp(atmp, btmp) michael@0: register const struct tm * const atmp; michael@0: register const struct tm * const btmp; michael@0: { michael@0: register int result; michael@0: michael@0: if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && michael@0: (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && michael@0: (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && michael@0: (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && michael@0: (result = (atmp->tm_min - btmp->tm_min)) == 0) michael@0: result = atmp->tm_sec - btmp->tm_sec; michael@0: return result; michael@0: } michael@0: michael@0: static time_t michael@0: time2sub(tmp, funcp, offset, okayp, do_norm_secs) michael@0: struct tm * const tmp; michael@0: struct tm * (* const funcp)(const time_t*, long, struct tm*); michael@0: const long offset; michael@0: int * const okayp; michael@0: const int do_norm_secs; michael@0: { michael@0: register const struct state * sp; michael@0: register int dir; michael@0: register int i, j; michael@0: register int saved_seconds; michael@0: register long li; michael@0: register time_t lo; michael@0: register time_t hi; michael@0: long y; michael@0: time_t newt; michael@0: time_t t; michael@0: struct tm yourtm, mytm; michael@0: michael@0: *okayp = FALSE; michael@0: yourtm = *tmp; michael@0: if (do_norm_secs) { michael@0: if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec, michael@0: SECSPERMIN)) michael@0: return WRONG; michael@0: } michael@0: if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR)) michael@0: return WRONG; michael@0: if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY)) michael@0: return WRONG; michael@0: y = yourtm.tm_year; michael@0: if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR)) michael@0: return WRONG; michael@0: /* michael@0: ** Turn y into an actual year number for now. michael@0: ** It is converted back to an offset from TM_YEAR_BASE later. michael@0: */ michael@0: if (long_increment_overflow(&y, TM_YEAR_BASE)) michael@0: return WRONG; michael@0: while (yourtm.tm_mday <= 0) { michael@0: if (long_increment_overflow(&y, -1)) michael@0: return WRONG; michael@0: li = y + (1 < yourtm.tm_mon); michael@0: yourtm.tm_mday += year_lengths[isleap(li)]; michael@0: } michael@0: while (yourtm.tm_mday > DAYSPERLYEAR) { michael@0: li = y + (1 < yourtm.tm_mon); michael@0: yourtm.tm_mday -= year_lengths[isleap(li)]; michael@0: if (long_increment_overflow(&y, 1)) michael@0: return WRONG; michael@0: } michael@0: for ( ; ; ) { michael@0: i = mon_lengths[isleap(y)][yourtm.tm_mon]; michael@0: if (yourtm.tm_mday <= i) michael@0: break; michael@0: yourtm.tm_mday -= i; michael@0: if (++yourtm.tm_mon >= MONSPERYEAR) { michael@0: yourtm.tm_mon = 0; michael@0: if (long_increment_overflow(&y, 1)) michael@0: return WRONG; michael@0: } michael@0: } michael@0: if (long_increment_overflow(&y, -TM_YEAR_BASE)) michael@0: return WRONG; michael@0: yourtm.tm_year = y; michael@0: if (yourtm.tm_year != y) michael@0: return WRONG; michael@0: if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN) michael@0: saved_seconds = 0; michael@0: else if (y + TM_YEAR_BASE < EPOCH_YEAR) { michael@0: /* michael@0: ** We can't set tm_sec to 0, because that might push the michael@0: ** time below the minimum representable time. michael@0: ** Set tm_sec to 59 instead. michael@0: ** This assumes that the minimum representable time is michael@0: ** not in the same minute that a leap second was deleted from, michael@0: ** which is a safer assumption than using 58 would be. michael@0: */ michael@0: if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN)) michael@0: return WRONG; michael@0: saved_seconds = yourtm.tm_sec; michael@0: yourtm.tm_sec = SECSPERMIN - 1; michael@0: } else { michael@0: saved_seconds = yourtm.tm_sec; michael@0: yourtm.tm_sec = 0; michael@0: } michael@0: /* michael@0: ** Do a binary search (this works whatever time_t's type is). michael@0: */ michael@0: if (!TYPE_SIGNED(time_t)) { michael@0: lo = 0; michael@0: hi = lo - 1; michael@0: } else if (!TYPE_INTEGRAL(time_t)) { michael@0: if (sizeof(time_t) > sizeof(float)) michael@0: hi = (time_t) DBL_MAX; michael@0: else hi = (time_t) FLT_MAX; michael@0: lo = -hi; michael@0: } else { michael@0: lo = 1; michael@0: for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i) michael@0: lo *= 2; michael@0: hi = -(lo + 1); michael@0: } michael@0: for ( ; ; ) { michael@0: t = lo / 2 + hi / 2; michael@0: if (t < lo) michael@0: t = lo; michael@0: else if (t > hi) michael@0: t = hi; michael@0: if ((*funcp)(&t, offset, &mytm) == NULL) { michael@0: /* michael@0: ** Assume that t is too extreme to be represented in michael@0: ** a struct tm; arrange things so that it is less michael@0: ** extreme on the next pass. michael@0: */ michael@0: dir = (t > 0) ? 1 : -1; michael@0: } else dir = tmcomp(&mytm, &yourtm); michael@0: if (dir != 0) { michael@0: if (t == lo) { michael@0: ++t; michael@0: if (t <= lo) michael@0: return WRONG; michael@0: ++lo; michael@0: } else if (t == hi) { michael@0: --t; michael@0: if (t >= hi) michael@0: return WRONG; michael@0: --hi; michael@0: } michael@0: if (lo > hi) michael@0: return WRONG; michael@0: if (dir > 0) michael@0: hi = t; michael@0: else lo = t; michael@0: continue; michael@0: } michael@0: if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) michael@0: break; michael@0: /* michael@0: ** Right time, wrong type. michael@0: ** Hunt for right time, right type. michael@0: ** It's okay to guess wrong since the guess michael@0: ** gets checked. michael@0: */ michael@0: sp = (const struct state *) michael@0: ((funcp == localsub) ? lclptr : gmtptr); michael@0: #ifdef ALL_STATE michael@0: if (sp == NULL) michael@0: return WRONG; michael@0: #endif /* defined ALL_STATE */ michael@0: for (i = sp->typecnt - 1; i >= 0; --i) { michael@0: if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) michael@0: continue; michael@0: for (j = sp->typecnt - 1; j >= 0; --j) { michael@0: if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) michael@0: continue; michael@0: newt = t + sp->ttis[j].tt_gmtoff - michael@0: sp->ttis[i].tt_gmtoff; michael@0: if ((*funcp)(&newt, offset, &mytm) == NULL) michael@0: continue; michael@0: if (tmcomp(&mytm, &yourtm) != 0) michael@0: continue; michael@0: if (mytm.tm_isdst != yourtm.tm_isdst) michael@0: continue; michael@0: /* michael@0: ** We have a match. michael@0: */ michael@0: t = newt; michael@0: goto label; michael@0: } michael@0: } michael@0: return WRONG; michael@0: } michael@0: label: michael@0: newt = t + saved_seconds; michael@0: if ((newt < t) != (saved_seconds < 0)) michael@0: return WRONG; michael@0: t = newt; michael@0: if ((*funcp)(&t, offset, tmp)) michael@0: *okayp = TRUE; michael@0: return t; michael@0: } michael@0: michael@0: static time_t michael@0: time2(tmp, funcp, offset, okayp) michael@0: struct tm * const tmp; michael@0: struct tm * (* const funcp)(const time_t*, long, struct tm*); michael@0: const long offset; michael@0: int * const okayp; michael@0: { michael@0: time_t t; michael@0: michael@0: /* michael@0: ** First try without normalization of seconds michael@0: ** (in case tm_sec contains a value associated with a leap second). michael@0: ** If that fails, try with normalization of seconds. michael@0: */ michael@0: t = time2sub(tmp, funcp, offset, okayp, FALSE); michael@0: return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE); michael@0: } michael@0: michael@0: static time_t michael@0: time1(tmp, funcp, offset) michael@0: struct tm * const tmp; michael@0: struct tm * (* const funcp)(const time_t *, long, struct tm *); michael@0: const long offset; michael@0: { michael@0: register time_t t; michael@0: register const struct state * sp; michael@0: register int samei, otheri; michael@0: register int sameind, otherind; michael@0: register int i; michael@0: register int nseen; michael@0: int seen[TZ_MAX_TYPES]; michael@0: int types[TZ_MAX_TYPES]; michael@0: int okay; michael@0: michael@0: if (tmp->tm_isdst > 1) michael@0: tmp->tm_isdst = 1; michael@0: t = time2(tmp, funcp, offset, &okay); michael@0: #ifdef PCTS michael@0: /* michael@0: ** PCTS code courtesy Grant Sullivan. michael@0: */ michael@0: if (okay) michael@0: return t; michael@0: if (tmp->tm_isdst < 0) michael@0: tmp->tm_isdst = 0; /* reset to std and try again */ michael@0: #endif /* defined PCTS */ michael@0: #ifndef PCTS michael@0: if (okay || tmp->tm_isdst < 0) michael@0: return t; michael@0: #endif /* !defined PCTS */ michael@0: /* michael@0: ** We're supposed to assume that somebody took a time of one type michael@0: ** and did some math on it that yielded a "struct tm" that's bad. michael@0: ** We try to divine the type they started from and adjust to the michael@0: ** type they need. michael@0: */ michael@0: sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr); michael@0: #ifdef ALL_STATE michael@0: if (sp == NULL) michael@0: return WRONG; michael@0: #endif /* defined ALL_STATE */ michael@0: for (i = 0; i < sp->typecnt; ++i) michael@0: seen[i] = FALSE; michael@0: nseen = 0; michael@0: for (i = sp->timecnt - 1; i >= 0; --i) michael@0: if (!seen[sp->types[i]]) { michael@0: seen[sp->types[i]] = TRUE; michael@0: types[nseen++] = sp->types[i]; michael@0: } michael@0: for (sameind = 0; sameind < nseen; ++sameind) { michael@0: samei = types[sameind]; michael@0: if (sp->ttis[samei].tt_isdst != tmp->tm_isdst) michael@0: continue; michael@0: for (otherind = 0; otherind < nseen; ++otherind) { michael@0: otheri = types[otherind]; michael@0: if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst) michael@0: continue; michael@0: tmp->tm_sec += sp->ttis[otheri].tt_gmtoff - michael@0: sp->ttis[samei].tt_gmtoff; michael@0: tmp->tm_isdst = !tmp->tm_isdst; michael@0: t = time2(tmp, funcp, offset, &okay); michael@0: if (okay) michael@0: return t; michael@0: tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - michael@0: sp->ttis[samei].tt_gmtoff; michael@0: tmp->tm_isdst = !tmp->tm_isdst; michael@0: } michael@0: } michael@0: return WRONG; michael@0: } michael@0: michael@0: time_t michael@0: mktime(tmp) michael@0: struct tm * const tmp; michael@0: { michael@0: tzset(); michael@0: return time1(tmp, localsub, 0L); michael@0: } michael@0: michael@0: #ifdef STD_INSPIRED michael@0: michael@0: time_t michael@0: timelocal(tmp) michael@0: struct tm * const tmp; michael@0: { michael@0: tmp->tm_isdst = -1; /* in case it wasn't initialized */ michael@0: return mktime(tmp); michael@0: } michael@0: michael@0: time_t michael@0: timegm(tmp) michael@0: struct tm * const tmp; michael@0: { michael@0: tmp->tm_isdst = 0; michael@0: return time1(tmp, gmtsub, 0L); michael@0: } michael@0: michael@0: time_t michael@0: timeoff(tmp, offset) michael@0: struct tm * const tmp; michael@0: const long offset; michael@0: { michael@0: tmp->tm_isdst = 0; michael@0: return time1(tmp, gmtsub, offset); michael@0: } michael@0: michael@0: #endif /* defined STD_INSPIRED */ michael@0: michael@0: #ifdef CMUCS michael@0: michael@0: /* michael@0: ** The following is supplied for compatibility with michael@0: ** previous versions of the CMUCS runtime library. michael@0: */ michael@0: michael@0: long michael@0: gtime(tmp) michael@0: struct tm * const tmp; michael@0: { michael@0: const time_t t = mktime(tmp); michael@0: michael@0: if (t == WRONG) michael@0: return -1; michael@0: return t; michael@0: } michael@0: michael@0: #endif /* defined CMUCS */ michael@0: michael@0: /* michael@0: ** XXX--is the below the right way to conditionalize?? michael@0: */ michael@0: michael@0: #ifdef STD_INSPIRED michael@0: michael@0: /* michael@0: ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599 michael@0: ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which michael@0: ** is not the case if we are accounting for leap seconds. michael@0: ** So, we provide the following conversion routines for use michael@0: ** when exchanging timestamps with POSIX conforming systems. michael@0: */ michael@0: michael@0: static long michael@0: leapcorr(timep) michael@0: time_t * timep; michael@0: { michael@0: register struct state * sp; michael@0: register struct lsinfo * lp; michael@0: register int i; michael@0: michael@0: sp = lclptr; michael@0: i = sp->leapcnt; michael@0: while (--i >= 0) { michael@0: lp = &sp->lsis[i]; michael@0: if (*timep >= lp->ls_trans) michael@0: return lp->ls_corr; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: time_t michael@0: time2posix(t) michael@0: time_t t; michael@0: { michael@0: tzset(); michael@0: return t - leapcorr(&t); michael@0: } michael@0: michael@0: time_t michael@0: posix2time(t) michael@0: time_t t; michael@0: { michael@0: time_t x; michael@0: time_t y; michael@0: michael@0: tzset(); michael@0: /* michael@0: ** For a positive leap second hit, the result michael@0: ** is not unique. For a negative leap second michael@0: ** hit, the corresponding time doesn't exist, michael@0: ** so we return an adjacent second. michael@0: */ michael@0: x = t + leapcorr(&t); michael@0: y = x - leapcorr(&x); michael@0: if (y < t) { michael@0: do { michael@0: x++; michael@0: y = x - leapcorr(&x); michael@0: } while (y < t); michael@0: if (t != y) michael@0: return x - 1; michael@0: } else if (y > t) { michael@0: do { michael@0: --x; michael@0: y = x - leapcorr(&x); michael@0: } while (y > t); michael@0: if (t != y) michael@0: return x + 1; michael@0: } michael@0: return x; michael@0: } michael@0: michael@0: #endif /* defined STD_INSPIRED */