Skip to content

Commit 67557fc

Browse files
committed
Bug #63699: performance improvements for varios ext/date functions
1 parent 2feea39 commit 67557fc

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ PHP NEWS
3434
. Fixed bug #55438 (Curlwapper is not sending http header randomly).
3535
([email protected], Pierrick)
3636

37+
- Date:
38+
. Fixed bug #63699 (Performance improvements for various ext/date functions).
39+
(Lars, original patch by njaguar at gmail dot com)
40+
3741
20 Dec 2012, PHP 5.4.10
3842

3943
- Core:

ext/date/php_date.c

+39-8
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,11 @@ int php_date_global_timezone_db_enabled;
495495
/* on 90'35; common sunrise declaration (sun body disappeared) */
496496
#define DATE_SUNRISE_ZENITH "90.583333"
497497

498+
static PHP_INI_MH(OnUpdate_date_timezone);
499+
498500
/* {{{ INI Settings */
499501
PHP_INI_BEGIN()
500-
STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdateString, default_timezone, zend_date_globals, date_globals)
502+
STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
501503
PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL)
502504
PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL)
503505
PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL)
@@ -599,6 +601,7 @@ static PHP_GINIT_FUNCTION(date)
599601
date_globals->default_timezone = NULL;
600602
date_globals->timezone = NULL;
601603
date_globals->tzcache = NULL;
604+
date_globals->timezone_valid = 0;
602605
}
603606
/* }}} */
604607

@@ -844,25 +847,53 @@ timelib_tzinfo *php_date_parse_tzfile_wrapper(char *formal_tzname, const timelib
844847
}
845848
/* }}} */
846849

850+
// created this callback method to check the date.timezone only when changed, to increase performance and error on an ini_set line
851+
/* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
852+
static PHP_INI_MH(OnUpdate_date_timezone)
853+
{
854+
if (OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC) == FAILURE) {
855+
return FAILURE;
856+
}
857+
858+
DATEG(timezone_valid) = 0;
859+
if (stage == PHP_INI_STAGE_RUNTIME) {
860+
if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
861+
php_error_docref(NULL TSRMLS_CC, E_WARNING, DATE_TZ_ERRMSG);
862+
} else {
863+
DATEG(timezone_valid) = 1;
864+
}
865+
}
866+
867+
return SUCCESS;
868+
}
869+
/* }}} */
870+
847871
/* {{{ Helper functions */
848872
static char* guess_timezone(const timelib_tzdb *tzdb TSRMLS_DC)
849873
{
850874
/* Checking configure timezone */
851-
if (DATEG(timezone) && (strlen(DATEG(timezone)) > 0)) {
875+
if (DATEG(timezone) && strlen(DATEG(timezone)) > 0) {
852876
return DATEG(timezone);
853877
}
854878
/* Check config setting for default timezone */
855879
if (!DATEG(default_timezone)) {
856880
/* Special case: ext/date wasn't initialized yet */
857881
zval ztz;
858-
859-
if (SUCCESS == zend_get_configuration_directive("date.timezone", sizeof("date.timezone"), &ztz) &&
860-
Z_TYPE(ztz) == IS_STRING &&
861-
Z_STRLEN(ztz) > 0 &&
862-
timelib_timezone_id_is_valid(Z_STRVAL(ztz), tzdb)) {
882+
883+
if (SUCCESS == zend_get_configuration_directive("date.timezone", sizeof("date.timezone"), &ztz) && Z_TYPE(ztz) == IS_STRING && Z_STRLEN(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL(ztz), tzdb)) {
863884
return Z_STRVAL(ztz);
864885
}
865-
} else if (*DATEG(default_timezone) && timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
886+
} else if (*DATEG(default_timezone)) {
887+
if (DATEG(timezone_valid) == 1) { // timezone already checked and validated
888+
return DATEG(default_timezone);
889+
}
890+
891+
if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
892+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
893+
return "UTC";
894+
}
895+
896+
DATEG(timezone_valid) = 1;
866897
return DATEG(default_timezone);
867898
}
868899
/* Fallback to UTC */

ext/date/php_date.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ struct _php_period_obj {
150150
};
151151

152152
ZEND_BEGIN_MODULE_GLOBALS(date)
153-
char *default_timezone;
154-
char *timezone;
155-
HashTable *tzcache;
153+
char *default_timezone;
154+
char *timezone;
155+
HashTable *tzcache;
156156
timelib_error_container *last_errors;
157+
int timezone_valid;
157158
ZEND_END_MODULE_GLOBALS(date)
158159

159160
#ifdef ZTS

0 commit comments

Comments
 (0)