From: Derick Rethans Date: Sat, 2 Jul 2005 21:19:25 +0000 (+0000) Subject: - Overhauled selecting the correct timezone. The timezone set with X-Git-Tag: php-5.1.0b3~218 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2b47899981318ef2cae922a59122989b0f78f143;p=php - Overhauled selecting the correct timezone. The timezone set with "date_timezone_set" override the TZ environment variable, which on its turn overrides the date.timezone setting. If none of the three is set, we fallback to UTC. - Added "date_timezone_set" function to set the timezone that the date functions will use. --- diff --git a/NEWS b/NEWS index 3c631d5979..ef997b79b6 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,8 @@ PHP NEWS ?? ??? 2005, PHP 5.1 Beta 3 - Upgraded bundled SQLite library for PDO:SQLite to 3.2.2 (Ilia) - Added PDO_MYSQL_ATTR_USE_BUFFERED_QUERY parameter for pdo_mysql. (Ilia) +- Added "date_timezone_set" function to set the timezone that the date + functions will use. (Derick) - Implemented feature request #33452 (Year belonging to ISO week). (Derick) - Fixed bug #33523 (Memory leak in xmlrpc_encode_request()). (Ilia) - Fixed bug #33491 (crash after extending MySQLi internal class). (Tony) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index bc490bd76e..3aa89c6106 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -32,13 +32,15 @@ function_entry date_functions[] = { PHP_FE(date, NULL) PHP_FE(gmdate, NULL) PHP_FE(strtotime, NULL) + PHP_FE(date_timezone_set, NULL) + PHP_FE(date_timezone_get, NULL) {NULL, NULL, NULL} }; ZEND_DECLARE_MODULE_GLOBALS(date) PHP_INI_BEGIN() - STD_PHP_INI_ENTRY("date.timezone", "GMT", PHP_INI_ALL, OnUpdateString, default_timezone, zend_date_globals, date_globals) + STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdateString, default_timezone, zend_date_globals, date_globals) PHP_INI_END() @@ -48,8 +50,8 @@ zend_module_entry date_module_entry = { date_functions, /* function list */ PHP_MINIT(date), /* process startup */ PHP_MSHUTDOWN(date), /* process shutdown */ - NULL, /* request startup */ - NULL, /* request shutdown */ + PHP_RINIT(date), /* request startup */ + PHP_RSHUTDOWN(date), /* request shutdown */ PHP_MINFO(date), /* extension info */ PHP_VERSION, /* extension version */ STANDARD_MODULE_PROPERTIES @@ -59,9 +61,29 @@ zend_module_entry date_module_entry = { static void php_date_init_globals(zend_date_globals *date_globals) { date_globals->default_timezone = NULL; + date_globals->timezone = NULL; } /* }}} */ +PHP_RINIT_FUNCTION(date) +{ + if (DATEG(timezone)) { + efree(DATEG(timezone)); + } + DATEG(timezone) = NULL; + + return SUCCESS; +} + +PHP_RSHUTDOWN_FUNCTION(date) +{ + if (DATEG(timezone)) { + efree(DATEG(timezone)); + } + DATEG(timezone) = NULL; + + return SUCCESS; +} PHP_MINIT_FUNCTION(date) { @@ -93,15 +115,39 @@ static char* guess_timezone(TSRMLS_D) { char *env; + /* Checking configure timezone */ + if (DATEG(timezone) && (strlen(DATEG(timezone)) > 0)) { + return DATEG(timezone); + } + /* Check environment variable */ env = getenv("TZ"); - if (env) { + if (env && *env) { return env; } - /* Check config setting */ - if (DATEG(default_timezone)) { + /* Check config setting for default timezone */ + if (DATEG(default_timezone) && (strlen(DATEG(default_timezone)) > 0)) { return DATEG(default_timezone); } - return "GMT"; + /* Fallback to UTC */ + return "UTC"; +} + +static timelib_tzinfo *get_timezone_info(TSRMLS_D) +{ + char *tz; + timelib_tzinfo *tzi; + + tz = guess_timezone(TSRMLS_C); + tzi = timelib_parse_tzfile(tz); + if (! tzi) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Timezone setting (date.timezone) or TZ environment variable contain an unknown timezone."); + tzi = timelib_parse_tzfile("UTC"); + + if (! tzi) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Timezone database is corrupt - this should *never* happen!"); + } + } + return tzi; } /* =[ date() and gmdate() ]================================================ */ @@ -256,12 +302,7 @@ static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime) t = timelib_time_ctor(); if (localtime) { - tzi = timelib_parse_tzfile(guess_timezone(TSRMLS_C)); - if (! tzi) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find any timezone setting"); - timelib_time_dtor(t); - RETURN_FALSE; - } + tzi = get_timezone_info(TSRMLS_C); timelib_unixtime2local(t, ts, tzi); } else { tzi = NULL; @@ -312,14 +353,7 @@ PHP_FUNCTION(strtotime) timelib_time *t, *now; timelib_tzinfo *tzi; - tzi = timelib_parse_tzfile(guess_timezone(TSRMLS_C)); - if (! tzi) { - tzi = timelib_parse_tzfile("GMT"); - } - if (! tzi) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find any timezone setting"); - RETURN_FALSE; - } + tzi = get_timezone_info(TSRMLS_C); if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "sl", ×, &time_len, &preset_ts) != FAILURE) { /* We have an initial timestamp */ @@ -366,6 +400,26 @@ PHP_FUNCTION(strtotime) } /* }}} */ +PHP_FUNCTION(date_timezone_set) +{ + char *zone; + int zone_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &zone, &zone_len) == FAILURE) { + RETURN_FALSE; + } + if (DATEG(timezone)) { + efree(DATEG(timezone)); + } + DATEG(timezone) = estrdup(zone); + RETURN_TRUE; +} + +PHP_FUNCTION(date_timezone_get) +{ + RETURN_STRING(DATEG(timezone), 0); +} + /* * Local variables: * tab-width: 4 diff --git a/ext/date/php_date.h b/ext/date/php_date.h index b8f5902e34..5b08a29024 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -24,16 +24,21 @@ extern zend_module_entry date_module_entry; #define phpext_date_ptr &date_module_entry -PHP_FUNCTION(strtotime); PHP_FUNCTION(date); PHP_FUNCTION(gmdate); +PHP_FUNCTION(strtotime); +PHP_FUNCTION(date_timezone_set); +PHP_FUNCTION(date_timezone_get); +PHP_RINIT_FUNCTION(date); +PHP_RSHUTDOWN_FUNCTION(date); PHP_MINIT_FUNCTION(date); PHP_MSHUTDOWN_FUNCTION(date); PHP_MINFO_FUNCTION(date); ZEND_BEGIN_MODULE_GLOBALS(date) char *default_timezone; + char *timezone; ZEND_END_MODULE_GLOBALS(date) #ifdef ZTS diff --git a/ext/date/tests/bug26198.phpt b/ext/date/tests/bug26198.phpt index c8bf39d53e..a5a9043c0e 100644 --- a/ext/date/tests/bug26198.phpt +++ b/ext/date/tests/bug26198.phpt @@ -2,6 +2,7 @@ Bug #26198 (strtotime handling of "M Y" and "Y M" format) --FILE-- diff --git a/ext/date/tests/bug29585.phpt b/ext/date/tests/bug29585.phpt index 17a3c97a2c..c86136729a 100644 --- a/ext/date/tests/bug29585.phpt +++ b/ext/date/tests/bug29585.phpt @@ -2,6 +2,7 @@ Bug #29585 (Support week numbers in strtotime()) --FILE-- diff --git a/ext/date/tests/default-timezone-2.phpt b/ext/date/tests/default-timezone-2.phpt index fe68ef4d49..c9a404bccb 100644 --- a/ext/date/tests/default-timezone-2.phpt +++ b/ext/date/tests/default-timezone-2.phpt @@ -4,7 +4,7 @@ date.timezone setting [2] date.timezone=Europe/Oslo --FILE-- --EXPECT-- diff --git a/ext/date/tests/timezone-configuration.phpt b/ext/date/tests/timezone-configuration.phpt new file mode 100644 index 0000000000..61d34384c1 --- /dev/null +++ b/ext/date/tests/timezone-configuration.phpt @@ -0,0 +1,19 @@ +--TEST-- +timezone configuration [1] +--INI-- +date.timezone=GMT +--FILE-- + +--EXPECT-- +1119125744 +1119129344 +1119125744