From d6f10f69a4ea2218e549cf37bb9c6f67d300a8fe Mon Sep 17 00:00:00 2001 From: Dave Marshall Date: Wed, 18 May 2016 13:01:15 +0100 Subject: [PATCH 1/5] Implement DateTimeImmutable::createFrom --- ext/date/php_date.c | 33 +++++++++++++++++++ ext/date/php_date.h | 1 + .../tests/DateTimeImmutable_createFrom.phpt | 31 +++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 ext/date/tests/DateTimeImmutable_createFrom.phpt diff --git a/ext/date/php_date.c b/ext/date/php_date.c index e86b3bd6513ec..20d4fad8c8484 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -319,6 +319,10 @@ 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_date_method_create_from, 0, 0, 1) + ZEND_ARG_INFO(0, DateTimeInterface) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_timezone_open, 0, 0, 1) ZEND_ARG_INFO(0, timezone) ZEND_END_ARG_INFO() @@ -521,6 +525,7 @@ static const zend_function_entry date_funcs_immutable[] = { 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_ME(DateTimeImmutable, createFrom , arginfo_date_method_create_from, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; @@ -2902,6 +2907,34 @@ PHP_METHOD(DateTimeImmutable, createFromMutable) } /* }}} */ +/* {{{ proto DateTimeImmutable::createFrom(DateTimeInterface object) + Creates new DateTimeImmutable object from an existing DateTimeInterface object. +*/ +PHP_METHOD(DateTimeImmutable, createFrom) +{ + zval *datetime_object = NULL; + php_date_obj *new_obj = NULL; + php_date_obj *old_obj = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &datetime_object, date_ce_interface) == FAILURE) { + return; + } + + php_date_instantiate(date_ce_immutable, return_value); + old_obj = Z_PHPDATE_P(datetime_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 = timelib_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(php_date_obj **dateobj, HashTable *myht) { zval *z_date; diff --git a/ext/date/php_date.h b/ext/date/php_date.h index d652aafaa1dd1..fbc91e4fa6754 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -87,6 +87,7 @@ PHP_METHOD(DateTimeImmutable, setDate); PHP_METHOD(DateTimeImmutable, setISODate); PHP_METHOD(DateTimeImmutable, setTimestamp); PHP_METHOD(DateTimeImmutable, createFromMutable); +PHP_METHOD(DateTimeImmutable, createFrom); PHP_METHOD(DateTimeZone, __construct); PHP_METHOD(DateTimeZone, __wakeup); diff --git a/ext/date/tests/DateTimeImmutable_createFrom.phpt b/ext/date/tests/DateTimeImmutable_createFrom.phpt new file mode 100644 index 0000000000000..cabbe1f39b1d5 --- /dev/null +++ b/ext/date/tests/DateTimeImmutable_createFrom.phpt @@ -0,0 +1,31 @@ +--TEST-- +Tests for DateTimeImmutable::createFrom +--INI-- +date.timezone=Europe/London +--FILE-- + +--EXPECTF-- +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} From 22f93cc18b8e5c69260c4723701fb01f518a7135 Mon Sep 17 00:00:00 2001 From: Dave Marshall Date: Tue, 17 Jul 2018 13:39:33 +0100 Subject: [PATCH 2/5] Refactors to use new helper --- ext/date/php_date.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 20d4fad8c8484..47fd5cfd29ea2 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -2924,14 +2924,7 @@ PHP_METHOD(DateTimeImmutable, createFrom) old_obj = Z_PHPDATE_P(datetime_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 = timelib_strdup(old_obj->time->tz_abbr); - } - if (old_obj->time->tz_info) { - new_obj->time->tz_info = old_obj->time->tz_info; - } + new_obj->time = timelib_time_clone(old_obj->time); } /* }}} */ From 43c635d2bbf1678aba39bfb6ab338decea70536b Mon Sep 17 00:00:00 2001 From: Dave Marshall Date: Tue, 17 Jul 2018 13:50:31 +0100 Subject: [PATCH 3/5] Refactors to match similar methods --- 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 47fd5cfd29ea2..c586db1246fc6 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -2916,9 +2916,9 @@ PHP_METHOD(DateTimeImmutable, createFrom) php_date_obj *new_obj = NULL; php_date_obj *old_obj = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &datetime_object, date_ce_interface) == FAILURE) { - return; - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_interface) + ZEND_PARSE_PARAMETERS_END(); php_date_instantiate(date_ce_immutable, return_value); old_obj = Z_PHPDATE_P(datetime_object); From 3e695a3fadabe40758f7194822bedfe8bdf9f1dc Mon Sep 17 00:00:00 2001 From: Dave Marshall Date: Tue, 17 Jul 2018 13:50:57 +0100 Subject: [PATCH 4/5] Adds DateTime::createFrom --- ext/date/php_date.c | 21 +++++++++++++++++ ext/date/php_date.h | 1 + ext/date/tests/DateTime_createFrom.phpt | 31 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 ext/date/tests/DateTime_createFrom.phpt diff --git a/ext/date/php_date.c b/ext/date/php_date.c index c586db1246fc6..ab08b522cdfa6 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -487,6 +487,7 @@ static const zend_function_entry date_funcs_date[] = { PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC) PHP_ME(DateTime, __set_state, arginfo_date_set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(DateTime, createFromImmutable, arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(DateTime, createFrom, arginfo_date_method_create_from, 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) @@ -2906,6 +2907,26 @@ PHP_METHOD(DateTimeImmutable, createFromMutable) new_obj->time = timelib_time_clone(old_obj->time); } /* }}} */ +/* {{{ proto DateTime::createFrom(DateTimeInterface object) + Creates new DateTime object from an existing DateTimeInterface object. +*/ +PHP_METHOD(DateTime, createFrom) +{ + zval *datetime_object = NULL; + php_date_obj *new_obj = NULL; + php_date_obj *old_obj = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_interface) + ZEND_PARSE_PARAMETERS_END(); + + php_date_instantiate(date_ce_date, return_value); + old_obj = Z_PHPDATE_P(datetime_object); + new_obj = Z_PHPDATE_P(return_value); + + new_obj->time = timelib_time_clone(old_obj->time); +} +/* }}} */ /* {{{ proto DateTimeImmutable::createFrom(DateTimeInterface object) Creates new DateTimeImmutable object from an existing DateTimeInterface object. diff --git a/ext/date/php_date.h b/ext/date/php_date.h index fbc91e4fa6754..e556e93599755 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -54,6 +54,7 @@ PHP_METHOD(DateTime, __construct); PHP_METHOD(DateTime, __wakeup); PHP_METHOD(DateTime, __set_state); PHP_METHOD(DateTime, createFromImmutable); +PHP_METHOD(DateTime, createFrom); PHP_FUNCTION(date_create); PHP_FUNCTION(date_create_immutable); PHP_FUNCTION(date_create_from_format); diff --git a/ext/date/tests/DateTime_createFrom.phpt b/ext/date/tests/DateTime_createFrom.phpt new file mode 100644 index 0000000000000..9c8e3128899ed --- /dev/null +++ b/ext/date/tests/DateTime_createFrom.phpt @@ -0,0 +1,31 @@ +--TEST-- +Tests for DateTime::createFrom +--INI-- +date.timezone=Europe/London +--FILE-- + +--EXPECTF-- +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} From 229866e6fad2b1c5afec1871c073874be4ad498a Mon Sep 17 00:00:00 2001 From: Dave Marshall Date: Tue, 17 Jul 2018 14:04:03 +0100 Subject: [PATCH 5/5] Adds new method to class registration test --- 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 16d7ad3275621..bc5d44a97dcd7 100644 --- a/ext/date/tests/DateTime_verify.phpt +++ b/ext/date/tests/DateTime_verify.phpt @@ -32,7 +32,7 @@ object(ReflectionClass)#%d (1) { string(8) "DateTime" } ..and get names of all its methods -array(19) { +array(20) { [0]=> object(ReflectionMethod)#%d (2) { ["name"]=> @@ -64,102 +64,109 @@ array(19) { [4]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(16) "createFromFormat" + string(10) "createFrom" ["class"]=> string(8) "DateTime" } [5]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(13) "getLastErrors" + string(16) "createFromFormat" ["class"]=> string(8) "DateTime" } [6]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(6) "format" + string(13) "getLastErrors" ["class"]=> string(8) "DateTime" } [7]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(6) "modify" + string(6) "format" ["class"]=> string(8) "DateTime" } [8]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(3) "add" + string(6) "modify" ["class"]=> string(8) "DateTime" } [9]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(3) "sub" + string(3) "add" ["class"]=> string(8) "DateTime" } [10]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(11) "getTimezone" + string(3) "sub" ["class"]=> string(8) "DateTime" } [11]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(11) "setTimezone" + string(11) "getTimezone" ["class"]=> string(8) "DateTime" } [12]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(9) "getOffset" + string(11) "setTimezone" ["class"]=> string(8) "DateTime" } [13]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(7) "setTime" + string(9) "getOffset" ["class"]=> string(8) "DateTime" } [14]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(7) "setDate" + string(7) "setTime" ["class"]=> string(8) "DateTime" } [15]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(10) "setISODate" + string(7) "setDate" ["class"]=> string(8) "DateTime" } [16]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(12) "setTimestamp" + string(10) "setISODate" ["class"]=> string(8) "DateTime" } [17]=> object(ReflectionMethod)#%d (2) { ["name"]=> - string(12) "getTimestamp" + string(12) "setTimestamp" ["class"]=> string(8) "DateTime" } [18]=> + object(ReflectionMethod)#%d (2) { + ["name"]=> + string(12) "getTimestamp" + ["class"]=> + string(8) "DateTime" + } + [19]=> object(ReflectionMethod)#%d (2) { ["name"]=> string(4) "diff"