Skip to content

Commit 42660f3

Browse files
committed
Adding a new createFromImmutable method to the
`DateTime` class to mirror the current `DateTime::createFromMutable()`
1 parent fe29b81 commit 42660f3

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

ext/date/php_date.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_date_format, 0, 0, 2)
183183
ZEND_ARG_INFO(0, format)
184184
ZEND_END_ARG_INFO()
185185

186+
ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_immutable, 0, 0, 1)
187+
ZEND_ARG_INFO(0, DateTimeImmutable)
188+
ZEND_END_ARG_INFO()
189+
186190
ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_format, 0, 0, 1)
187191
ZEND_ARG_INFO(0, format)
188192
ZEND_END_ARG_INFO()
@@ -463,9 +467,10 @@ static const zend_function_entry date_funcs_interface[] = {
463467
};
464468

465469
const zend_function_entry date_funcs_date[] = {
466-
PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
467-
PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC)
468-
PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
470+
PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
471+
PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC)
472+
PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
473+
PHP_ME(DateTime, createFromImmutable, arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
469474
PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
470475
PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
471476
PHP_ME_MAPPING(format, date_format, arginfo_date_method_format, 0)
@@ -2832,6 +2837,34 @@ PHP_METHOD(DateTime, __wakeup)
28322837
}
28332838
/* }}} */
28342839

2840+
/* {{{ proto DateTime::createFromImmutable(DateTimeImmutable object)
2841+
Creates new DateTime object from an existing DateTimeImmutable object.
2842+
*/
2843+
PHP_METHOD(DateTime, createFromImmutable)
2844+
{
2845+
zval *datetimeimmutable_object = NULL;
2846+
php_date_obj *new_obj = NULL;
2847+
php_date_obj *old_obj = NULL;
2848+
2849+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O!", &datetimeimmutable_object, date_ce_immutable) == FAILURE) {
2850+
return;
2851+
}
2852+
2853+
php_date_instantiate(date_ce_date, return_value);
2854+
old_obj = Z_PHPDATE_P(datetimeimmutable_object);
2855+
new_obj = Z_PHPDATE_P(return_value);
2856+
2857+
new_obj->time = timelib_time_ctor();
2858+
*new_obj->time = *old_obj->time;
2859+
if (old_obj->time->tz_abbr) {
2860+
new_obj->time->tz_abbr = strdup(old_obj->time->tz_abbr);
2861+
}
2862+
if (old_obj->time->tz_info) {
2863+
new_obj->time->tz_info = old_obj->time->tz_info;
2864+
}
2865+
}
2866+
/* }}} */
2867+
28352868
/* Helper function used to add an associative array of warnings and errors to a zval */
28362869
static void zval_from_error_container(zval *z, timelib_error_container *error) /* {{{ */
28372870
{

ext/date/php_date.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ PHP_FUNCTION(getdate);
5050
PHP_METHOD(DateTime, __construct);
5151
PHP_METHOD(DateTime, __wakeup);
5252
PHP_METHOD(DateTime, __set_state);
53+
PHP_METHOD(DateTime, createFromImmutable);
5354
PHP_FUNCTION(date_create);
5455
PHP_FUNCTION(date_create_immutable);
5556
PHP_FUNCTION(date_create_from_format);

0 commit comments

Comments
 (0)