derick          Wed Oct  5 14:38:31 2005 EDT

  Modified files:              
    /php-src/ext/date   php_date.c php_date.h 
  Log:
  - MF51: Windows Timezone guessing code
  
  
http://cvs.php.net/diff.php/php-src/ext/date/php_date.c?r1=1.57&r2=1.58&ty=u
Index: php-src/ext/date/php_date.c
diff -u php-src/ext/date/php_date.c:1.57 php-src/ext/date/php_date.c:1.58
--- php-src/ext/date/php_date.c:1.57    Mon Oct  3 19:43:43 2005
+++ php-src/ext/date/php_date.c Wed Oct  5 14:38:30 2005
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_date.c,v 1.57 2005/10/03 23:43:43 tony2001 Exp $ */
+/* $Id: php_date.c,v 1.58 2005/10/05 18:38:30 derick Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -187,6 +187,14 @@
 }
 /* }}} */
 
+
+static void _php_date_tzinfo_dtor(void *tzinfo)
+{
+       timelib_tzinfo **tzi = (timelib_tzinfo **)tzinfo;
+
+       timelib_tzinfo_dtor(*tzi);
+}
+
 /* {{{ PHP_RINIT_FUNCTION */
 PHP_RINIT_FUNCTION(date)
 {
@@ -194,6 +202,7 @@
                efree(DATEG(timezone));
        }
        DATEG(timezone) = NULL;
+       zend_hash_init(&DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
 
        return SUCCESS;
 }
@@ -206,6 +215,7 @@
                efree(DATEG(timezone));
        }
        DATEG(timezone) = NULL;
+       zend_hash_destroy(&DATEG(tzcache));
 
        return SUCCESS;
 }
@@ -269,6 +279,20 @@
 }
 /* }}} */
 
+/* {{{ Timezone Cache functions */
+static timelib_tzinfo *php_date_parse_tzfile(char *tzname, timelib_tzdb *tzdb 
TSRMLS_DC)
+{
+       timelib_tzinfo *tzi, **ptzi;
+
+       if (zend_hash_find(&DATEG(tzcache), tzname, strlen(tzname) + 1, (void 
**) &ptzi) == SUCCESS) {
+               return *ptzi;
+       }
+
+       tzi = timelib_parse_tzfile(tzname, tzdb);
+       zend_hash_add(&DATEG(tzcache), tzname, strlen(tzname) + 1, (void *) 
&tzi, sizeof(timelib_tzinfo*), NULL);
+       return tzi;
+}
+/* }}} */
 
 /* {{{ Helper functions */
 static char* guess_timezone(TSRMLS_D)
@@ -281,7 +305,7 @@
        }
        /* Check environment variable */
        env = getenv("TZ");
-       if (env && *env && strlen(env)) {
+       if (env && *env) {
                return env;
        }
        /* Check config setting for default timezone */
@@ -306,6 +330,37 @@
                return tzid;
        }
 #endif
+#ifdef PHP_WIN32
+       {
+               char *tzid;
+               TIME_ZONE_INFORMATION tzi;
+
+               switch (GetTimeZoneInformation(&tzi))
+               {
+                       case TIME_ZONE_ID_UNKNOWN:
+                               /* we have no clue what it is, return UTC */
+                               php_error_docref(NULL TSRMLS_CC, E_STRICT, "It 
is not safe to rely on the systems timezone settings, please use the 
date.timezone setting, the TZ environment variable or the 
date_default_timezone_set() function. We use 'UTC' instead.");
+                               break;
+
+                       case TIME_ZONE_ID_STANDARD:
+                               tzid = timelib_timezone_id_from_abbr("", 
(tzi.Bias + tzi.StandardBias) * -60, 0);
+                               if (! tzid) {
+                                       tzid = "UTC";
+                               }
+                               php_error_docref(NULL TSRMLS_CC, E_STRICT, "It 
is not safe to rely on the systems timezone settings, please use the 
date.timezone setting, the TZ environment variable or the 
date_default_timezone_set() function. We use '%s' for '%.1f/no DST' instead.", 
tzid, (float) ((tzi.Bias + tzi.StandardBias) / -60));
+                               break;
+
+                       case TIME_ZONE_ID_DAYLIGHT:
+                               tzid = timelib_timezone_id_from_abbr("", 
(tzi.Bias + tzi.DaylightBias) * -60, 1);
+                               if (! tzid) {
+                                       tzid = "UTC";
+                               }
+                               php_error_docref(NULL TSRMLS_CC, E_STRICT, "It 
is not safe to rely on the systems timezone settings, please use the 
date.timezone setting, the TZ environment variable or the 
date_default_timezone_set() function. We use '%s' for '%.1f/DST' instead.", 
tzid, (float) ((tzi.Bias + tzi.DaylightBias) / -60));
+                               break;
+
+               }
+       }
+#endif
        /* Fallback to UTC */
        return "UTC";
 }
