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

📄 datetime.c

📁 php-4.4.7学习linux时下载的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    +----------------------------------------------------------------------+   | PHP Version 4                                                        |   +----------------------------------------------------------------------+   | Copyright (c) 1997-2007 The PHP Group                                |   +----------------------------------------------------------------------+   | This source file is subject to version 3.01 of the PHP license,      |   | that is bundled with this package in the file LICENSE, and is        |   | available through the world-wide-web at the following url:           |   | http://www.php.net/license/3_01.txt                                  |   | If you did not receive a copy of the PHP license and are unable to   |   | obtain it through the world-wide-web, please send a note to          |   | license@php.net so we can mail you a copy immediately.               |   +----------------------------------------------------------------------+   | Authors: Andi Gutmans <andi@zend.com>                                |   |          Zeev Suraski <zeev@zend.com>                                |   |          Rasmus Lerdorf <rasmus@php.net>                             |   +----------------------------------------------------------------------+ *//* $Id: datetime.c,v 1.96.2.19.2.4 2007/01/01 09:46:47 sebastian Exp $ */#include "php.h"#include "zend_operators.h"#include "datetime.h"#include "php_globals.h"#include <time.h>#ifdef HAVE_SYS_TIME_H# include <sys/time.h>#endif#include <stdio.h>#include "php_parsedate.h"char *mon_full_names[] = {	"January", "February", "March", "April",	"May", "June", "July", "August",	"September", "October", "November", "December"};char *mon_short_names[] = {	"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};char *day_full_names[] = {	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};char *day_short_names[] = {	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};#if !defined(HAVE_TM_ZONE) && !defined(_TIMEZONE) && !defined(HAVE_DECLARED_TIMEZONE)#ifdef NETWARE#define timezone    _timezone   /* timezone is called '_timezone' in new version of LibC */#endifextern time_t timezone;extern int daylight;#endifstatic int phpday_tab[2][12] = {	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};#define isleap(year) ((((year) % 4) == 0 && ((year) % 100) != 0) || ((year) % 400)==0)#define YEAR_BASE 1900/* {{{ proto int time(void)   Return current UNIX timestamp */PHP_FUNCTION(time){	RETURN_LONG((long)time(NULL));}/* }}} *//* {{{ php_mktime */void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gm){	pval **arguments[7];	struct tm *ta, tmbuf;	time_t t, seconds;	int i, gmadjust, arg_count = ZEND_NUM_ARGS();	int is_dst = -1, chgsecs = 0;	long val;	if (arg_count > 7 || zend_get_parameters_array_ex(arg_count, arguments) == FAILURE) {		WRONG_PARAM_COUNT;	}	/* convert supplied arguments to longs */	for (i = 0; i < arg_count; i++) {		convert_to_long_ex(arguments[i]);	}	t = time(NULL);#ifdef HAVE_TZSET	tzset();#endif	/*	** Set default time parameters with local time values,	** EVEN when some GMT time parameters are specified!	** This may give strange result, with PHP gmmktime(0, 0, 0),	** which is assumed to return GMT midnight time	** for today (in localtime), so that the result time may be	** AFTER or BEFORE the current time.	** May be we should initialize tn using gmtime(), so that	** default parameters for PHP gmmktime would be the current	** GMT time values...	*/	ta = php_localtime_r(&t, &tmbuf);	/* Let DST be unknown. mktime() should compute the right value	** and behave correctly. Unless the user overrides this.	*/	ta->tm_isdst = -1;	/*	** Now change date values with supplied parameters.	*/	switch(arg_count) {	case 7: /* daylight saving time flag */#ifdef PHP_WIN32		if (daylight > 0) {			ta->tm_isdst = is_dst = Z_LVAL_PP(arguments[6]);		} else {			ta->tm_isdst = is_dst = 0;		}#else		ta->tm_isdst = is_dst = Z_LVAL_PP(arguments[6]);#endif		/* fall-through */	case 6: /* year */		/* special case: 		   a zero in year, month and day is considered illegal		   as it would be interpreted as 30.11.1999 otherwise		*/		if (  (  Z_LVAL_PP(arguments[5])==0)			  &&(Z_LVAL_PP(arguments[4])==0)			  &&(Z_LVAL_PP(arguments[3])==0)			  ) {			RETURN_LONG(-1);		}		/*		** Accept parameter in range 0..1000 interpreted as 1900..2900		** (if 100 is given, it means year 2000)		** or in range 1001..9999 interpreted as is (this will store		** negative tm_year for years in range 1001..1899)		** This function is then Y2K ready, and accepts a wide range of		** dates including the whole gregorian calendar.		** But it cannot represent ancestral dates prior to year 1001.		** Additionally, input parameters of 0..70 are mapped to 100..170		*/		if (Z_LVAL_PP(arguments[5]) < 70)			ta->tm_year = Z_LVAL_PP(arguments[5]) + 100;		else			ta->tm_year = Z_LVAL_PP(arguments[5])			  - ((Z_LVAL_PP(arguments[5]) > 1000) ? 1900 : 0);		/* fall-through */	case 5: /* day in month (1-based) */ 		val = (*arguments[4])->value.lval; 		if (val < 1) { 			chgsecs += (1-val) * 60*60*24; 			val = 1; 					} 		ta->tm_mday = val; 		/* fall-through */ 	case 4: /* month (zero-based) */		val = (*arguments[3])->value.lval - 1; 		while (val < 0) { 			val += 12; ta->tm_year--; 		} 		ta->tm_mon = val; 		/* fall-through */ 	case 3: /* second */		val = (*arguments[2])->value.lval; 		if (val < 1) { 			chgsecs += (1-val); val = 1; 		} 		ta->tm_sec = val; 		/* fall-through */ 	case 2: /* minute */		val = (*arguments[1])->value.lval; 		if (val < 1) { 			chgsecs += (1-val) * 60; val = 1; 		} 		ta->tm_min = val; 		/* fall-through */ 	case 1: /* hour */		val = (*arguments[0])->value.lval; 		/*		   We avoid midnight and a couple of hours after midnight here to work around		   various OS-level bugs in mktime and specifically daylight savings time issues		   in many mktime implementation.		   See bugs #27533 and #27719 for more info.		*/		if (val < 4) { 			chgsecs += (4-val) * 60*60; val = 4; 		} 		ta->tm_hour = val; 		/* fall-through */ 	case 0: 		break; 	} 		t = mktime(ta); #ifdef PHP_WIN32	if (t - chgsecs < 0) {		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Windows does not support negative values for this function");		RETURN_LONG(-1);	}#endif	seconds = t - chgsecs;	/*	   Here we check to see if the chgsecs fuzz factor we applied caused us to	   move from dst to non-dst or vice-versa.  If so we adjust accordingly to	   avoid being off by an hour on the dst changeover date.	*/	if (is_dst == -1) {		struct tm t1, t2;		t1 = *localtime(&t);		t2 = *localtime(&seconds);		if (t1.tm_isdst != t2.tm_isdst) {			seconds += (t1.tm_isdst == 1) ? 3600 : -3600;			ta = localtime(&seconds);		}		/*		   If the user didn't specify whether the timestamp passed in was dst or not		   then we fill it in based on the dst setting at the evaluated timestamp		   at the current TZ		*/		is_dst = ta->tm_isdst; 	}		if (gm) {#if HAVE_TM_GMTOFF	    /*		** mktime(ta) very nicely just filled ta->tm_gmtoff with		** the exactly right value for adjustment if we want GMT.	    */	    gmadjust = ta->tm_gmtoff;#else	    /*	    ** If correcting for daylight savings time, we set the adjustment to		** the value of timezone - 3600 seconds.	    */#ifdef __CYGWIN__	    gmadjust = -(is_dst ? _timezone - 3600 : _timezone);#else	    gmadjust = -(is_dst ? timezone - 3600 : timezone);#endif#endif		seconds += gmadjust;	}	RETURN_LONG(seconds);}/* }}} *//* {{{ proto int mktime(int hour, int min, int sec, int mon, int day, int year)   Get UNIX timestamp for a date */PHP_FUNCTION(mktime){	php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);}/* }}} *//* {{{ proto int gmmktime(int hour, int min, int sec, int mon, int day, int year)   Get UNIX timestamp for a GMT date */PHP_FUNCTION(gmmktime){	php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);}/* }}} *//* {{{ php_date */static void php_date(INTERNAL_FUNCTION_PARAMETERS, int gm){	pval **format, **timestamp;	time_t the_time;	struct tm *ta, tmbuf;	int i, size = 0, length, h, beat, fd, wd, yd, wk;	char tmp_buff[32];#if !HAVE_TM_GMTOFF	long tzone;	char *tname[2]= {"GMT Standard Time", "BST"};#endif	switch(ZEND_NUM_ARGS()) {	case 1:		if (zend_get_parameters_ex(1, &format) == FAILURE) {			WRONG_PARAM_COUNT;		}		the_time = time(NULL);		break;	case 2:		if (zend_get_parameters_ex(2, &format, &timestamp) == FAILURE) {			WRONG_PARAM_COUNT;		}		convert_to_long_ex(timestamp);		the_time = Z_LVAL_PP(timestamp);#ifdef PHP_WIN32		if (the_time < 0) {			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Windows does not support dates prior to midnight (00:00:00), January 1, 1970");			RETURN_FALSE;		}#endif		break;	default:		WRONG_PARAM_COUNT;	}	convert_to_string_ex(format);	if (gm) {		ta = php_gmtime_r(&the_time, &tmbuf);#if !HAVE_TM_GMTOFF		tzone = 0;#endif	} else {		ta = php_localtime_r(&the_time, &tmbuf);#if !HAVE_TM_GMTOFF#ifdef __CYGWIN__		tzone = _timezone;#else		tzone = timezone;#endif		if (tzname[0] != NULL) {			tname[0] = tzname[0];		} else {			tname[0] = "???";		}		if (tzname[1] != NULL) {			tname[1] = tzname[1];		}#endif	}	if (!ta) {			/* that really shouldn't happen... */		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected error");		RETURN_FALSE;	}	for (i = 0; i < Z_STRLEN_PP(format); i++) {		switch (Z_STRVAL_PP(format)[i]) {			case 'r':		/* rfc822 format */				size += 31;				break;			case 'U':		/* seconds since the epoch */				size += 10;				break;			case 'F':		/* month, textual, full */			case 'l':		/* day (of the week), textual */				size += 28;				break;			case 'T':		/* timezone name */#if HAVE_TM_ZONE				size += strlen(ta->tm_zone);#elif HAVE_TZNAME				if (ta->tm_isdst > 0 ) {					size += strlen(tname[1]);				} else {					size += strlen(tname[0]);				}#endif				break;			case 'Z':		/* timezone offset in seconds */				size += 6;				break;			case 'O':		/* GMT offset in [+-]HHMM format */				size += 5;				break;			case 'Y':		/* year, numeric, 4 digits */				size += 4;				break;			case 'M':		/* month, textual, 3 letters */			case 'D':		/* day, textual, 3 letters */			case 'z':		/* day of the year, 1 to 366 */			case 'B':		/* Swatch Beat, 3 digits */				size += 3;				break;			case 'y':		/* year, numeric, 2 digits */			case 'm':		/* month, numeric */			case 'n':		/* month, numeric, no leading zeroes */			case 'd':		/* day of the month, numeric */			case 'j':		/* day of the month, numeric, no leading zeros */			case 'H':		/* hour, numeric, 24 hour format */			case 'h':		/* hour, numeric, 12 hour format */			case 'G':		/* hour, numeric, 24 hour format, no leading zeroes */			case 'g':		/* hour, numeric, 12 hour format, no leading zeroes */			case 'i':		/* minutes, numeric */			case 's':		/* seconds, numeric */			case 'A':		/* AM/PM */			case 'a':		/* am/pm */			case 'S':		/* standard english suffix for the day of the month (e.g. 3rd, 2nd, etc) */			case 't':		/* days in current month */			case 'W':		/* ISO-8601 week number of year, weeks starting on Monday */				size += 2;				break;			case '\\':				if (i < Z_STRLEN_PP(format) - 1) {					i++;				}				size ++;				break;			case 'L':		/* boolean for leap year */			case 'w':		/* day of the week, numeric */			case 'I':		/* DST? */			default:				size++;				break;		}	}	Z_STRVAL_P(return_value) = (char *) emalloc(size + 1);	Z_STRVAL_P(return_value)[0] = '\0';	for (i = 0; i < Z_STRLEN_PP(format); i++) {		switch (Z_STRVAL_PP(format)[i]) {			case '\\':				if (i < Z_STRLEN_PP(format) - 1) {					char ch[2];					ch[0]=Z_STRVAL_PP(format)[i + 1];					ch[1]='\0';					strcat(Z_STRVAL_P(return_value), ch);					i++;				}				break;			case 'U':		/* seconds since the epoch */				sprintf(tmp_buff, "%ld", (long)the_time); /* SAFE */				strcat(Z_STRVAL_P(return_value), tmp_buff);				break;			case 'F':		/* month, textual, full */				strcat(Z_STRVAL_P(return_value), mon_full_names[ta->tm_mon]);				break;			case 'l':		/* day (of the week), textual, full */				strcat(Z_STRVAL_P(return_value), day_full_names[ta->tm_wday]);				break;			case 'Y':		/* year, numeric, 4 digits */				sprintf(tmp_buff, "%d", ta->tm_year + YEAR_BASE);  /* SAFE */				strcat(Z_STRVAL_P(return_value), tmp_buff);				break;			case 'M':		/* month, textual, 3 letters */				strcat(Z_STRVAL_P(return_value), mon_short_names[ta->tm_mon]);				break;			case 'D':		/* day (of the week), textual, 3 letters */				strcat(Z_STRVAL_P(return_value), day_short_names[ta->tm_wday]);				break;			case 'z':		/* day (of the year) */				sprintf(tmp_buff, "%d", ta->tm_yday);  /* SAFE */				strcat(Z_STRVAL_P(return_value), tmp_buff);				break;			case 'y':		/* year, numeric, 2 digits */				sprintf(tmp_buff, "%02d", ((ta->tm_year)%100));  /* SAFE */				strcat(Z_STRVAL_P(return_value), tmp_buff);				break;			case 'm':		/* month, numeric */				sprintf(tmp_buff, "%02d", ta->tm_mon + 1);  /* SAFE */				strcat(Z_STRVAL_P(return_value), tmp_buff);				break;

⌨️ 快捷键说明

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