diff options
author | Michael Wallner <mike@php.net> | 2006-08-14 20:03:11 +0000 |
---|---|---|
committer | Michael Wallner <mike@php.net> | 2006-08-14 20:03:11 +0000 |
commit | 8657927354c6c03320f7733b5859cd5272712b33 (patch) | |
tree | c300cb23bbed577b218a7c274b97c8fcad2bb331 | |
parent | 8eaa9eae52b7d5176e4bfecec5666d44a2aa8128 (diff) | |
download | php-git-8657927354c6c03320f7733b5859cd5272712b33.tar.gz |
MFH: add cloning support to DateTime classes
-rw-r--r-- | ext/date/php_date.c | 61 | ||||
-rw-r--r-- | ext/date/tests/oo_002.phpt | 28 |
2 files changed, 85 insertions, 4 deletions
diff --git a/ext/date/php_date.c b/ext/date/php_date.c index debda7b0cf..ed46a3be60 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -296,6 +296,8 @@ 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); 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 @@ static void date_register_classes(TSRMLS_D) 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 @@ static void date_register_classes(TSRMLS_D) 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 @@ static zend_object_value date_object_new_date(zend_class_entry *class_type TSRML 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 @@ static zend_object_value date_object_new_date(zend_class_entry *class_type TSRML 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 @@ static zend_object_value date_object_new_timezone(zend_class_entry *class_type T 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 @@ static zend_object_value date_object_new_timezone(zend_class_entry *class_type T 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; diff --git a/ext/date/tests/oo_002.phpt b/ext/date/tests/oo_002.phpt new file mode 100644 index 0000000000..79cec9eb5f --- /dev/null +++ b/ext/date/tests/oo_002.phpt @@ -0,0 +1,28 @@ +--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" |