From 42660f3d6a7adf1852fc3a77fb17cecef99f9fd1 Mon Sep 17 00:00:00 2001 From: Trevor Suarez Date: Thu, 5 Mar 2015 01:58:09 -0500 Subject: [PATCH 1/5] Adding a new `createFromImmutable` method to the `DateTime` class to mirror the current `DateTime::createFromMutable()` --- ext/date/php_date.c | 39 ++++++++++++++++++++++++++++++++++++--- ext/date/php_date.h | 1 + 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index ffdea3d0909d7..31d3ceb439080 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -183,6 +183,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_date_format, 0, 0, 2) ZEND_ARG_INFO(0, format) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_immutable, 0, 0, 1) + ZEND_ARG_INFO(0, DateTimeImmutable) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_format, 0, 0, 1) ZEND_ARG_INFO(0, format) ZEND_END_ARG_INFO() @@ -463,9 +467,10 @@ static const zend_function_entry date_funcs_interface[] = { }; const zend_function_entry date_funcs_date[] = { - PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) - PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC) - PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) + PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC) + PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(DateTime, createFromImmutable, arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(format, date_format, arginfo_date_method_format, 0) @@ -2832,6 +2837,34 @@ PHP_METHOD(DateTime, __wakeup) } /* }}} */ +/* {{{ proto DateTime::createFromImmutable(DateTimeImmutable object) + Creates new DateTime object from an existing DateTimeImmutable object. +*/ +PHP_METHOD(DateTime, createFromImmutable) +{ + zval *datetimeimmutable_object = NULL; + php_date_obj *new_obj = NULL; + php_date_obj *old_obj = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "O!", &datetimeimmutable_object, date_ce_immutable) == FAILURE) { + return; + } + + php_date_instantiate(date_ce_date, return_value); + old_obj = Z_PHPDATE_P(datetimeimmutable_object); + new_obj = Z_PHPDATE_P(return_value); + + 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; + } +} +/* }}} */ + /* Helper function used to add an associative array of warnings and errors to a zval */ static void zval_from_error_container(zval *z, timelib_error_container *error) /* {{{ */ { diff --git a/ext/date/php_date.h b/ext/date/php_date.h index ee4bac0ef0b03..a423eab2d8d7c 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -50,6 +50,7 @@ PHP_FUNCTION(getdate); PHP_METHOD(DateTime, __construct); PHP_METHOD(DateTime, __wakeup); PHP_METHOD(DateTime, __set_state); +PHP_METHOD(DateTime, createFromImmutable); PHP_FUNCTION(date_create); PHP_FUNCTION(date_create_immutable); PHP_FUNCTION(date_create_from_format); From 46f22acdcdc5b5c6dbc5432efb2d0aed47df1820 Mon Sep 17 00:00:00 2001 From: Trevor Suarez Date: Thu, 5 Mar 2015 02:02:50 -0500 Subject: [PATCH 2/5] Adding a new test for the new method --- .../tests/DateTime_createFromImmutable.phpt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ext/date/tests/DateTime_createFromImmutable.phpt diff --git a/ext/date/tests/DateTime_createFromImmutable.phpt b/ext/date/tests/DateTime_createFromImmutable.phpt new file mode 100644 index 0000000000000..76731b1d9542a --- /dev/null +++ b/ext/date/tests/DateTime_createFromImmutable.phpt @@ -0,0 +1,26 @@ +--TEST-- +Tests for DateTime::createFromImmutable. +--INI-- +date.timezone=America/New_York +--FILE-- + +--EXPECTF-- +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2015-03-05 07:00:16.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "America/New_York" +} + +Warning: DateTime::createFromImmutable() expects parameter 1 to be DateTimeImmutable, object given in %stests%eDateTime_createFromImmutable.php on line %d +NULL From d3863aeaf768611190594986fd32453fa083407a Mon Sep 17 00:00:00 2001 From: Trevor Suarez Date: Thu, 5 Mar 2015 02:56:31 -0500 Subject: [PATCH 3/5] Whoops! String length.... --- ext/date/tests/DateTime_createFromImmutable.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/date/tests/DateTime_createFromImmutable.phpt b/ext/date/tests/DateTime_createFromImmutable.phpt index 76731b1d9542a..bfde94d825c56 100644 --- a/ext/date/tests/DateTime_createFromImmutable.phpt +++ b/ext/date/tests/DateTime_createFromImmutable.phpt @@ -19,7 +19,7 @@ object(DateTime)#%d (3) { ["timezone_type"]=> int(3) ["timezone"]=> - string(13) "America/New_York" + string(16) "America/New_York" } Warning: DateTime::createFromImmutable() expects parameter 1 to be DateTimeImmutable, object given in %stests%eDateTime_createFromImmutable.php on line %d From 1de1b6f9279a3122f1bbfa8641bf0d0dc520fd74 Mon Sep 17 00:00:00 2001 From: Trevor Suarez Date: Thu, 5 Mar 2015 03:44:58 -0500 Subject: [PATCH 4/5] Updating the `DateTime_verify` test to include the new method in the reflected method list --- ext/date/tests/DateTime_verify.phpt | 37 +++++++++++++++++------------ 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/ext/date/tests/DateTime_verify.phpt b/ext/date/tests/DateTime_verify.phpt index 33768969dc1bd..604527494087b 100644 --- a/ext/date/tests/DateTime_verify.phpt +++ b/ext/date/tests/DateTime_verify.phpt @@ -27,7 +27,7 @@ object(ReflectionClass)#%d (1) { string(8) "DateTime" } ..and get names of all its methods -array(18) { +array(19) { [0]=> object(ReflectionMethod)#%d (2) { ["name"]=> @@ -52,102 +52,109 @@ array(18) { [3]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(16) "createFromFormat" + string(19) "createFromImmutable" ["class"]=> string(8) "DateTime" } [4]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(13) "getLastErrors" + string(16) "createFromFormat" ["class"]=> string(8) "DateTime" } [5]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(6) "format" + string(13) "getLastErrors" ["class"]=> string(8) "DateTime" } [6]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(6) "modify" + string(6) "format" ["class"]=> string(8) "DateTime" } [7]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(3) "add" + string(6) "modify" ["class"]=> string(8) "DateTime" } [8]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(3) "sub" + string(3) "add" ["class"]=> string(8) "DateTime" } [9]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(11) "getTimezone" + string(3) "sub" ["class"]=> string(8) "DateTime" } [10]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(11) "setTimezone" + string(11) "getTimezone" ["class"]=> string(8) "DateTime" } [11]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(9) "getOffset" + string(11) "setTimezone" ["class"]=> string(8) "DateTime" } [12]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(7) "setTime" + string(9) "getOffset" ["class"]=> string(8) "DateTime" } [13]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(7) "setDate" + string(7) "setTime" ["class"]=> string(8) "DateTime" } [14]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(10) "setISODate" + string(7) "setDate" ["class"]=> string(8) "DateTime" } [15]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(12) "setTimestamp" + string(10) "setISODate" ["class"]=> string(8) "DateTime" } [16]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(12) "getTimestamp" + string(12) "setTimestamp" ["class"]=> string(8) "DateTime" } [17]=> + object(ReflectionMethod)#%d (2) { + ["name"]=> + string(12) "getTimestamp" + ["class"]=> + string(8) "DateTime" + } + [18]=> object(ReflectionMethod)#%d (2) { ["name"]=> string(4) "diff" From 98c6567e718859fcbd3b0db49920fac9f6a271ef Mon Sep 17 00:00:00 2001 From: Trevor Suarez Date: Wed, 11 Mar 2015 09:29:08 -0400 Subject: [PATCH 5/5] Putting whitespace back to the way it was. --- ext/date/php_date.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 31d3ceb439080..bea7003fe9bf6 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -467,9 +467,9 @@ static const zend_function_entry date_funcs_interface[] = { }; const zend_function_entry date_funcs_date[] = { - PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) - PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC) - PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) + PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC) + PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(DateTime, createFromImmutable, arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)