]> granicus.if.org Git - php/commitdiff
- Overhauled selecting the correct timezone. The timezone set with
authorDerick Rethans <derick@php.net>
Sat, 2 Jul 2005 21:19:25 +0000 (21:19 +0000)
committerDerick Rethans <derick@php.net>
Sat, 2 Jul 2005 21:19:25 +0000 (21:19 +0000)
  "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.

NEWS
ext/date/php_date.c
ext/date/php_date.h
ext/date/tests/bug26198.phpt
ext/date/tests/bug29585.phpt
ext/date/tests/default-timezone-2.phpt
ext/date/tests/timezone-configuration.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 3c631d59795fcea798427452d75b7fdc33446945..ef997b79b610783325cd39ede3636690e5389c1d 100644 (file)
--- 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)
index bc490bd76e3d94542fbaf39adc27b14957d8fb3a..3aa89c6106de1ebc45a0476633b19e247114b3a1 100644 (file)
@@ -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", &times, &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
index b8f5902e34ceb831a16a07e4190f44cc88b50b39..5b08a2902458ce2bf7816e70b7ea886c01275688 100644 (file)
 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
index c8bf39d53ea2118d608b3d57de9a2ce2427954e3..a5a9043c0e9c4ce042d8039d75841c384431eb1f 100644 (file)
@@ -2,6 +2,7 @@
 Bug #26198 (strtotime handling of "M Y" and "Y M" format)
 --FILE--
 <?php
+       putenv("TZ=");
        echo gmdate("F Y (Y-m-d H:i:s T)\n", strtotime("Oct 2001"));
        echo gmdate("M Y (Y-m-d H:i:s T)\n", strtotime("2001 Oct"));
 ?>
index 17a3c97a2cb4a7caa1c0eb64e821e5753ae0f759..c86136729a0ab6100bc990190a2e4eabfd15cb42 100644 (file)
@@ -2,6 +2,7 @@
 Bug #29585 (Support week numbers in strtotime())
 --FILE--
 <?php
+putenv('TZ=');
 echo gmdate("Y-m-d H:i:s", strtotime("2004W30"));
 
 ?>
index fe68ef4d4930befda70b3876d9a25952aab370a6..c9a404bccba15b8fb964dae1a6990ba41980fdae 100644 (file)
@@ -4,7 +4,7 @@ date.timezone setting [2]
 date.timezone=Europe/Oslo
 --FILE--
 <?php
-       putenv('TZ'); // clean TZ so that it doesn't bypass the ini option
+       putenv('TZ='); // clean TZ so that it doesn't bypass the ini option
        echo strtotime("2005-06-18 22:15:44");
 ?>
 --EXPECT--
diff --git a/ext/date/tests/timezone-configuration.phpt b/ext/date/tests/timezone-configuration.phpt
new file mode 100644 (file)
index 0000000..61d3438
--- /dev/null
@@ -0,0 +1,19 @@
+--TEST--
+timezone configuration [1]
+--INI--
+date.timezone=GMT
+--FILE--
+<?php
+       putenv('TZ=Europe/Oslo');
+       echo strtotime("2005-06-18 22:15:44"), "\n";
+
+       putenv('TZ=Europe/London');
+       echo strtotime("2005-06-18 22:15:44"), "\n";
+
+       date_timezone_set('Europe/Oslo');
+       echo strtotime("2005-06-18 22:15:44"), "\n";
+?>
+--EXPECT--
+1119125744
+1119129344
+1119125744