mike            Tue Aug  1 13:27:41 2006 UTC

  Added files:                 
    /php-src/ext/date/tests     oo_001.phpt 

  Modified files:              
    /php-src/ext/date   php_date.c php_date.h 
  Log:
  - 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.107&r2=1.108&diff_format=u
Index: php-src/ext/date/php_date.c
diff -u php-src/ext/date/php_date.c:1.107 php-src/ext/date/php_date.c:1.108
--- php-src/ext/date/php_date.c:1.107   Thu Jul 20 15:48:36 2006
+++ php-src/ext/date/php_date.c Tue Aug  1 13:27:41 2006
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_date.c,v 1.107 2006/07/20 15:48:36 derick Exp $ */
+/* $Id: php_date.c,v 1.108 2006/08/01 13:27:41 mike Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -184,7 +184,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)
 
@@ -201,6 +201,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)
@@ -213,9 +214,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_FENTRY(listAbbreviations, zif_timezone_abbreviations_list, NULL, 
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        ZEND_FENTRY(listIdentifiers, zif_timezone_identifiers_list, NULL, 
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        {NULL, NULL, NULL}
@@ -287,6 +289,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);
@@ -1656,25 +1664,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) {
@@ -1707,6 +1706,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;
@@ -1814,6 +1839,7 @@
                RETURN_FALSE;
        }
        dateobj = (php_date_obj *) zend_object_store_get_object(object 
TSRMLS_CC);
+       DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
        str = date_format(format, format_len, &length, dateobj->time, 
dateobj->time->is_localtime, 0 TSRMLS_CC);
        if (UG(unicode)) {
                RETURN_UNICODEL((UChar*) str, length, 0);
@@ -1854,6 +1880,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;
@@ -1882,6 +1909,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);
@@ -1902,6 +1930,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);
@@ -1920,6 +1949,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);
@@ -1940,6 +1970,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;
@@ -1956,6 +1987,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;
@@ -1972,6 +2004,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;
@@ -1982,9 +2015,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;
@@ -1992,27 +2044,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)
@@ -2024,6 +2074,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);
 }
@@ -2059,14 +2110,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;
@@ -2076,6 +2129,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.30&r2=1.31&diff_format=u
Index: php-src/ext/date/php_date.h
diff -u php-src/ext/date/php_date.h:1.30 php-src/ext/date/php_date.h:1.31
--- php-src/ext/date/php_date.h:1.30    Tue Jul 18 12:37:49 2006
+++ php-src/ext/date/php_date.h Tue Aug  1 13:27:41 2006
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_date.h,v 1.30 2006/07/18 12:37:49 derick Exp $ */
+/* $Id: php_date.h,v 1.31 2006/08/01 13:27:41 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);
@@ -60,11 +61,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