mike            Tue Aug  1 13:28:28 2006 UTC

  Added files:                 (Branch: PHP_5_2)
    /php-src/ext/date/tests     oo_001.phpt 

  Modified files:              
    /php-src/ext/date   php_date.c php_date.h 
  Log:
  MFH:
   - OO fixes:
    . no is_ref for objects in PHP5+
    . initialize objects when created with the `new` operator
    . check for proper initialization in child classes
   - fix typo: transistion -> transition
   - add test
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/date/php_date.c?r1=1.43.2.45.2.20&r2=1.43.2.45.2.21&diff_format=u
Index: php-src/ext/date/php_date.c
diff -u php-src/ext/date/php_date.c:1.43.2.45.2.20 
php-src/ext/date/php_date.c:1.43.2.45.2.21
--- php-src/ext/date/php_date.c:1.43.2.45.2.20  Fri Jul 28 16:32:31 2006
+++ php-src/ext/date/php_date.c Tue Aug  1 13:28:28 2006
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_date.c,v 1.43.2.45.2.20 2006/07/28 16:32:31 bjori Exp $ */
+/* $Id: php_date.c,v 1.43.2.45.2.21 2006/08/01 13:28:28 mike Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -182,7 +182,7 @@
        PHP_FE(timezone_name_get, NULL)
        PHP_FE(timezone_name_from_abbr, NULL)
        PHP_FE(timezone_offset_get, NULL)
-       PHP_FE(timezone_transistions_get, NULL)
+       PHP_FE(timezone_transitions_get, NULL)
        PHP_FE(timezone_identifiers_list, NULL)
        PHP_FE(timezone_abbreviations_list, NULL)
 
@@ -198,6 +198,7 @@
 };
 
 zend_function_entry date_funcs_date[] = {
+       PHP_ME(DateTime, __construct, NULL, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
        ZEND_NAMED_FE(format, ZEND_FN(date_format), NULL)
        ZEND_NAMED_FE(modify, ZEND_FN(date_modify), NULL)
        ZEND_NAMED_FE(getTimezone, ZEND_FN(date_timezone_get), NULL)
@@ -210,9 +211,10 @@
 };
 
 zend_function_entry date_funcs_timezone[] = {
+       PHP_ME(DateTimeZone, __construct, NULL, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
        ZEND_NAMED_FE(getName, ZEND_FN(timezone_name_get), NULL)
        ZEND_NAMED_FE(getOffset, ZEND_FN(timezone_offset_get), NULL)
-       ZEND_NAMED_FE(getTransistions, ZEND_FN(timezone_transistions_get), NULL)
+       ZEND_NAMED_FE(getTransitions, ZEND_FN(timezone_transitions_get), NULL)
        ZEND_ME_MAPPING(listAbbreviations, timezone_abbreviations_list, NULL, 
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        ZEND_ME_MAPPING(listIdentifiers, timezone_identifiers_list, NULL, 
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        {NULL, NULL, NULL}
@@ -284,6 +286,12 @@
        }       \
        obj = (php_date_obj *) zend_object_store_get_object(object TSRMLS_CC);  
\
 
+#define DATE_CHECK_INITIALIZED(member, class_name) \
+       if (!(member)) { \
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "The " #class_name 
" object has not been correctly initialized by its constructor"); \
+               RETURN_FALSE; \
+       }
+
 static void date_object_free_storage_date(void *object TSRMLS_DC);
 static void date_object_free_storage_timezone(void *object TSRMLS_DC);
 static zend_object_value date_object_new_date(zend_class_entry *class_type 
TSRMLS_DC);
@@ -1528,25 +1536,16 @@
        Z_TYPE_P(object) = IS_OBJECT;
        object_init_ex(object, pce);
        object->refcount = 1;
-       object->is_ref = 1;
+       object->is_ref = 0;
        return object;
 }
 
-PHP_FUNCTION(date_create)
+static void date_initialize(php_date_obj *dateobj, /*const*/ char *time_str, 
int time_str_len, zval *timezone_object TSRMLS_DC)
 {
-       php_date_obj   *dateobj;
-       zval           *timezone_object = NULL;
        timelib_time   *now;
        timelib_tzinfo *tzi;
-       char           *time_str;
-       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;
-       }
-
-       date_instantiate(date_ce_date, return_value TSRMLS_CC);
-       dateobj = (php_date_obj *) zend_object_store_get_object(return_value 
TSRMLS_CC);
+       int free_tzi = 0;
+       
        dateobj->time = timelib_strtotime(time_str_len ? time_str : "now", 
time_str_len ? time_str_len : sizeof("now") -1, NULL, DATE_TIMEZONEDB);
 
        if (timezone_object) {
@@ -1579,6 +1578,32 @@
        timelib_time_dtor(now); 
 }
 
+PHP_FUNCTION(date_create)
+{
+       zval           *timezone_object = NULL;
+       char           *time_str = NULL;
+       int             time_str_len = 0;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sO", &time_str, 
&time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       date_instantiate(date_ce_date, return_value TSRMLS_CC);
+       date_initialize(zend_object_store_get_object(return_value TSRMLS_CC), 
time_str, time_str_len, timezone_object TSRMLS_CC);
+}
+
+PHP_METHOD(DateTime, __construct)
+{
+       zval *timezone_object = NULL;
+       char *time_str = NULL;
+       int time_str_len = 0;
+       
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sO", 
&time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
+               /* TODO: throw Exception on unparseable dates */
+               date_initialize(zend_object_store_get_object(getThis() 
TSRMLS_CC), time_str, time_str_len, timezone_object TSRMLS_CC);
+       }
+}
+
 PHP_FUNCTION(date_parse)
 {
        char                           *date;
@@ -1686,6 +1711,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        RETURN_STRING(date_format(format, format_len, dateobj->time, 
dateobj->time->is_localtime), 0);
 }
 
@@ -1701,6 +1727,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
 
        tmp_time = timelib_strtotime(modify, modify_len, NULL, DATE_TIMEZONEDB);
        dateobj->time->relative.y = tmp_time->relative.y;
@@ -1729,6 +1756,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        if (dateobj->time->is_localtime && dateobj->time->tz_info) {
                date_instantiate(date_ce_timezone, return_value TSRMLS_CC);
                tzobj = (php_timezone_obj *) 
zend_object_store_get_object(return_value TSRMLS_CC);
@@ -1749,6 +1777,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        tzobj = (php_timezone_obj *) 
zend_object_store_get_object(timezone_object TSRMLS_CC);
        if (dateobj->time->tz_info) {
                timelib_tzinfo_dtor(dateobj->time->tz_info);
@@ -1767,6 +1796,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        if (dateobj->time->is_localtime && dateobj->time->tz_info) {
                offset = timelib_get_time_zone_info(dateobj->time->sse, 
dateobj->time->tz_info);
                RETVAL_LONG(offset->offset);
@@ -1787,6 +1817,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        dateobj->time->h = h;
        dateobj->time->i = i;
        dateobj->time->s = s;
@@ -1803,6 +1834,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        dateobj->time->y = y;
        dateobj->time->m = m;
        dateobj->time->d = d;
@@ -1819,6 +1851,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        dateobj->time->y = y;
        dateobj->time->m = 1;
        dateobj->time->d = 1;
@@ -1829,9 +1862,28 @@
 }
 
 
+static int timezone_initialize(timelib_tzinfo **tzi, /*const*/ char *tz 
TSRMLS_DC)
+{
+       char *tzid;
+       
+       *tzi = NULL;
+       
+       if ((tzid = timelib_timezone_id_from_abbr(tz, -1, 0))) {
+               *tzi = php_date_parse_tzfile(tzid, DATE_TIMEZONEDB TSRMLS_CC);
+       } else {
+               *tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB TSRMLS_CC);
+       }
+       
+       if (*tzi) {
+               return SUCCESS;
+       } else {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown or bad 
timezone (%s)", tz);
+               return FAILURE;
+       }
+}
+
 PHP_FUNCTION(timezone_open)
 {
-       php_timezone_obj *tzobj;
        char *tz;
        int   tz_len;
        timelib_tzinfo *tzi = NULL;
@@ -1839,27 +1891,25 @@
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &tz, &tz_len) 
== FAILURE) {
                RETURN_FALSE;
        }
-       /* Try finding the tz information as "Timezone Abbreviation" */
-       if (!tzi) {
-               char *tzid;
-               
-               tzid = timelib_timezone_id_from_abbr(tz, -1, 0);
-               if (tzid) {
-                       tzi = php_date_parse_tzfile(tzid, DATE_TIMEZONEDB 
TSRMLS_CC);
-               }
-       }
-       /* Try finding the tz information as "Timezone Identifier" */
-       if (!tzi) {
-               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) {
-               date_instantiate(date_ce_timezone, return_value TSRMLS_CC);
-               tzobj = (php_timezone_obj *) 
zend_object_store_get_object(return_value TSRMLS_CC);
-               tzobj->tz = tzi;
-       } else {
+       if (SUCCESS != timezone_initialize(&tzi, tz TSRMLS_CC)) {
                RETURN_FALSE;
        }
+       ((php_timezone_obj *) 
zend_object_store_get_object(date_instantiate(date_ce_timezone, return_value 
TSRMLS_CC) TSRMLS_CC))->tz = tzi;
+}
+
+PHP_METHOD(DateTimeZone, __construct)
+{
+       char *tz;
+       int tz_len;
+       timelib_tzinfo *tzi = NULL;
+       
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", 
&tz, &tz_len)) {
+               php_set_error_handling(EH_THROW, NULL TSRMLS_CC);
+               if (SUCCESS == timezone_initialize(&tzi, tz TSRMLS_CC)) {
+                       ((php_timezone_obj *) 
zend_object_store_get_object(getThis() TSRMLS_CC))->tz = tzi;
+               }
+               php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
+       }
 }
 
 PHP_FUNCTION(timezone_name_get)