@@ -316,10 +371,10 @@
        timelib_tzinfo *tzi;
        
        tz = guess_timezone(TSRMLS_C);
-       tzi = timelib_parse_tzfile(tz, DATE_TIMEZONEDB);
+       tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB TSRMLS_CC);
        if (! tzi) {
                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Timezone setting 
(date.timezone) or TZ environment variable contains an unknown timezone.");
-               tzi = timelib_parse_tzfile("UTC", DATE_TIMEZONEDB);
+               tzi = php_date_parse_tzfile("UTC", DATE_TIMEZONEDB TSRMLS_CC);
 
                if (! tzi) {
                        php_error_docref(NULL TSRMLS_CC, E_ERROR, "Timezone 
database is corrupt - this should *never* happen!");
@@ -600,11 +655,14 @@
 
 static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
 {
-       char *format;
-       int   format_len;
-       time_t  ts = time(NULL);
-       char           *string;
+       char   *format;
+       int     format_len;
+       time_t  ts;
+       char   *string;
 
+       if (ZEND_NUM_ARGS() == 1) {
+               ts = time(NULL);
+       }
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &format, 
&format_len, &ts) == FAILURE) {
                RETURN_FALSE;
        }
@@ -638,10 +696,6 @@
 
        string = date_format(format, format_len, &return_len, t, localtime, 0 
TSRMLS_CC);
        
-       if (localtime) {
-               timelib_tzinfo_dtor(tzi);
-       }
-       
        timelib_time_dtor(t);
        return string;
 }
@@ -723,7 +777,6 @@
                now = timelib_time_ctor();
                timelib_unixtime2local(now, (timelib_sll) time(NULL), tzi);
        } else {
-               timelib_tzinfo_dtor(tzi);
                RETURN_FALSE;
        }
 
@@ -740,7 +793,6 @@
                timelib_tzinfo_dtor(t->tz_info);
        }
 
-       timelib_tzinfo_dtor(tzi);
        timelib_time_dtor(now); 
        timelib_time_dtor(t);
 
@@ -834,9 +886,6 @@
        ts = timelib_date_to_int(now, &error);
        ts += adjust_seconds;
        timelib_time_dtor(now);
-       if (!gmt) {
-               timelib_tzinfo_dtor(tzi);
-       }
 
        if (error) {
                RETURN_FALSE;
@@ -940,10 +989,6 @@
 #endif
        }
 
-       if (!gmt) {
-               timelib_tzinfo_dtor(tzi);
-       }
-       
        buf = (char *) emalloc(buf_len);
        while ((real_len=strftime(buf, buf_len, format, &ta))==buf_len || 
real_len==0) {
                buf_len *= 2;
@@ -1033,7 +1078,6 @@
                add_next_index_long(return_value, ts->dst);
        }
 
-       timelib_tzinfo_dtor(tzi);
        timelib_time_dtor(ts);
 }
 /* }}} */
