]> granicus.if.org Git - php/commitdiff
Bug #63699: performance improvements for varios ext/date functions
authorLars Strojny <lstrojny@php.net>
Sun, 6 Jan 2013 02:06:09 +0000 (03:06 +0100)
committerLars Strojny <lstrojny@php.net>
Sun, 6 Jan 2013 02:06:09 +0000 (03:06 +0100)
NEWS
ext/date/php_date.c
ext/date/php_date.h

diff --git a/NEWS b/NEWS
index 91b7b46637fd979874b0814ce091d959debb3f73..75c981681f20b4058dc619a399f6487553bd619b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -34,6 +34,10 @@ PHP                                                                        NEWS
   . Fixed bug #55438 (Curlwapper is not sending http header randomly).
     (phpnet@lostreality.org, Pierrick)
 
+- Date:
+  . Fixed bug #63699 (Performance improvements for various ext/date functions).
+    (Lars, original patch by njaguar at gmail dot com)
+
 20 Dec 2012, PHP 5.4.10
 
 - Core:
index ac119a35d5b5c1d630dd0e8f20982befdcae9dce..1837f945207a37815fe81d094571a96b070aafc3 100644 (file)
@@ -495,9 +495,11 @@ int php_date_global_timezone_db_enabled;
 /* on 90'35; common sunrise declaration (sun body disappeared) */
 #define DATE_SUNRISE_ZENITH "90.583333"
 
+static PHP_INI_MH(OnUpdate_date_timezone);
+
 /* {{{ INI Settings */
 PHP_INI_BEGIN()
-       STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdateString, default_timezone, zend_date_globals, date_globals)
+       STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
        PHP_INI_ENTRY("date.default_latitude",           DATE_DEFAULT_LATITUDE,        PHP_INI_ALL, NULL)
        PHP_INI_ENTRY("date.default_longitude",          DATE_DEFAULT_LONGITUDE,       PHP_INI_ALL, NULL)
        PHP_INI_ENTRY("date.sunset_zenith",              DATE_SUNSET_ZENITH,           PHP_INI_ALL, NULL)
@@ -599,6 +601,7 @@ static PHP_GINIT_FUNCTION(date)
        date_globals->default_timezone = NULL;
        date_globals->timezone = NULL;
        date_globals->tzcache = NULL;
+       date_globals->timezone_valid = 0;
 }
 /* }}} */
 
@@ -844,25 +847,53 @@ timelib_tzinfo *php_date_parse_tzfile_wrapper(char *formal_tzname, const timelib
 }
 /* }}} */
 
+// created this callback method to check the date.timezone only when changed, to increase performance and error on an ini_set line
+/* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
+static PHP_INI_MH(OnUpdate_date_timezone)
+{
+       if (OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC) == FAILURE) {
+               return FAILURE;
+       }
+
+       DATEG(timezone_valid) = 0;
+       if (stage == PHP_INI_STAGE_RUNTIME) {
+               if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, DATE_TZ_ERRMSG);
+               } else {
+                       DATEG(timezone_valid) = 1;
+               }
+       }
+
+       return SUCCESS;
+}
+/* }}} */
+
 /* {{{ Helper functions */
 static char* guess_timezone(const timelib_tzdb *tzdb TSRMLS_DC)
 {
        /* Checking configure timezone */
-       if (DATEG(timezone) && (strlen(DATEG(timezone)) > 0)) {
+       if (DATEG(timezone) && strlen(DATEG(timezone)) > 0) {
                return DATEG(timezone);
        }
        /* Check config setting for default timezone */
        if (!DATEG(default_timezone)) {
                /* Special case: ext/date wasn't initialized yet */
                zval ztz;
-               
-               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)) {
+
+               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)) {
                        return Z_STRVAL(ztz);
                }
-       } else if (*DATEG(default_timezone) && timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
+       } else if (*DATEG(default_timezone)) {
+               if (DATEG(timezone_valid) == 1) { // timezone already checked and validated
+                       return DATEG(default_timezone);
+               }
+
+               if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
+                       return "UTC";
+               }
+
+               DATEG(timezone_valid) = 1;
                return DATEG(default_timezone);
        }
        /* Fallback to UTC */
index c9c165050a72b56df0c06f6ccfd02ab228f938e6..f0b662b5d9f35dd6c7b654b7a5d4f5e8f253bb73 100644 (file)
@@ -150,10 +150,11 @@ struct _php_period_obj {
 };
 
 ZEND_BEGIN_MODULE_GLOBALS(date)
-       char      *default_timezone;
-       char      *timezone;
-       HashTable *tzcache;
+       char                    *default_timezone;
+       char                    *timezone;
+       HashTable               *tzcache;
        timelib_error_container *last_errors;
+       int                     timezone_valid;
 ZEND_END_MODULE_GLOBALS(date)
 
 #ifdef ZTS