⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 localtime.c

📁 asterisk 是一个很有知名度开源软件
💻 C
📖 第 1 页 / 共 4 页
字号:
						sp->chars[sp->charcnt++] =							ts.chars[i];					i = 0;					while (i < ts.timecnt &&						ts.ats[i] <=						sp->ats[sp->timecnt - 1])							++i;					while (i < ts.timecnt &&					    sp->timecnt < TZ_MAX_TIMES) {						sp->ats[sp->timecnt] =							ts.ats[i];						sp->types[sp->timecnt] =							sp->typecnt +							ts.types[i];						++sp->timecnt;						++i;					}					sp->ttis[sp->typecnt++] = ts.ttis[0];					sp->ttis[sp->typecnt++] = ts.ttis[1];			}	}	i = 2 * YEARSPERREPEAT;	sp->goback = sp->goahead = sp->timecnt > i;	sp->goback = sp->goback && sp->types[i] == sp->types[0] &&		differ_by_repeat(sp->ats[i], sp->ats[0]);	sp->goahead = sp->goahead &&		sp->types[sp->timecnt - 1] == sp->types[sp->timecnt - 1 - i] &&		differ_by_repeat(sp->ats[sp->timecnt - 1],			 sp->ats[sp->timecnt - 1 - i]);	return 0;}static const int	mon_lengths[2][MONSPERYEAR] = {	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }};static const int	year_lengths[2] = {	DAYSPERNYEAR, DAYSPERLYEAR};/*! \brief** Given a pointer into a time zone string, scan until a character that is not** a valid character in a zone name is found. Return a pointer to that** character.*/static const char * getzname(const char *strp){	char	c;	while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&		c != '+')			++strp;	return strp;}/*! \brief** Given a pointer into an extended time zone string, scan until the ending** delimiter of the zone name is located. Return a pointer to the delimiter.**** As with getzname above, the legal character set is actually quite** restricted, with other characters producing undefined results.** We don't do any checking here; checking is done later in common-case code.*/static const char * getqzname(const char *strp, const int delim){	int	c;	while ((c = *strp) != '\0' && c != delim)		++strp;	return strp;}/*! \brief** Given a pointer into a time zone string, extract a number from that string.** Check that the number is within a specified range; if it is not, return** NULL.** Otherwise, return a pointer to the first character not part of the number.*/static const char *getnum(const char *strp, int *nump, const int min, const int max){	char	c;	int	num;	if (strp == NULL || !is_digit(c = *strp))		return NULL;	num = 0;	do {		num = num * 10 + (c - '0');		if (num > max)			return NULL;	/* illegal value */		c = *++strp;	} while (is_digit(c));	if (num < min)		return NULL;		/* illegal value */	*nump = num;	return strp;}/*! \brief** Given a pointer into a time zone string, extract a number of seconds,** in hh[:mm[:ss]] form, from the string.** If any error occurs, return NULL.** Otherwise, return a pointer to the first character not part of the number** of seconds.*/static const char *getsecs(const char *strp, long * const secsp){	int	num;	/*	** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like	** "M10.4.6/26", which does not conform to Posix,	** but which specifies the equivalent of	** ``02:00 on the first Sunday on or after 23 Oct''.	*/	strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);	if (strp == NULL)		return NULL;	*secsp = num * (long) SECSPERHOUR;	if (*strp == ':') {		++strp;		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);		if (strp == NULL)			return NULL;		*secsp += num * SECSPERMIN;		if (*strp == ':') {			++strp;			/* `SECSPERMIN' allows for leap seconds. */			strp = getnum(strp, &num, 0, SECSPERMIN);			if (strp == NULL)				return NULL;			*secsp += num;		}	}	return strp;}/*! \brief** Given a pointer into a time zone string, extract an offset, in** [+-]hh[:mm[:ss]] form, from the string.** If any error occurs, return NULL.** Otherwise, return a pointer to the first character not part of the time.*/static const char *getoffset(const char *strp, long *offsetp){	int	neg = 0;	if (*strp == '-') {		neg = 1;		++strp;	} else if (*strp == '+')		++strp;	strp = getsecs(strp, offsetp);	if (strp == NULL)		return NULL;		/* illegal time */	if (neg)		*offsetp = -*offsetp;	return strp;}/*! \brief** Given a pointer into a time zone string, extract a rule in the form** date[/time]. See POSIX section 8 for the format of "date" and "time".** If a valid rule is not found, return NULL.** Otherwise, return a pointer to the first character not part of the rule.*/static const char *getrule(const char *strp, struct rule *rulep){	if (*strp == 'J') {		/*		** Julian day.		*/		rulep->r_type = JULIAN_DAY;		++strp;		strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);	} else if (*strp == 'M') {		/*		** Month, week, day.		*/		rulep->r_type = MONTH_NTH_DAY_OF_WEEK;		++strp;		strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);		if (strp == NULL)			return NULL;		if (*strp++ != '.')			return NULL;		strp = getnum(strp, &rulep->r_week, 1, 5);		if (strp == NULL)			return NULL;		if (*strp++ != '.')			return NULL;		strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);	} else if (is_digit(*strp)) {		/*		** Day of year.		*/		rulep->r_type = DAY_OF_YEAR;		strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);	} else	return NULL;		/* invalid format */	if (strp == NULL)		return NULL;	if (*strp == '/') {		/*		** Time specified.		*/		++strp;		strp = getsecs(strp, &rulep->r_time);	} else	rulep->r_time = 2 * SECSPERHOUR;	/* default = 2:00:00 */	return strp;}/*! \brief** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the** year, a rule, and the offset from UTC at the time that rule takes effect,** calculate the Epoch-relative time that rule takes effect.*/static time_t transtime(const time_t janfirst, const int year, const struct rule *rulep, const long offset){	int	leapyear;	time_t	value;	int	i;	int		d, m1, yy0, yy1, yy2, dow;	INITIALIZE(value);	leapyear = isleap(year);	switch (rulep->r_type) {	case JULIAN_DAY:		/*		** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap		** years.		** In non-leap years, or if the day number is 59 or less, just		** add SECSPERDAY times the day number-1 to the time of		** January 1, midnight, to get the day.		*/		value = janfirst + (rulep->r_day - 1) * SECSPERDAY;		if (leapyear && rulep->r_day >= 60)			value += SECSPERDAY;		break;	case DAY_OF_YEAR:		/*		** n - day of year.		** Just add SECSPERDAY times the day number to the time of		** January 1, midnight, to get the day.		*/		value = janfirst + rulep->r_day * SECSPERDAY;		break;	case MONTH_NTH_DAY_OF_WEEK:		/*		** Mm.n.d - nth "dth day" of month m.		*/		value = janfirst;		for (i = 0; i < rulep->r_mon - 1; ++i)			value += mon_lengths[leapyear][i] * SECSPERDAY;		/*		** Use Zeller's Congruence to get day-of-week of first day of		** month.		*/		m1 = (rulep->r_mon + 9) % 12 + 1;		yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;		yy1 = yy0 / 100;		yy2 = yy0 % 100;		dow = ((26 * m1 - 2) / 10 +			1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;		if (dow < 0)			dow += DAYSPERWEEK;		/*		** "dow" is the day-of-week of the first day of the month. Get		** the day-of-month (zero-origin) of the first "dow" day of the		** month.		*/		d = rulep->r_day - dow;		if (d < 0)			d += DAYSPERWEEK;		for (i = 1; i < rulep->r_week; ++i) {			if (d + DAYSPERWEEK >=				mon_lengths[leapyear][rulep->r_mon - 1])					break;			d += DAYSPERWEEK;		}		/*		** "d" is the day-of-month (zero-origin) of the day we want.		*/		value += d * SECSPERDAY;		break;	}	/*	** "value" is the Epoch-relative time of 00:00:00 UTC on the day in	** question. To get the Epoch-relative time of the specified local	** time on that day, add the transition time and the current offset	** from UTC.	*/	return value + rulep->r_time + offset;}/*! \note** Given a POSIX section 8-style TZ string, fill in the rule tables as** appropriate.*/static int tzparse(const char *name, struct state *sp, const int lastditch){	const char *			stdname;	const char *			dstname;	size_t				stdlen;	size_t				dstlen;	long				stdoffset;	long				dstoffset;	time_t *		atp;	unsigned char *	typep;	char *			cp;	int			load_result;	INITIALIZE(dstname);	stdname = name;	if (lastditch) {		stdlen = strlen(name);	/* length of standard zone name */		name += stdlen;		if (stdlen >= sizeof sp->chars)			stdlen = (sizeof sp->chars) - 1;		stdoffset = 0;	} else {		if (*name == '<') {			name++;			stdname = name;			name = getqzname(name, '>');			if (*name != '>')				return -1;			stdlen = name - stdname;			name++;		} else {			name = getzname(name);			stdlen = name - stdname;		}		if (*name == '\0')			return -1;		name = getoffset(name, &stdoffset);		if (name == NULL)			return -1;	}	load_result = tzload(TZDEFRULES, sp, FALSE);	if (load_result != 0)		sp->leapcnt = 0;		/* so, we're off a little */	if (*name != '\0') {		if (*name == '<') {			dstname = ++name;			name = getqzname(name, '>');			if (*name != '>')				return -1;			dstlen = name - dstname;			name++;		} else {			dstname = name;			name = getzname(name);			dstlen = name - dstname; /* length of DST zone name */		}		if (*name != '\0' && *name != ',' && *name != ';') {			name = getoffset(name, &dstoffset);			if (name == NULL)				return -1;		} else	dstoffset = stdoffset - SECSPERHOUR;		if (*name == '\0' && load_result != 0)			name = TZDEFRULESTRING;		if (*name == ',' || *name == ';') {			struct rule	start;			struct rule	end;			int	year;			time_t	janfirst;			time_t		starttime;			time_t		endtime;			++name;			if ((name = getrule(name, &start)) == NULL)				return -1;			if (*name++ != ',')				return -1;			if ((name = getrule(name, &end)) == NULL)				return -1;			if (*name != '\0')				return -1;			sp->typecnt = 2;	/* standard time and DST */			/*			** Two transitions per year, from EPOCH_YEAR forward.			*/			sp->ttis[0].tt_gmtoff = -dstoffset;			sp->ttis[0].tt_isdst = 1;			sp->ttis[0].tt_abbrind = stdlen + 1;			sp->ttis[1].tt_gmtoff = -stdoffset;			sp->ttis[1].tt_isdst = 0;			sp->ttis[1].tt_abbrind = 0;			atp = sp->ats;			typep = sp->types;			janfirst = 0;			sp->timecnt = 0;			for (year = EPOCH_YEAR;			    sp->timecnt + 2 <= TZ_MAX_TIMES;			    ++year) {			    	time_t	newfirst;				starttime = transtime(janfirst, year, &start,					stdoffset);				endtime = transtime(janfirst, year, &end,					dstoffset);				if (starttime > endtime) {					*atp++ = endtime;					*typep++ = 1;	/* DST ends */					*atp++ = starttime;					*typep++ = 0;	/* DST begins */				} else {					*atp++ = starttime;					*typep++ = 0;	/* DST begins */					*atp++ = endtime;					*typep++ = 1;	/* DST ends */				}				sp->timecnt += 2;				newfirst = janfirst;				newfirst += year_lengths[isleap(year)] *					SECSPERDAY;				if (newfirst <= janfirst)					break;				janfirst = newfirst;			}		} else {			long	theirstdoffset;			long	theirdstoffset;			long	theiroffset;			int	isdst;			int	i;			int	j;			if (*name != '\0')				return -1;			/*			** Initial values of theirstdoffset and theirdstoffset.			*/			theirstdoffset = 0;			for (i = 0; i < sp->timecnt; ++i) {				j = sp->types[i];				if (!sp->ttis[j].tt_isdst) {					theirstdoffset =						-sp->ttis[j].tt_gmtoff;					break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -