Skip to content

Commit aa22e80

Browse files
committed
Merge branch 'PHP-5.5' into PHP-5.6
* PHP-5.5: Adding a new `createFromImmutable` method to the `DateTime` class to mirror the current `DateTime::createFromMutable()`
2 parents 3e7f47c + c2e2255 commit aa22e80

File tree

6 files changed

+87
-16
lines changed

6 files changed

+87
-16
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ PHP NEWS
1919
- Curl:
2020
. Implemented FR#69278 (HTTP2 support). (Masaki Kagaya)
2121

22+
- Date:
23+
. Added DateTime::createFromImmutable(). (Trevor Suarez)
24+
2225
- Enchant:
2326
. Fixed bug #65406 (Enchant broker plugins are in the wrong place in windows
2427
builds). (Anatol)

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ PHP 5.6 UPGRADE NOTES
271271

272272
- Datetime:
273273
Added DatePeriod::getStartDate(), DatePeriod::getEndDate(), DatePeriod::getDateInterval() in 5.6.5.
274+
Added DateTime::createFromImmutable() in 5.6.8.
274275

275276
- GMP:
276277
Added gmp_root($a, $nth) and gmp_rootrem($a, $nth) for calculating nth roots.

ext/date/php_date.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_date_format, 0, 0, 2)
185185
ZEND_ARG_INFO(0, format)
186186
ZEND_END_ARG_INFO()
187187

188+
ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_immutable, 0, 0, 1)
189+
ZEND_ARG_INFO(0, DateTimeImmutable)
190+
ZEND_END_ARG_INFO()
191+
188192
ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_format, 0, 0, 1)
189193
ZEND_ARG_INFO(0, format)
190194
ZEND_END_ARG_INFO()
@@ -468,6 +472,7 @@ const zend_function_entry date_funcs_date[] = {
468472
PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
469473
PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC)
470474
PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
475+
PHP_ME(DateTime, createFromImmutable, arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
471476
PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
472477
PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
473478
PHP_ME_MAPPING(format, date_format, arginfo_date_method_format, 0)
@@ -2781,7 +2786,7 @@ PHP_METHOD(DateTimeImmutable, createFromMutable)
27812786
php_date_obj *new_obj = NULL;
27822787
php_date_obj *old_obj = NULL;
27832788

2784-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O!", &datetime_object, date_ce_date) == FAILURE) {
2789+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &datetime_object, date_ce_date) == FAILURE) {
27852790
return;
27862791
}
27872792

@@ -2912,6 +2917,34 @@ PHP_METHOD(DateTime, __wakeup)
29122917
}
29132918
/* }}} */
29142919

2920+
/* {{{ proto DateTime::createFromImmutable(DateTimeImmutable object)
2921+
Creates new DateTime object from an existing DateTimeImmutable object.
2922+
*/
2923+
PHP_METHOD(DateTime, createFromImmutable)
2924+
{
2925+
zval *datetimeimmutable_object = NULL;
2926+
php_date_obj *new_obj = NULL;
2927+
php_date_obj *old_obj = NULL;
2928+
2929+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &datetimeimmutable_object, date_ce_immutable) == FAILURE) {
2930+
return;
2931+
}
2932+
2933+
php_date_instantiate(date_ce_date, return_value TSRMLS_CC);
2934+
old_obj = (php_date_obj *) zend_object_store_get_object(datetimeimmutable_object TSRMLS_CC);
2935+
new_obj = (php_date_obj *) zend_object_store_get_object(return_value TSRMLS_CC);
2936+
2937+
new_obj->time = timelib_time_ctor();
2938+
*new_obj->time = *old_obj->time;
2939+
if (old_obj->time->tz_abbr) {
2940+
new_obj->time->tz_abbr = strdup(old_obj->time->tz_abbr);
2941+
}
2942+
if (old_obj->time->tz_info) {
2943+
new_obj->time->tz_info = old_obj->time->tz_info;
2944+
}
2945+
}
2946+
/* }}} */
2947+
29152948
/* Helper function used to add an associative array of warnings and errors to a zval */
29162949
static void zval_from_error_container(zval *z, timelib_error_container *error)
29172950
{

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);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Tests for DateTime::createFromImmutable.
3+
--INI--
4+
date.timezone=America/New_York
5+
--FILE--
6+
<?php
7+
$current = "2015-03-05 07:00:16";
8+
9+
$i = DateTime::createFromImmutable(date_create_immutable($current));
10+
var_dump($i);
11+
12+
$i = DateTime::createFromImmutable(date_create($current));
13+
var_dump($i);
14+
?>
15+
--EXPECTF--
16+
object(DateTime)#%d (3) {
17+
["date"]=>
18+
string(26) "2015-03-05 07:00:16.000000"
19+
["timezone_type"]=>
20+
int(3)
21+
["timezone"]=>
22+
string(16) "America/New_York"
23+
}
24+
25+
Warning: DateTime::createFromImmutable() expects parameter 1 to be DateTimeImmutable, object given in %stests%eDateTime_createFromImmutable.php on line %d
26+
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)