📄 localtime.c
字号:
/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2005, Digium, Inc. * * Mark Spencer <markster@digium.com> * * Most of this code is in the public domain, so clarified as of * June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov). * * All modifications to this code to abstract timezones away from * the environment are by Tilghman Lesher, <tlesher@vcch.com>, with * the copyright assigned to Digium. * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. *//*! \file * * Multi-timezone Localtime code * * The original source from this file may be obtained from ftp://elsie.nci.nih.gov/pub/ *//*** This file is in the public domain, so clarified as of** 1996-06-05 by Arthur David Olson.*//*** Leap second handling from Bradley White.** POSIX-style TZ environment variable handling from Guy Harris.*//* #define DEBUG *//*LINTLIBRARY*/#include "asterisk.h"ASTERISK_FILE_VERSION(__FILE__, "$Revision: 144991 $")#include <sys/stat.h>#include <fcntl.h>#include <float.h>#include "private.h"#include "tzfile.h"#include "asterisk/lock.h"#include "asterisk/localtime.h"#include "asterisk/strings.h"#include "asterisk/linkedlists.h"#include "asterisk/utils.h"#ifndef lint#ifndef NOIDstatic char __attribute__((unused)) elsieid[] = "@(#)localtime.c 8.5";#endif /* !defined NOID */#endif /* !defined lint */#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 *//*** 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 */static const char gmt[] = "GMT";static const struct timeval WRONG = { 0, 0 };/*! \note * 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 *//*!< \brief time type information */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 */};/*! \brief leap second information */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 */#ifndef TZ_STRLEN_MAX#define TZ_STRLEN_MAX 255#endif /* !defined TZ_STRLEN_MAX */struct state { /*! Name of the file that this references */ char name[TZ_STRLEN_MAX + 1]; 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]; AST_LIST_ENTRY(state) list;};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 int gmtload P((struct state * sp));static struct ast_tm * gmtsub P((const struct timeval * timep, long offset, struct ast_tm * tmp));static struct ast_tm * localsub P((const struct timeval * timep, long offset, struct ast_tm * tmp, const 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, const int base));static int normalize_overflow P((int * tensptr, int * unitsptr, const int base));static struct timeval time1 P((struct ast_tm * tmp, struct ast_tm * (*funcp) P((const struct timeval *, long, struct ast_tm *, const struct state *sp)), long offset, const struct state *sp));static struct timeval time2 P((struct ast_tm *tmp, struct ast_tm * (*funcp) P((const struct timeval *, long, struct ast_tm*, const struct state *sp)), long offset, int * okayp, const struct state *sp));static struct timeval time2sub P((struct ast_tm *tmp, struct ast_tm * (*funcp) (const struct timeval *, long, struct ast_tm*, const struct state *sp), long offset, int * okayp, int do_norm_secs, const struct state *sp));static struct ast_tm * timesub P((const struct timeval * timep, long offset, const struct state * sp, struct ast_tm * tmp));static int tmcomp P((const struct ast_tm * atmp, const struct ast_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 tzparse P((const char * name, struct state * sp, int lastditch));static AST_LIST_HEAD_STATIC(zonelist, state);#ifndef TZ_STRLEN_MAX#define TZ_STRLEN_MAX 255#endif /* !defined TZ_STRLEN_MAX *//*! \note** 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 long detzcode(const char * const codep){ long result; int i; result = (codep[0] & 0x80) ? ~0L : 0; for (i = 0; i < 4; ++i) result = (result << 8) | (codep[i] & 0xff); return result;}static time_t detzcode64(const char * const codep){ time_t result; 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 int differ_by_repeat(const time_t t1, const time_t t0){ const long long at1 = t1, at0 = t0; if (TYPE_INTEGRAL(time_t) && TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) return 0; return at1 - at0 == SECSPERREPEAT;}static int tzload(const char *name, struct state * const sp, const int doextend){ const char * p; int i; int fid; int stored; int nread; union { struct tzhead tzhead; char buf[2 * sizeof(struct tzhead) + 2 * sizeof *sp + 4 * TZ_MAX_TIMES]; } u; if (name == NULL && (name = TZDEFAULT) == NULL) return -1; { 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]; 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) return -1; } nread = read(fid, u.buf, sizeof u.buf); 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) { 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) { struct lsinfo * lsisp; lsisp = &sp->lsis[i]; lsisp->ls_trans = (stored == 4) ? detzcode(p) : detzcode64(p); p += stored; lsisp->ls_corr = detzcode(p); p += 4; } for (i = 0; i < sp->typecnt; ++i) { struct ttinfo * ttisp; ttisp = &sp->ttis[i]; if (ttisstdcnt == 0) ttisp->tt_ttisstd = FALSE; else { ttisp->tt_ttisstd = *p++; if (ttisp->tt_ttisstd != TRUE && ttisp->tt_ttisstd != FALSE) return -1; } } for (i = 0; i < sp->typecnt; ++i) { struct ttinfo * ttisp; ttisp = &sp->ttis[i]; if (ttisgmtcnt == 0) ttisp->tt_ttisgmt = FALSE; else { ttisp->tt_ttisgmt = *p++; if (ttisp->tt_ttisgmt != TRUE && ttisp->tt_ttisgmt != FALSE) return -1; } } /* ** Out-of-sort ats should mean we're running on a ** signed time_t system but using a data file with ** unsigned values (or vice versa). */ for (i = 0; i < sp->timecnt - 2; ++i) if (sp->ats[i] > sp->ats[i + 1]) { ++i; if (TYPE_SIGNED(time_t)) { /* ** Ignore the end (easy). */ sp->timecnt = i; } else { /* ** Ignore the beginning (harder). */ int j; for (j = 0; j + i < sp->timecnt; ++j) { sp->ats[j] = sp->ats[j + i]; sp->types[j] = sp->types[j + i]; } sp->timecnt = j; } break; } /* ** If this is an old file, we're done. */ if (u.tzhead.tzh_version[0] == '\0') break; nread -= p - u.buf; for (i = 0; i < nread; ++i) u.buf[i] = p[i]; /* ** If this is a narrow integer time_t system, we're done. */ if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t)) break; } if (doextend && nread > 2 && u.buf[0] == '\n' && u.buf[nread - 1] == '\n' && sp->typecnt + 2 <= TZ_MAX_TYPES) { struct state ts; int result; u.buf[nread - 1] = '\0'; result = tzparse(&u.buf[1], &ts, FALSE); if (result == 0 && ts.typecnt == 2 && sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) { for (i = 0; i < 2; ++i) ts.ttis[i].tt_abbrind += sp->charcnt; for (i = 0; i < ts.charcnt; ++i)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -