cvs: php-src(PHP_5_2) /ext/date php_date.c /ext/date/tests oo_002.phpt

From: Date: Mon, 14 Aug 2006 20:03:11 +0000
Subject: cvs: php-src(PHP_5_2) /ext/date php_date.c /ext/date/tests oo_002.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-39977@lists.php.net to get a copy of this message
mike Mon Aug 14 20:03:11 2006 UTC Added files: (Branch: PHP_5_2) /php-src/ext/date/tests oo_002.phpt Modified files: /php-src/ext/date php_date.c Log: MFH: add cloning support to DateTime classes http://cvs.php.net/viewvc.cgi/php-src/ext/date/php_date.c?r1=1.43.2.45.2.23&r2=1.43.2.45.2.24&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.23 php-src/ext/date/php_date.c:1.43.2.45.2.24 --- php-src/ext/date/php_date.c:1.43.2.45.2.23 Tue Aug 1 16:45:40 2006 +++ php-src/ext/date/php_date.c Mon Aug 14 20:03:11 2006 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_date.c,v 1.43.2.45.2.23 2006/08/01 16:45:40 mike Exp $ */ +/* $Id: php_date.c,v 1.43.2.45.2.24 2006/08/14 20:03:11 mike Exp $ */ #include "php.h" #include "php_streams.h" @@ -296,6 +296,8 @@ 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); static zend_object_value date_object_new_timezone(zend_class_entry *class_type TSRMLS_DC); +static zend_object_value date_object_clone_date(zval *this_ptr TSRMLS_DC); +static zend_object_value date_object_clone_timezone(zval *this_ptr TSRMLS_DC); /* {{{ Module struct */ zend_module_entry date_module_entry = { @@ -1442,7 +1444,7 @@ ce_date.create_object = date_object_new_date; date_ce_date = zend_register_internal_class_ex(&ce_date, NULL, NULL TSRMLS_CC); memcpy(&date_object_handlers_date, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); - date_object_handlers_date.clone_obj = NULL; + date_object_handlers_date.clone_obj = date_object_clone_date; #define REGISTER_DATE_CLASS_CONST_STRING(const_name, value) \ zend_declare_class_constant_stringl(date_ce_date, const_name, sizeof(const_name)-1, value, sizeof(value)-1 TSRMLS_CC); @@ -1464,10 +1466,10 @@ ce_timezone.create_object = date_object_new_timezone; date_ce_timezone = zend_register_internal_class_ex(&ce_timezone, NULL, NULL TSRMLS_CC); memcpy(&date_object_handlers_timezone, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); - date_object_handlers_timezone.clone_obj = NULL; + date_object_handlers_timezone.clone_obj = date_object_clone_timezone; } -static zend_object_value date_object_new_date(zend_class_entry *class_type TSRMLS_DC) +inline zend_object_value date_object_new_date_ex(zend_class_entry *class_type, php_date_obj **ptr TSRMLS_DC) { php_date_obj *intern; zend_object_value retval; @@ -1475,6 +1477,9 @@ intern = emalloc(sizeof(php_date_obj)); memset(intern, 0, sizeof(php_date_obj)); + if (ptr) { + *ptr = intern; + } zend_object_std_init(&intern->std, class_type TSRMLS_CC); zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); @@ -1485,7 +1490,34 @@ return retval; } -static zend_object_value date_object_new_timezone(zend_class_entry *class_type TSRMLS_DC) +static zend_object_value date_object_new_date(zend_class_entry *class_type TSRMLS_DC) +{ + return date_object_new_date_ex(class_type, NULL TSRMLS_CC); +} + +static zend_object_value date_object_clone_date(zval *this_ptr TSRMLS_DC) +{ + zend_object *old_zo = zend_objects_get_address(this_ptr TSRMLS_CC); + php_date_obj *old_obj = (php_date_obj *) zend_object_store_get_object(this_ptr TSRMLS_CC); + php_date_obj *new_obj = NULL; + zend_object_value new_ov = date_object_new_date_ex(old_zo->ce, &new_obj TSRMLS_CC); + + zend_objects_clone_members(&new_obj->std, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC); + + /* this should probably moved to a new timelib_time *timelime_time_clone(timelib_time *) */ + new_obj->time = timelib_time_ctor(); + *new_obj->time = *old_obj->time; + if (old_obj->time->tz_abbr) { + new_obj->time->tz_abbr = strdup(old_obj->time->tz_abbr); + } + if (old_obj->time->tz_info) { + new_obj->time->tz_info = timelib_tzinfo_clone(old_obj->time->tz_info); + } + + return new_ov; +} + +inline zend_object_value date_object_new_timezone_ex(zend_class_entry *class_type, php_timezone_obj **ptr TSRMLS_DC) { php_timezone_obj *intern; zend_object_value retval; @@ -1493,6 +1525,9 @@ intern = emalloc(sizeof(php_timezone_obj)); memset(intern, 0, sizeof(php_timezone_obj)); + if (ptr) { + *ptr = intern; + } zend_object_std_init(&intern->std, class_type TSRMLS_CC); zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); @@ -1503,6 +1538,24 @@ return retval; } +static zend_object_value date_object_new_timezone(zend_class_entry *class_type TSRMLS_DC) +{ + return date_object_new_timezone_ex(class_type, NULL TSRMLS_CC); +} + +static zend_object_value date_object_clone_timezone(zval *this_ptr TSRMLS_DC) +{ + zend_object *old_zo = zend_objects_get_address(this_ptr TSRMLS_CC); + php_timezone_obj *old_obj = (php_timezone_obj *) zend_object_store_get_object(this_ptr TSRMLS_CC); + php_timezone_obj *new_obj = NULL; + zend_object_value new_ov = date_object_new_timezone_ex(old_zo->ce, &new_obj TSRMLS_CC); + + zend_objects_clone_members(&new_obj->std, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC); + new_obj->tz = timelib_tzinfo_clone(old_obj->tz); + + return new_ov; +} + static void date_object_free_storage_date(void *object TSRMLS_DC) { php_date_obj *intern = (php_date_obj *)object; http://cvs.php.net/viewvc.cgi/php-src/ext/date/tests/oo_002.phpt?view=markup&rev=1.1 Index: php-src/ext/date/tests/oo_002.phpt +++ php-src/ext/date/tests/oo_002.phpt --TEST-- date OO cloning --INI-- date.timezone=Europe/Berlin --FILE-- <?php class _d extends DateTime {} class _t extends DateTimeZone {} $d = new _d("1pm Aug 1 GMT"); var_dump($d->format(DateTime::RFC822)); $c = clone $d; var_dump($c->format(DateTime::RFC822)); $d->modify("1 hour after"); $c->modify("1 second ago"); var_dump($d->format(DateTime::RFC822)); var_dump($c->format(DateTime::RFC822)); $t = new DateTimeZone("Asia/Tokyo"); var_dump($t->getName()); $c = clone $t; var_dump($c->getName()); ?> --EXPECT-- string(29) "Tue, 01 Aug 06 13:00:00 +0000" string(29) "Tue, 01 Aug 06 13:00:00 +0000" string(29) "Tue, 01 Aug 06 14:00:00 +0000" string(29) "Tue, 01 Aug 06 12:59:59 +0000" string(10) "Asia/Tokyo" string(10) "Asia/Tokyo"

« previous php.cvs (#39977) next »