com php-src: Added DateTimeImmutable::createFromMutable.: NEWS ext/date/php_date.c ext/date/php_date.h ext/date/te
sts/DateTimeImmutable_createFromMutable.phpt
| From: | Derick Rethans | Date: | Sun, 02 Mar 2014 19:18:44 +0000 |
| Subject: | com php-src: Added DateTimeImmutable::createFromMutable.: NEWS ext/date/php_date.c ext/date/php_date.h ext/date/te sts/DateTimeImmutable_createFromMutable.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-76309@lists.php.net to get a copy of this message | ||
Commit: 574f230d3c7076d034422c448fd58810b9b8a488
Author: Derick Rethans <github@derickrethans.nl> Sun, 2 Mar 2014 14:17:16 -0500
Parents: b8d75cc464e9f0ea69d4b03b453cc361bd5eae79
Branches: PHP-5.6 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=574f230d3c7076d034422c448fd58810b9b8a488
Log:
Added DateTimeImmutable::createFromMutable.
Changed paths:
M NEWS
M ext/date/php_date.c
M ext/date/php_date.h
A ext/date/tests/DateTimeImmutable_createFromMutable.phpt
Diff:
diff --git a/NEWS b/NEWS
index cbd2247..f82f69e 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,10 @@ PHP NEWS
. Fixed bug #66109 (Can't reset CURLOPT_CUSTOMREQUEST to default behaviour)
(Tjerk)
+- Date:
+ . Added DateTimeImmutable::createFromMutable to create a DateTimeImmutable
+ object from an existing DateTime (mutable) object (Derick)
+
- Mail:
. Fixed bug #66535 (Don't add newline after X-PHP-Originating-Script) (Tjerk)
diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index f6b12bc..5852bb2 100644
--- a/ext/date/php_date.c
+++ b/ext/date/php_date.c
@@ -299,6 +299,10 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_date_method_timestamp_get, 0)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_mutable, 0, 0, 1)
+ ZEND_ARG_INFO(0, DateTime)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_open, 0, 0, 1)
ZEND_ARG_INFO(0, timezone)
ZEND_END_ARG_INFO()
@@ -495,6 +499,7 @@ const zend_function_entry date_funcs_immutable[] = {
PHP_ME(DateTimeImmutable, setDate, arginfo_date_method_date_set, 0)
PHP_ME(DateTimeImmutable, setISODate, arginfo_date_method_isodate_set, 0)
PHP_ME(DateTimeImmutable, setTimestamp, arginfo_date_method_timestamp_set, 0)
+ PHP_ME(DateTimeImmutable, createFromMutable, arginfo_date_method_create_from_mutable,
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END
};
@@ -2748,6 +2753,34 @@ PHP_METHOD(DateTimeImmutable, __construct)
}
/* }}} */
+/* {{{ proto DateTimeImmutable::createFromMutable(DateTimeZone object)
+ Creates new DateTimeImmutable object from an existing mutable DateTime object.
+*/
+PHP_METHOD(DateTimeImmutable, createFromMutable)
+{
+ zval *datetime_object = NULL;
+ php_date_obj *new_obj = NULL;
+ php_date_obj *old_obj = NULL;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O!", &datetime_object,
date_ce_date) == FAILURE) {
+ return;
+ }
+
+ php_date_instantiate(date_ce_immutable, return_value TSRMLS_CC);
+ old_obj = (php_date_obj *) zend_object_store_get_object(datetime_object TSRMLS_CC);
+ new_obj = (php_date_obj *) zend_object_store_get_object(return_value TSRMLS_CC);
+
+ 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 = old_obj->time->tz_info;
+ }
+}
+/* }}} */
+
static int php_date_initialize_from_hash(zval **return_value, php_date_obj **dateobj, HashTable
*myht TSRMLS_DC)
{
zval **z_date = NULL;
diff --git a/ext/date/php_date.h b/ext/date/php_date.h
index d7343e0..2b3ae4d 100644
--- a/ext/date/php_date.h
+++ b/ext/date/php_date.h
@@ -82,6 +82,7 @@ PHP_METHOD(DateTimeImmutable, setTime);
PHP_METHOD(DateTimeImmutable, setDate);
PHP_METHOD(DateTimeImmutable, setISODate);
PHP_METHOD(DateTimeImmutable, setTimestamp);
+PHP_METHOD(DateTimeImmutable, createFromMutable);
PHP_METHOD(DateTimeZone, __construct);
PHP_METHOD(DateTimeZone, __wakeup);
diff --git a/ext/date/tests/DateTimeImmutable_createFromMutable.phpt
b/ext/date/tests/DateTimeImmutable_createFromMutable.phpt
new file mode 100644
index 0000000..f1d3caa
--- /dev/null
+++ b/ext/date/tests/DateTimeImmutable_createFromMutable.phpt
@@ -0,0 +1,26 @@
+--TEST--
+Tests for DateTimeImmutable::createFromMutable.
+--INI--
+date.timezone=Europe/London
+--FILE--
+<?php
+$current = "2014-03-02 16:24:08";
+
+$i = DateTimeImmutable::createFromMutable( date_create( $current ) );
+var_dump( $i );
+
+$i = DateTimeImmutable::createFromMutable( date_create_immutable( $current ) );
+var_dump( $i );
+?>
+--EXPECTF--
+object(DateTimeImmutable)#%d (3) {
+ ["date"]=>
+ string(19) "2014-03-02 16:24:08"
+ ["timezone_type"]=>
+ int(3)
+ ["timezone"]=>
+ string(13) "Europe/London"
+}
+
+Warning: DateTimeImmutable::createFromMutable() expects parameter 1 to be DateTime, object given in
%stests/DateTimeImmutable_createFromMutable.php on line %d
+NULL