summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerick Rethans <github@derickrethans.nl>2014-03-02 14:17:16 -0500
committerDerick Rethans <github@derickrethans.nl>2014-03-02 14:18:44 -0500
commit574f230d3c7076d034422c448fd58810b9b8a488 (patch)
tree42c4f3f3b9d3f1265f74b632a61d222f0bd7d3fe
parentb8d75cc464e9f0ea69d4b03b453cc361bd5eae79 (diff)
downloadphp-git-574f230d3c7076d034422c448fd58810b9b8a488.tar.gz
Added DateTimeImmutable::createFromMutable.
-rw-r--r--NEWS4
-rw-r--r--ext/date/php_date.c33
-rw-r--r--ext/date/php_date.h1
-rw-r--r--ext/date/tests/DateTimeImmutable_createFromMutable.phpt26
4 files changed, 64 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index cbd2247cbe..f82f69e1a5 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 f6b12bc42e..5852bb2ca6 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 d7343e02bc..2b3ae4dcc1 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 0000000000..f1d3caa7db
--- /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