Skip to content

Commit 02970e8

Browse files
committed
Added method DateTime::createFromImmutable()
1 parent 1837eea commit 02970e8

File tree

4 files changed

+93
-19
lines changed

4 files changed

+93
-19
lines changed

ext/date/php_date.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ ZEND_END_ARG_INFO()
309309
ZEND_BEGIN_ARG_INFO(arginfo_date_method_timestamp_get, 0)
310310
ZEND_END_ARG_INFO()
311311

312+
313+
ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_immutable, 0, 0, 1)
314+
ZEND_ARG_INFO(0, DateTimeImmutable)
315+
ZEND_END_ARG_INFO()
316+
312317
ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_mutable, 0, 0, 1)
313318
ZEND_ARG_INFO(0, DateTime)
314319
ZEND_END_ARG_INFO()
@@ -469,9 +474,10 @@ static const zend_function_entry date_funcs_interface[] = {
469474
};
470475

471476
const zend_function_entry date_funcs_date[] = {
472-
PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
473-
PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC)
474-
PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
477+
PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
478+
PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC)
479+
PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
480+
PHP_ME(DateTime, createFromImmutable, arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
475481
PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
476482
PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
477483
PHP_ME_MAPPING(format, date_format, arginfo_date_method_format, 0)
@@ -2816,7 +2822,35 @@ PHP_METHOD(DateTimeImmutable, __construct)
28162822
}
28172823
/* }}} */
28182824

2819-
/* {{{ proto DateTimeImmutable::createFromMutable(DateTimeZone object)
2825+
/* {{{ proto DateTime::createFromImmutable(DateTimeImmutable object)
2826+
Creates new DateTime object from an existing immutable DateTimeImmutable object.
2827+
*/
2828+
PHP_METHOD(DateTime, createFromImmutable)
2829+
{
2830+
zval *datetimeimmutable_object = NULL;
2831+
php_date_obj *new_obj = NULL;
2832+
php_date_obj *old_obj = NULL;
2833+
2834+
ZEND_PARSE_PARAMETERS_START(1, 1)
2835+
Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
2836+
ZEND_PARSE_PARAMETERS_END();
2837+
2838+
php_date_instantiate(date_ce_date, return_value);
2839+
old_obj = Z_PHPDATE_P(datetimeimmutable_object);
2840+
new_obj = Z_PHPDATE_P(return_value);
2841+
2842+
new_obj->time = timelib_time_ctor();
2843+
*new_obj->time = *old_obj->time;
2844+
if (old_obj->time->tz_abbr) {
2845+
new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr);
2846+
}
2847+
if (old_obj->time->tz_info) {
2848+
new_obj->time->tz_info = old_obj->time->tz_info;
2849+
}
2850+
}
2851+
/* }}} */
2852+
2853+
/* {{{ proto DateTimeImmutable::createFromMutable(DateTime object)
28202854
Creates new DateTimeImmutable object from an existing mutable DateTime object.
28212855
*/
28222856
PHP_METHOD(DateTimeImmutable, createFromMutable)

ext/date/php_date.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ PHP_FUNCTION(getdate);
5353
PHP_METHOD(DateTime, __construct);
5454
PHP_METHOD(DateTime, __wakeup);
5555
PHP_METHOD(DateTime, __set_state);
56+
PHP_METHOD(DateTime, createFromImmutable);
5657
PHP_FUNCTION(date_create);
5758
PHP_FUNCTION(date_create_immutable);
5859
PHP_FUNCTION(date_create_from_format);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Tests for DateTime::createFromImmutable.
3+
--INI--
4+
date.timezone=Europe/London
5+
--FILE--
6+
<?php
7+
$current = "2014-03-02 16:24:08";
8+
$i = date_create_immutable( $current );
9+
10+
$m = DateTime::createFromImmutable( $i );
11+
var_dump( $m );
12+
13+
$m->modify('+ 1 hour');
14+
15+
var_dump( $i->format('Y-m-d H:i:s') === $current );
16+
17+
$m = DateTime::createFromImmutable( date_create( $current ) );
18+
var_dump( $m );
19+
?>
20+
--EXPECTF--
21+
object(DateTime)#%d (3) {
22+
["date"]=>
23+
string(26) "2014-03-02 16:24:08.000000"
24+
["timezone_type"]=>
25+
int(3)
26+
["timezone"]=>
27+
string(13) "Europe/London"
28+
}
29+
bool(true)
30+
31+
Warning: DateTime::createFromImmutable() expects parameter 1 to be DateTimeImmutable, object given in %stests%eDateTime_createFromImmutable.php on line %d
32+
NULL