@@ -1068,7 +1112,6 @@
        add_assoc_string(return_value, "month", mon_full_names[ts->m - 1], 1);
        add_index_long(return_value, 0, timestamp);
 
-       timelib_tzinfo_dtor(tzi);
        timelib_time_dtor(ts);
 }
 /* }}} */
@@ -1120,8 +1163,6 @@
 {
        php_timezone_obj *intern = (php_timezone_obj *)object;
 
-       timelib_tzinfo_dtor(intern->tz);
-       
        efree(object);
 }
 
@@ -1164,7 +1205,7 @@
        timelib_time   *now;
        timelib_tzinfo *tzi;
        char           *time_str;
-       int             time_str_len = 0;
+       int             time_str_len = 0, free_tzi = 0;;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sO", &time_str, 
&time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
                RETURN_FALSE;
@@ -1179,8 +1220,10 @@
 
                tzobj = (php_timezone_obj *) 
zend_object_store_get_object(timezone_object TSRMLS_CC);
                tzi = timelib_tzinfo_clone(tzobj->tz);
+               free_tzi = 1;
        } else if (dateobj->time->tz_info) {
                tzi = timelib_tzinfo_clone(dateobj->time->tz_info);
+               free_tzi = 1;
        } else {
                tzi = get_timezone_info(TSRMLS_C);
        }
@@ -1194,7 +1237,9 @@
        if (now->tz_info != tzi) {
                timelib_tzinfo_dtor(now->tz_info);
        }
-       timelib_tzinfo_dtor(now->tz_info);
+       if (free_tzi) {
+               timelib_tzinfo_dtor(tzi);
+       }
        timelib_time_dtor(now); 
 }
 
@@ -1394,12 +1439,12 @@
                
                tzid = timelib_timezone_id_from_abbr(tz, -1, 0);
                if (tzid) {
-                       tzi = timelib_parse_tzfile(tzid, DATE_TIMEZONEDB);
+                       tzi = php_date_parse_tzfile(tzid, DATE_TIMEZONEDB 
TSRMLS_CC);
                }
        }
        /* Try finding the tz information as "Timezone Identifier" */
        if (!tzi) {
-               tzi = timelib_parse_tzfile(tz, DATE_TIMEZONEDB);
+               tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB TSRMLS_CC);
        }
        /* If we find it we instantiate the object otherwise, well, we don't 
and return false */
        if (tzi) {
@@ -1545,7 +1590,6 @@
 
        default_tz = get_timezone_info(TSRMLS_C);
        RETVAL_STRING(default_tz->name, 1);
-       timelib_tzinfo_dtor(default_tz);
 }
 /* }}} */
 
http://cvs.php.net/diff.php/php-src/ext/date/php_date.h?r1=1.21&r2=1.22&ty=u
Index: php-src/ext/date/php_date.h
diff -u php-src/ext/date/php_date.h:1.21 php-src/ext/date/php_date.h:1.22
--- php-src/ext/date/php_date.h:1.21    Mon Oct  3 07:15:15 2005
+++ php-src/ext/date/php_date.h Wed Oct  5 14:38:30 2005
@@ -16,12 +16,13 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_date.h,v 1.21 2005/10/03 11:15:15 derick Exp $ */
+/* $Id: php_date.h,v 1.22 2005/10/05 18:38:30 derick Exp $ */
 
 #ifndef PHP_DATE_H
 #define PHP_DATE_H
 
 #include "lib/timelib.h"
+#include "Zend/zend_hash.h"
 
 extern zend_module_entry date_module_entry;
 #define phpext_date_ptr &date_module_entry
@@ -77,8 +78,9 @@
 PHP_MINFO_FUNCTION(date);
 
 ZEND_BEGIN_MODULE_GLOBALS(date)
-       char *default_timezone;
-       char *timezone;
+       char      *default_timezone;
+       char      *timezone;
+       HashTable  tzcache;
 ZEND_END_MODULE_GLOBALS(date)
 
 #ifdef ZTS

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to