@@ -1871,6 +1921,7 @@
                RETURN_FALSE;
        }
        tzobj = (php_timezone_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(tzobj->tz, DateTimeZone);
 
        RETURN_STRING(tzobj->tz->name, 1);
 }
@@ -1906,14 +1957,16 @@
                RETURN_FALSE;
        }
        tzobj = (php_timezone_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(tzobj->tz, DateTimeZone);
        dateobj = (php_date_obj *) zend_object_store_get_object(dateobject 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
 
        offset = timelib_get_time_zone_info(dateobj->time->sse, tzobj->tz);
        RETVAL_LONG(offset->offset);
        timelib_time_offset_dtor(offset);
 }
 
-PHP_FUNCTION(timezone_transistions_get)
+PHP_FUNCTION(timezone_transitions_get)
 {
        zval                *object, *element;
        php_timezone_obj    *tzobj;
@@ -1923,6 +1976,7 @@
                RETURN_FALSE;
        }
        tzobj = (php_timezone_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(tzobj->tz, DateTimeZone);
 
        array_init(return_value);
        for (i = 0; i < tzobj->tz->timecnt; ++i) {
http://cvs.php.net/viewvc.cgi/php-src/ext/date/php_date.h?r1=1.17.2.11.2.1&r2=1.17.2.11.2.2&diff_format=u
Index: php-src/ext/date/php_date.h
diff -u php-src/ext/date/php_date.h:1.17.2.11.2.1 
php-src/ext/date/php_date.h:1.17.2.11.2.2
--- php-src/ext/date/php_date.h:1.17.2.11.2.1   Tue Jul 18 12:38:05 2006
+++ php-src/ext/date/php_date.h Tue Aug  1 13:28:28 2006
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_date.h,v 1.17.2.11.2.1 2006/07/18 12:38:05 derick Exp $ */
+/* $Id: php_date.h,v 1.17.2.11.2.2 2006/08/01 13:28:28 mike Exp $ */
 
 #ifndef PHP_DATE_H
 #define PHP_DATE_H
@@ -47,6 +47,7 @@
 PHP_FUNCTION(getdate);
 
 /* Advanced Interface */
+PHP_METHOD(DateTime, __construct);
 PHP_FUNCTION(date_create);
 PHP_FUNCTION(date_parse);
 PHP_FUNCTION(date_format);
@@ -59,11 +60,12 @@
 PHP_FUNCTION(date_date_set);
 PHP_FUNCTION(date_isodate_set);
 
+PHP_METHOD(DateTimeZone, __construct);
 PHP_FUNCTION(timezone_open);
 PHP_FUNCTION(timezone_name_get);
 PHP_FUNCTION(timezone_name_from_abbr);
 PHP_FUNCTION(timezone_offset_get);
-PHP_FUNCTION(timezone_transistions_get);
+PHP_FUNCTION(timezone_transitions_get);
 PHP_FUNCTION(timezone_identifiers_list);
 PHP_FUNCTION(timezone_abbreviations_list);
 

http://cvs.php.net/viewvc.cgi/php-src/ext/date/tests/oo_001.phpt?view=markup&rev=1.1
Index: php-src/ext/date/tests/oo_001.phpt
+++ php-src/ext/date/tests/oo_001.phpt
--TEST--
date OO interface
--FILE--
<?php
class _d extends DateTime {
        function __construct() {
        }
}
class _t extends DateTimeZone {
        function __construct() {
        }
}

$d = new DateTime;
var_dump($d->format("Y-m-d H:i:s"));

$d = new _d;
var_dump($d->format("Y-m-d H:i:s"));


$t = new DateTimeZone("UTC");
var_dump($t->getName());

$t = new _t;
var_dump($t->getName());

try {
        new DateTimeZone("GottaFindThisOne");
} catch (Exception $e) {
        echo $e->getMessage(),"\n";
}

echo "DONE\n";
?>
--EXPECTF--
string(19) "%d-%d-%d %d:%d:%d"

Warning: DateTime::format(): The DateTime object has not been correctly 
initialized by its constructor in %soo_001.php on line %d
bool(false)
string(3) "UTC"

Warning: DateTimeZone::getName(): The DateTimeZone object has not been 
correctly initialized by its constructor in %soo_001.php on line %d
bool(false)
DateTimeZone::__construct(): Unknown or bad timezone (GottaFindThisOne)
DONE

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

Reply via email to