ext/date/tests/DateTime_verify.phpt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object(ReflectionClass)#%d (1) {
2727
string(8) "DateTime"
2828
}
2929
..and get names of all its methods
30-
array(18) {
30+
array(19) {
3131
[0]=>
3232
object(ReflectionMethod)#%d (2) {
3333
["name"]=>
@@ -52,102 +52,109 @@ array(18) {
5252
[3]=>
5353
object(ReflectionMethod)#%d (2) {
5454
["name"]=>
55-
string(16) "createFromFormat"
55+
string(19) "createFromImmutable"
5656
["class"]=>
5757
string(8) "DateTime"
5858
}
5959
[4]=>
6060
object(ReflectionMethod)#%d (2) {
6161
["name"]=>
62-
string(13) "getLastErrors"
62+
string(16) "createFromFormat"
6363
["class"]=>
6464
string(8) "DateTime"
6565
}
6666
[5]=>
6767
object(ReflectionMethod)#%d (2) {
6868
["name"]=>
69-
string(6) "format"
69+
string(13) "getLastErrors"
7070
["class"]=>
7171
string(8) "DateTime"
7272
}
7373
[6]=>
7474
object(ReflectionMethod)#%d (2) {
7575
["name"]=>
76-
string(6) "modify"
76+
string(6) "format"
7777
["class"]=>
7878
string(8) "DateTime"
7979
}
8080
[7]=>
8181
object(ReflectionMethod)#%d (2) {
8282
["name"]=>
83-
string(3) "add"
83+
string(6) "modify"
8484
["class"]=>
8585
string(8) "DateTime"
8686
}
8787
[8]=>
8888
object(ReflectionMethod)#%d (2) {
8989
["name"]=>
90-
string(3) "sub"
90+
string(3) "add"
9191
["class"]=>
9292
string(8) "DateTime"
9393
}
9494
[9]=>
9595
object(ReflectionMethod)#%d (2) {
9696
["name"]=>
97-
string(11) "getTimezone"
97+
string(3) "sub"
9898
["class"]=>
9999
string(8) "DateTime"
100100
}
101101
[10]=>
102102
object(ReflectionMethod)#%d (2) {
103103
["name"]=>
104-
string(11) "setTimezone"
104+
string(11) "getTimezone"
105105
["class"]=>
106106
string(8) "DateTime"
107107
}
108108
[11]=>
109109
object(ReflectionMethod)#%d (2) {
110110
["name"]=>
111-
string(9) "getOffset"
111+
string(11) "setTimezone"
112112
["class"]=>
113113
string(8) "DateTime"
114114
}
115115
[12]=>
116116
object(ReflectionMethod)#%d (2) {
117117
["name"]=>
118-
string(7) "setTime"
118+
string(9) "getOffset"
119119
["class"]=>
120120
string(8) "DateTime"
121121
}
122122
[13]=>
123123
object(ReflectionMethod)#%d (2) {
124124
["name"]=>
125-
string(7) "setDate"
125+
string(7) "setTime"
126126
["class"]=>
127127
string(8) "DateTime"
128128
}
129129
[14]=>
130130
object(ReflectionMethod)#%d (2) {
131131
["name"]=>
132-
string(10) "setISODate"
132+
string(7) "setDate"
133133
["class"]=>
134134
string(8) "DateTime"
135135
}
136136
[15]=>
137137
object(ReflectionMethod)#%d (2) {
138138
["name"]=>
139-
string(12) "setTimestamp"
139+
string(10) "setISODate"
140140
["class"]=>
141141
string(8) "DateTime"
142142
}
143143
[16]=>
144144
object(ReflectionMethod)#%d (2) {
145145
["name"]=>
146-
string(12) "getTimestamp"
146+
string(12) "setTimestamp"
147147
["class"]=>
148148
string(8) "DateTime"
149149
}
150150
[17]=>
151+
object(ReflectionMethod)#%d (2) {
152+
["name"]=>
153+
string(12) "getTimestamp"
154+
["class"]=>
155+
string(8) "DateTime"
156+
}
157+
[18]=>
151158
object(ReflectionMethod)#%d (2) {
152159
["name"]=>
153160
string(4) "diff"

0 commit comments

Comments
 (0)