Skip to content

Commit 8acc52c

Browse files
committed
Adding ability to create a DateTime or DateTimeImmutable from an instance of the interface
1 parent dabc28d commit 8acc52c

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

ext/date/php_date.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ static const zend_function_entry date_funcs_date[] = {
153153
PHP_ME(DateTime, __wakeup, arginfo_class_DateTimeInterface___wakeup, ZEND_ACC_PUBLIC)
154154
PHP_ME(DateTime, __set_state, arginfo_class_DateTime___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
155155
PHP_ME(DateTime, createFromImmutable, arginfo_class_DateTime_createFromImmutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
156+
PHP_ME(DateTime, createFromInterface, arginfo_class_DateTime_createFromInterface, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
156157
PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_class_DateTime_createFromFormat, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
157158
PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_class_DateTime_getLastErrors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
158159
PHP_ME_MAPPING(format, date_format, arginfo_class_DateTimeInterface_format, 0)
@@ -191,6 +192,7 @@ static const zend_function_entry date_funcs_immutable[] = {
191192
PHP_ME(DateTimeImmutable, setISODate, arginfo_class_DateTimeImmutable_setISODate, 0)
192193
PHP_ME(DateTimeImmutable, setTimestamp, arginfo_class_DateTimeImmutable_setTimestamp, 0)
193194
PHP_ME(DateTimeImmutable, createFromMutable, arginfo_class_DateTimeImmutable_createFromMutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
195+
PHP_ME(DateTimeImmutable, createFromInterface, arginfo_class_DateTimeImmutable_createFromInterface, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
194196
PHP_FE_END
195197
};
196198

@@ -2545,6 +2547,27 @@ PHP_METHOD(DateTime, createFromImmutable)
25452547
}
25462548
/* }}} */
25472549

2550+
/* {{{ proto DateTime::createFromInterface(DateTimeInterface object)
2551+
Creates new DateTime object from an existing DateTimeInterface object.
2552+
*/
2553+
PHP_METHOD(DateTime, createFromInterface)
2554+
{
2555+
zval *datetimeinterface_object = NULL;
2556+
php_date_obj *new_obj = NULL;
2557+
php_date_obj *old_obj = NULL;
2558+
2559+
ZEND_PARSE_PARAMETERS_START(1, 1)
2560+
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
2561+
ZEND_PARSE_PARAMETERS_END();
2562+
2563+
php_date_instantiate(date_ce_date, return_value);
2564+
old_obj = Z_PHPDATE_P(datetimeinterface_object);
2565+
new_obj = Z_PHPDATE_P(return_value);
2566+
2567+
new_obj->time = timelib_time_clone(old_obj->time);
2568+
}
2569+
/* }}} */
2570+
25482571
/* {{{ proto DateTimeImmutable::createFromMutable(DateTime object)
25492572
Creates new DateTimeImmutable object from an existing mutable DateTime object.
25502573
*/
@@ -2566,6 +2589,27 @@ PHP_METHOD(DateTimeImmutable, createFromMutable)
25662589
}
25672590
/* }}} */
25682591

2592+
/* {{{ proto DateTimeImmutable::createFromInterface(DateTimeInterface object)
2593+
Creates new DateTimeImmutable object from an existing DateTimeInterface object.
2594+
*/
2595+
PHP_METHOD(DateTimeImmutable, createFromInterface)
2596+
{
2597+
zval *datetimeinterface_object = NULL;
2598+
php_date_obj *new_obj = NULL;
2599+
php_date_obj *old_obj = NULL;
2600+
2601+
ZEND_PARSE_PARAMETERS_START(1, 1)
2602+
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
2603+
ZEND_PARSE_PARAMETERS_END();
2604+
2605+
php_date_instantiate(date_ce_immutable, return_value);
2606+
old_obj = Z_PHPDATE_P(datetimeinterface_object);
2607+
new_obj = Z_PHPDATE_P(return_value);
2608+
2609+
new_obj->time = timelib_time_clone(old_obj->time);
2610+
}
2611+
/* }}} */
2612+
25692613
static int php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht)
25702614
{
25712615
zval *z_date;

ext/date/php_date.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ PHP_METHOD(DateTime, __construct);
4646
PHP_METHOD(DateTime, __wakeup);
4747
PHP_METHOD(DateTime, __set_state);
4848
PHP_METHOD(DateTime, createFromImmutable);
49+
PHP_METHOD(DateTime, createFromInterface);
4950
PHP_FUNCTION(date_create);
5051
PHP_FUNCTION(date_create_immutable);
5152
PHP_FUNCTION(date_create_from_format);
@@ -79,6 +80,7 @@ PHP_METHOD(DateTimeImmutable, setDate);
7980
PHP_METHOD(DateTimeImmutable, setISODate);
8081
PHP_METHOD(DateTimeImmutable, setTimestamp);
8182
PHP_METHOD(DateTimeImmutable, createFromMutable);
83+
PHP_METHOD(DateTimeImmutable, createFromInterface);
8284

8385
PHP_METHOD(DateTimeZone, __construct);
8486
PHP_METHOD(DateTimeZone, __wakeup);

ext/date/php_date_arginfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTime_createFromImmutable, 0, 0, 1)
257257
ZEND_ARG_OBJ_INFO(0, object, DateTimeImmutable, 0)
258258
ZEND_END_ARG_INFO()
259259

260+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTime_createFromInterface, 0, 0, 1)
261+
ZEND_ARG_OBJ_INFO(0, object, DateTimeInterface, 0)
262+
ZEND_END_ARG_INFO()
263+
260264
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTime_createFromFormat, 0, 0, 2)
261265
ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0)
262266
ZEND_ARG_TYPE_INFO(0, time, IS_STRING, 0)
@@ -310,6 +314,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTimeImmutable_createFromMutable, 0, 0,
310314
ZEND_ARG_OBJ_INFO(0, object, DateTime, 0)
311315
ZEND_END_ARG_INFO()
312316

317+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTimeImmutable_createFromInterface, 0, 0, 1)
318+
ZEND_ARG_OBJ_INFO(0, object, DateTimeInterface, 0)
319+
ZEND_END_ARG_INFO()
320+
313321
#define arginfo_class_DateTimeImmutable_createFromFormat arginfo_class_DateTime_createFromFormat
314322

315323
#define arginfo_class_DateTimeImmutable_getLastErrors arginfo_class_DateTimeInterface_getTimezone
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Tests for DateTimeImmutable::createFromInterface
3+
--INI--
4+
date.timezone=Europe/London
5+
--FILE--
6+
<?php
7+
$current = "2014-03-02 16:24:08";
8+
9+
$i = DateTimeImmutable::createFromInterface( date_create( $current ) );
10+
var_dump( $i );
11+
12+
DateTimeImmutable::createFromInterface( date_create_immutable( $current ) );
13+
var_dump( $i );
14+
?>
15+
--EXPECTF--
16+
object(DateTimeImmutable)#%d (3) {
17+
["date"]=>
18+
string(26) "2014-03-02 16:24:08.000000"
19+
["timezone_type"]=>
20+
int(3)
21+
["timezone"]=>
22+
string(13) "Europe/London"
23+
}
24+
object(DateTimeImmutable)#%d (3) {
25+
["date"]=>
26+
string(26) "2014-03-02 16:24:08.000000"
27+
["timezone_type"]=>
28+
int(3)
29+
["timezone"]=>
30+
string(13) "Europe/London"
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Tests for DateTime::createFromInterface
3+
--INI--
4+
date.timezone=Europe/London
5+
--FILE--
6+
<?php
7+
$current = "2014-03-02 16:24:08";
8+
9+
$i = DateTime::createFromInterface( date_create( $current ) );
10+
var_dump( $i );
11+
12+
DateTime::createFromInterface( date_create_immutable( $current ) );
13+
var_dump( $i );
14+
?>
15+
--EXPECTF--
16+
object(DateTime)#%d (3) {
17+
["date"]=>
18+
string(26) "2014-03-02 16:24:08.000000"
19+
["timezone_type"]=>
20+
int(3)
21+
["timezone"]=>
22+
string(13) "Europe/London"
23+
}
24+
object(DateTime)#%d (3) {
25+
["date"]=>
26+
string(26) "2014-03-02 16:24:08.000000"
27+
["timezone_type"]=>
28+
int(3)
29+
["timezone"]=>
30+
string(13) "Europe/London"
31+
}

0 commit comments

Comments
 (0)