📄 tztime.c
字号:
/*** This file is in the public domain, so clarified as of** 1996-06-05 by Arthur David Olson.*/#include <stdio.h>#ifndef lint#ifndef NOIDstatic char elsieid[] = "@(#)localtime.c 8.3";#endif /* !defined NOID */#endif /* !defined lint *//*** Leap second handling from Bradley White.** POSIX-style TZ environment variable handling from Guy Harris.*//*LINTLIBRARY*/#include "private.h"#include "tzfile.h"#include "fcntl.h"#include "float.h" /* for FLT_MAX and DBL_MAX */#ifndef TZ_ABBR_MAX_LEN#define TZ_ABBR_MAX_LEN 16#endif /* !defined TZ_ABBR_MAX_LEN */#ifndef TZ_ABBR_CHAR_SET#define TZ_ABBR_CHAR_SET \ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"#endif /* !defined TZ_ABBR_CHAR_SET */#ifndef TZ_ABBR_ERR_CHAR#define TZ_ABBR_ERR_CHAR '_'#endif /* !defined TZ_ABBR_ERR_CHAR */#define INDEXFILE "/system/usr/share/zoneinfo/zoneinfo.idx"#define DATAFILE "/system/usr/share/zoneinfo/zoneinfo.dat"#define NAMELEN 40#define INTLEN 4#define READLEN (NAMELEN + 3 * INTLEN)/*** SunOS 4.1.1 headers lack O_BINARY.*/#ifdef O_BINARY#define OPEN_MODE (O_RDONLY | O_BINARY)#endif /* defined O_BINARY */#ifndef O_BINARY#define OPEN_MODE O_RDONLY#endif /* !defined O_BINARY */#ifndef WILDABBR/*** Someone might make incorrect use of a time zone abbreviation:** 1. They might reference tzname[0] before calling tzset (explicitly** or implicitly).** 2. They might reference tzname[1] before calling tzset (explicitly** or implicitly).** 3. They might reference tzname[1] after setting to a time zone** in which Daylight Saving Time is never observed.** 4. They might reference tzname[0] after setting to a time zone** in which Standard Time is never observed.** 5. They might reference tm.TM_ZONE after calling offtime.** What's best to do in the above cases is open to debate;** for now, we just set things up so that in any of the five cases** WILDABBR is used. Another possibility: initialize tzname[0] to the** string "tzname[0] used before set", and similarly for the other cases.** And another: initialize tzname[0] to "ERA", with an explanation in the** manual page of what this "time zone abbreviation" means (doing this so** that tzname[0] has the "normal" length of three characters).*/#define WILDABBR " "#endif /* !defined WILDABBR */static char wildabbr[] = WILDABBR;static const char gmt[] = "GMT";/*** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.** We default to US rules as of 1999-08-17.** POSIX 1003.1 section 8.1.1 says that the default DST rules are** implementation dependent; for historical reasons, US rules are a** common default.*/#ifndef TZDEFRULESTRING#define TZDEFRULESTRING ",M4.1.0,M10.5.0"#endif /* !defined TZDEFDST */struct ttinfo { /* time type information */ long tt_gmtoff; /* UTC offset in seconds */ int tt_isdst; /* used to set tm_isdst */ int tt_abbrind; /* abbreviation list index */ int tt_ttisstd; /* TRUE if transition is std time */ int tt_ttisgmt; /* TRUE if transition is UTC */};struct lsinfo { /* leap second information */ time_t ls_trans; /* transition time */ long ls_corr; /* correction to apply */};#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))#ifdef TZNAME_MAX#define MY_TZNAME_MAX TZNAME_MAX#endif /* defined TZNAME_MAX */#ifndef TZNAME_MAX#define MY_TZNAME_MAX 255#endif /* !defined TZNAME_MAX */struct state { int leapcnt; int timecnt; int typecnt; int charcnt; int goback; int goahead; time_t ats[TZ_MAX_TIMES]; unsigned char types[TZ_MAX_TIMES]; struct ttinfo ttis[TZ_MAX_TYPES]; char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), (2 * (MY_TZNAME_MAX + 1)))]; struct lsinfo lsis[TZ_MAX_LEAPS];};struct rule { int r_type; /* type of rule--see below */ int r_day; /* day number of rule */ int r_week; /* week number of rule */ int r_mon; /* month number of rule */ long r_time; /* transition time of rule */};#define JULIAN_DAY 0 /* Jn - Julian day */#define DAY_OF_YEAR 1 /* n - day of year */#define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week *//*** Prototypes for static functions.*/static long detzcode P((const char * codep));static time_t detzcode64 P((const char * codep));static int differ_by_repeat P((time_t t1, time_t t0));static const char * getzname P((const char * strp));static const char * getqzname P((const char * strp, const int delim));static const char * getnum P((const char * strp, int * nump, int min, int max));static const char * getsecs P((const char * strp, long * secsp));static const char * getoffset P((const char * strp, long * offsetp));static const char * getrule P((const char * strp, struct rule * rulep));static void gmtload P((struct state * sp));static struct tm * gmtsub P((const time_t * timep, long offset, struct tm * tmp));static struct tm * localsub P((const time_t * timep, long offset, struct tm * tmp, struct state *sp));static int increment_overflow P((int * number, int delta));static int leaps_thru_end_of P((int y));static int long_increment_overflow P((long * number, int delta));static int long_normalize_overflow P((long * tensptr, int * unitsptr, int base));static int normalize_overflow P((int * tensptr, int * unitsptr, int base));static void settzname P((void));static time_t time1 P((struct tm * tmp, struct tm * (*funcp) P((const time_t *, long, struct tm *, const struct state* sp)), long offset, const struct state * sp));static time_t time2 P((struct tm *tmp, struct tm * (*funcp) P((const time_t *, long, struct tm*, const struct state* sp)), long offset, int * okayp, const struct state * sp));static time_t time2sub P((struct tm *tmp, struct tm * (*funcp) P((const time_t*, long, struct tm*,const struct state *sp)), long offset, int * okayp, int do_norm_secs, const struct state *sp));static struct tm * timesub P((const time_t * timep, long offset, const struct state * sp, struct tm * tmp));static int tmcomp P((const struct tm * atmp, const struct tm * btmp));static time_t transtime P((time_t janfirst, int year, const struct rule * rulep, long offset));static int tzload P((const char * name, struct state * sp, int doextend));static int tzload_uncached P((const char * name, struct state * sp, int doextend));static int tzparse P((const char * name, struct state * sp, int lastditch));#ifdef ALL_STATEstatic struct state * gmtptr;#endif /* defined ALL_STATE */#ifndef ALL_STATEstatic struct state gmtmem;#define gmtptr (&gmtmem)#endif /* State Farm */#define CACHE_COUNT 4static char * g_cacheNames[CACHE_COUNT] = {0,0};static struct state g_cacheStates[CACHE_COUNT];static int g_lastCache = 0;static struct state g_utc;unsigned char g_utcSet = 0;#ifndef TZ_STRLEN_MAX#define TZ_STRLEN_MAX 255#endif /* !defined TZ_STRLEN_MAX */static char lcl_TZname[TZ_STRLEN_MAX + 1];static int lcl_is_set;static int gmt_is_set;char * tzname[2] = { wildabbr, wildabbr};/*** Section 4.12.3 of X3.159-1989 requires that** Except for the strftime function, these functions [asctime,** ctime, gmtime, localtime] return values in one of two static** objects: a broken-down time structure and an array of char.** Thanks to Paul Eggert for noting this.*/static struct tm tm;#ifdef USG_COMPATtime_t timezone = 0;int daylight = 0;#endif /* defined USG_COMPAT */#ifdef ALTZONEtime_t altzone = 0;#endif /* defined ALTZONE */static longdetzcode(codep)const char * const codep;{ register long result; register int i; result = (codep[0] & 0x80) ? ~0L : 0; for (i = 0; i < 4; ++i) result = (result << 8) | (codep[i] & 0xff); return result;}static time_tdetzcode64(codep)const char * const codep;{ register time_t result; register int i; result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0; for (i = 0; i < 8; ++i) result = result * 256 + (codep[i] & 0xff); return result;}static intdiffer_by_repeat(t1, t0)const time_t t1;const time_t t0;{ if (TYPE_INTEGRAL(time_t) && TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) return 0; return t1 - t0 == SECSPERREPEAT;}static int toint(unsigned char *s) { return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];}static inttzload(const char *name, struct state * const sp, const int doextend){ if (name) { int i, err; if (0 == strcmp(name, "UTC")) { if (!g_utcSet) { tzload_uncached(name, &g_utc, 1); g_utcSet = 1; } //printf("tzload: utc\n"); *sp = g_utc; return 0; } for (i=0; i<CACHE_COUNT; i++) { if (g_cacheNames[i] && 0 == strcmp(name, g_cacheNames[i])) { *sp = g_cacheStates[i]; //printf("tzload: hit: %s\n", name); return 0; } } //printf("tzload: miss: %s\n", name); g_lastCache++; if (g_lastCache >= CACHE_COUNT) { g_lastCache = 0; } i = g_lastCache; if (g_cacheNames[i]) { free(g_cacheNames[i]); } err = tzload_uncached(name, &(g_cacheStates[i]), 1); if (err == 0) { g_cacheNames[i] = strdup(name); *sp = g_cacheStates[i]; return 0; } else { g_cacheNames[i] = NULL; return err; } } return tzload_uncached(name, sp, doextend);}static inttzload_uncached(name, sp, doextend)register const char * name;register struct state * const sp;register const int doextend;{ register const char * p; register int i; register int fid; register int stored; register int nread; union { struct tzhead tzhead; char buf[2 * sizeof(struct tzhead) + 2 * sizeof *sp + 4 * TZ_MAX_TIMES]; } u; int toread = sizeof u.buf; if (name == NULL && (name = TZDEFAULT) == NULL) return -1; { register int doaccess; /* ** Section 4.9.1 of the C standard says that ** "FILENAME_MAX expands to an integral constant expression ** that is the size needed for an array of char large enough ** to hold the longest file name string that the implementation ** guarantees can be opened." */ char fullname[FILENAME_MAX + 1]; const char *origname = name; if (name[0] == ':') ++name; doaccess = name[0] == '/'; if (!doaccess) { if ((p = TZDIR) == NULL) return -1; if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) return -1; (void) strcpy(fullname, p); (void) strcat(fullname, "/"); (void) strcat(fullname, name); /* ** Set doaccess if '.' (as in "../") shows up in name. */ if (strchr(name, '.') != NULL) doaccess = TRUE; name = fullname; } if (doaccess && access(name, R_OK) != 0) return -1; if ((fid = open(name, OPEN_MODE)) == -1) { char buf[READLEN]; char name[NAMELEN + 1]; int fidix = open(INDEXFILE, OPEN_MODE); int off = -1; if (fidix < 0) { return -1; } while (read(fidix, buf, sizeof(buf)) == sizeof(buf)) { memcpy(name, buf, NAMELEN); name[NAMELEN] = '\0'; if (strcmp(name, origname) == 0) { off = toint((unsigned char *) buf + NAMELEN); toread = toint((unsigned char *) buf + NAMELEN + INTLEN); break; } } close(fidix); if (off < 0) return -1; fid = open(DATAFILE, OPEN_MODE); if (fid < 0) { return -1; } if (lseek(fid, off, SEEK_SET) < 0) { return -1; } } } nread = read(fid, u.buf, toread); if (close(fid) < 0 || nread <= 0) return -1; for (stored = 4; stored <= 8; stored *= 2) { int ttisstdcnt; int ttisgmtcnt; ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt); ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt); sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt); sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt); sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt); sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt); p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt; if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) return -1; if (nread - (p - u.buf) < sp->timecnt * stored + /* ats */ sp->timecnt + /* types */ sp->typecnt * 6 + /* ttinfos */ sp->charcnt + /* chars */ sp->leapcnt * (stored + 4) + /* lsinfos */ ttisstdcnt + /* ttisstds */ ttisgmtcnt) /* ttisgmts */ return -1; for (i = 0; i < sp->timecnt; ++i) { sp->ats[i] = (stored == 4) ? detzcode(p) : detzcode64(p); p += stored; } for (i = 0; i < sp->timecnt; ++i) { sp->types[i] = (unsigned char) *p++; if (sp->types[i] >= sp->typecnt) return -1; } for (i = 0; i < sp->typecnt; ++i) { register struct ttinfo * ttisp; ttisp = &sp->ttis[i]; ttisp->tt_gmtoff = detzcode(p); p += 4; ttisp->tt_isdst = (unsigned char) *p++; if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) return -1; ttisp->tt_abbrind = (unsigned char) *p++; if (ttisp->tt_abbrind < 0 || ttisp->tt_abbrind > sp->charcnt) return -1; } for (i = 0; i < sp->charcnt; ++i) sp->chars[i] = *p++; sp->chars[i] = '\0'; /* ensure '\0' at end */ for (i = 0; i < sp->leapcnt; ++i) { register struct lsinfo * lsisp; lsisp = &sp->lsis[i]; lsisp->ls_trans = (stored == 4) ?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -