Skip to content

Commit 1658b5b

Browse files
mikeSimonsonnikic
authored andcommitted
Adding DateTime(Immutable)::createFromInterface()
These are like DateTime::createFromImmutable() DateTimeImmutable::createFromMutable() but accept any DateTimeInterface instead. Closes GH-5016.
1 parent fb8ffda commit 1658b5b

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ PHP 8.0 UPGRADE NOTES
334334
RFC: https://wiki.php.net/rfc/weak_maps
335335
. Added ValueError class.
336336

337+
- Date:
338+
. Added DateTime::createFromInterface() and
339+
DateTimeImmutable::createFromInterface().
340+
337341
========================================
338342
3. Changes in SAPI modules
339343
========================================

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.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public static function __set_state(array $array);
146146
/** @return DateTime */
147147
public static function createFromImmutable(DateTimeImmutable $object);
148148

149+
public static function createFromInterface(DateTimeInterface $object): DateTime;
150+
149151
/** @return DateTime|false */
150152
public static function createFromFormat(
151153
string $format, string $time, ?DateTimeZone $timezone = null);
@@ -187,6 +189,8 @@ public static function __set_state(array $array);
187189
/** @return DateTimeImmutable */
188190
public static function createFromMutable(DateTime $object);
189191

192+
public static function createFromInterface(DateTimeInterface $object): DateTimeImmutable;
193+
190194
/** @return DateTimeImmutable|false */
191195
public static function createFromFormat(
192196
string $format, string $time, ?DateTimeZone $timezone = null);

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_WITH_RETURN_OBJ_INFO_EX(arginfo_class_DateTime_createFromInterface, 0, 1, DateTime, 0)
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_WITH_RETURN_OBJ_INFO_EX(arginfo_class_DateTimeImmutable_createFromInterface, 0, 1, DateTimeImmutable, 0)
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
$i = DateTimeImmutable::createFromInterface( date_create_immutable( $current ) );
13+
var_dump( $i );
14+
15+
$current = "2019-12-16 15:06:46 CET";
16+
17+
$i = DateTimeImmutable::createFromInterface( date_create( $current ) );
18+
var_dump( $i );
19+
20+
$i = DateTimeImmutable::createFromInterface( date_create_immutable( $current ) );
21+
var_dump( $i );
22+
23+
$current = "2019-12-16 15:08:20 +0100";
24+
25+
$i = DateTimeImmutable::createFromInterface( date_create( $current ) );
26+
var_dump( $i );
27+
28+
$i = DateTimeImmutable::createFromInterface( date_create_immutable( $current ) );
29+
var_dump( $i );
30+
?>
31+
--EXPECTF--
32+
object(DateTimeImmutable)#%d (3) {
33+
["date"]=>
34+
string(26) "2014-03-02 16:24:08.000000"
35+
["timezone_type"]=>
36+
int(3)
37+
["timezone"]=>
38+
string(13) "Europe/London"
39+
}
40+
object(DateTimeImmutable)#%d (3) {
41+
["date"]=>
42+
string(26) "2014-03-02 16:24:08.000000"
43+
["timezone_type"]=>
44+
int(3)
45+
["timezone"]=>
46+
string(13) "Europe/London"
47+
}
48+
object(DateTimeImmutable)#%d (3) {
49+
["date"]=>
50+
string(26) "2019-12-16 15:06:46.000000"
51+
["timezone_type"]=>
52+
int(2)
53+
["timezone"]=>
54+
string(3) "CET"
55+
}
56+
object(DateTimeImmutable)#%d (3) {
57+
["date"]=>
58+
string(26) "2019-12-16 15:06:46.000000"
59+
["timezone_type"]=>
60+
int(2)
61+
["timezone"]=>
62+
string(3) "CET"
63+
}
64+
object(DateTimeImmutable)#%d (3) {
65+
["date"]=>
66+
string(26) "2019-12-16 15:08:20.000000"
67+
["timezone_type"]=>
68+
int(1)
69+
["timezone"]=>
70+
string(6) "+01:00"
71+
}
72+
object(DateTimeImmutable)#%d (3) {
73+
["date"]=>
74+
string(26) "2019-12-16 15:08:20.000000"
75+
["timezone_type"]=>
76+
int(1)
77+
["timezone"]=>
78+
string(6) "+01:00"
79+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
$i = DateTime::createFromInterface( date_create_immutable( $current ) );
13+
var_dump( $i );
14+
15+
$current = "2019-12-16 15:06:46 CET";
16+
17+
$i = DateTime::createFromInterface( date_create( $current ) );
18+
var_dump( $i );
19+
20+
$i = DateTime::createFromInterface( date_create_immutable( $current ) );
21+
var_dump( $i );
22+
23+
$current = "2019-12-16 15:08:20 +0100";
24+
25+
$i = DateTime::createFromInterface( date_create( $current ) );
26+
var_dump( $i );
27+
28+
$i = DateTime::createFromInterface( date_create_immutable( $current ) );
29+
var_dump( $i );
30+
?>
31+
--EXPECTF--
32+
object(DateTime)#%d (3) {
33+
["date"]=>
34+
string(26) "2014-03-02 16:24:08.000000"
35+
["timezone_type"]=>
36+
int(3)
37+
["timezone"]=>
38+
string(13) "Europe/London"
39+
}
40+
object(DateTime)#%d (3) {
41+
["date"]=>
42+
string(26) "2014-03-02 16:24:08.000000"
43+
["timezone_type"]=>
44+
int(3)
45+
["timezone"]=>
46+
string(13) "Europe/London"
47+
}
48+
object(DateTime)#%d (3) {
49+
["date"]=>
50+
string(26) "2019-12-16 15:06:46.000000"
51+
["timezone_type"]=>
52+
int(2)
53+
["timezone"]=>
54+
string(3) "CET"
55+
}
56+
object(DateTime)#%d (3) {
57+
["date"]=>
58+
string(26) "2019-12-16 15:06:46.000000"
59+
["timezone_type"]=>
60+
int(2)
61+
["timezone"]=>
62+
string(3) "CET"
63+
}
64+
object(DateTime)#%d (3) {
65+
["date"]=>
66+
string(26) "2019-12-16 15:08:20.000000"
67+
["timezone_type"]=>
68+
int(1)
69+
["timezone"]=>
70+
string(6) "+01:00"
71+
}
72+
object(DateTime)#%d (3) {
73+
["date"]=>
74+
string(26) "2019-12-16 15:08:20.000000"
75+
["timezone_type"]=>
76+
int(1)
77+
["timezone"]=>
78+
string(6) "+01:00"
79+
}

0 commit comments

Comments
 (0)