mirror of
https://github.com/php/php-src.git
synced 2024-12-03 23:05:57 +08:00
Fixed bug #60302: DateTime::createFromFormat should new static(), not new self()
Also fixes similar issues for DateTimeImmutable::createFromFormat, DateTime::createFromImmmutable, DateTime::createFromInterface, DateTimeImmutable::createFromMutable, and DateTimeImmutable::createFromInterface.
This commit is contained in:
parent
6b7e2f0d15
commit
90434d7fe3
4
NEWS
4
NEWS
@ -10,6 +10,10 @@ PHP NEWS
|
||||
. Fixed bug #79897 (Promoted constructor params with attribs cause crash).
|
||||
(Deus Kane)
|
||||
|
||||
- Date:
|
||||
. Fixed bug #60302 (DateTime::createFromFormat should new static(), not new
|
||||
self()). (Derick)
|
||||
|
||||
- JIT:
|
||||
. Fixed bug #79864 (JIT segfault in Symfony OptionsResolver). (Dmitry)
|
||||
. Fixed bug #79888 (Incorrect execution with JIT enabled). (Dmitry)
|
||||
|
@ -2292,7 +2292,7 @@ PHP_FUNCTION(date_create)
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ Returns new DateTime object */
|
||||
/* {{{ Returns new DateTimeImmutable object */
|
||||
PHP_FUNCTION(date_create_immutable)
|
||||
{
|
||||
zval *timezone_object = NULL;
|
||||
@ -2327,7 +2327,7 @@ PHP_FUNCTION(date_create_from_format)
|
||||
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
|
||||
ZEND_PARSE_PARAMETERS_END();
|
||||
|
||||
php_date_instantiate(date_ce_date, return_value);
|
||||
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
|
||||
if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
|
||||
zval_ptr_dtor(return_value);
|
||||
RETURN_FALSE;
|
||||
@ -2349,7 +2349,7 @@ PHP_FUNCTION(date_create_immutable_from_format)
|
||||
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
|
||||
ZEND_PARSE_PARAMETERS_END();
|
||||
|
||||
php_date_instantiate(date_ce_immutable, return_value);
|
||||
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
|
||||
if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
|
||||
zval_ptr_dtor(return_value);
|
||||
RETURN_FALSE;
|
||||
@ -2408,7 +2408,7 @@ PHP_METHOD(DateTime, createFromImmutable)
|
||||
Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
|
||||
ZEND_PARSE_PARAMETERS_END();
|
||||
|
||||
php_date_instantiate(date_ce_date, return_value);
|
||||
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
|
||||
old_obj = Z_PHPDATE_P(datetimeimmutable_object);
|
||||
new_obj = Z_PHPDATE_P(return_value);
|
||||
|
||||
@ -2427,7 +2427,7 @@ PHP_METHOD(DateTime, createFromInterface)
|
||||
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
|
||||
ZEND_PARSE_PARAMETERS_END();
|
||||
|
||||
php_date_instantiate(date_ce_date, return_value);
|
||||
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
|
||||
old_obj = Z_PHPDATE_P(datetimeinterface_object);
|
||||
new_obj = Z_PHPDATE_P(return_value);
|
||||
|
||||
@ -2446,7 +2446,7 @@ PHP_METHOD(DateTimeImmutable, createFromMutable)
|
||||
Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_date)
|
||||
ZEND_PARSE_PARAMETERS_END();
|
||||
|
||||
php_date_instantiate(date_ce_immutable, return_value);
|
||||
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
|
||||
old_obj = Z_PHPDATE_P(datetime_object);
|
||||
new_obj = Z_PHPDATE_P(return_value);
|
||||
|
||||
@ -2465,7 +2465,7 @@ PHP_METHOD(DateTimeImmutable, createFromInterface)
|
||||
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
|
||||
ZEND_PARSE_PARAMETERS_END();
|
||||
|
||||
php_date_instantiate(date_ce_immutable, return_value);
|
||||
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
|
||||
old_obj = Z_PHPDATE_P(datetimeinterface_object);
|
||||
new_obj = Z_PHPDATE_P(return_value);
|
||||
|
||||
|
@ -0,0 +1,81 @@
|
||||
--TEST--
|
||||
Tests for DateTimeImmutable::createFromInterface inheritance
|
||||
--INI--
|
||||
date.timezone=Europe/London
|
||||
--FILE--
|
||||
<?php
|
||||
class MyDateTimeImmutable extends DateTimeImmutable {}
|
||||
|
||||
$current = "2014-03-02 16:24:08";
|
||||
|
||||
$i = MyDateTimeImmutable::createFromInterface( date_create( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$i = MyDateTimeImmutable::createFromInterface( date_create_immutable( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$current = "2019-12-16 15:06:46 CET";
|
||||
|
||||
$i = MyDateTimeImmutable::createFromInterface( date_create( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$i = MyDateTimeImmutable::createFromInterface( date_create_immutable( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$current = "2019-12-16 15:08:20 +0100";
|
||||
|
||||
$i = MyDateTimeImmutable::createFromInterface( date_create( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$i = MyDateTimeImmutable::createFromInterface( date_create_immutable( $current ) );
|
||||
var_dump( $i );
|
||||
?>
|
||||
--EXPECTF--
|
||||
object(MyDateTimeImmutable)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2014-03-02 16:24:08.000000"
|
||||
["timezone_type"]=>
|
||||
int(3)
|
||||
["timezone"]=>
|
||||
string(13) "Europe/London"
|
||||
}
|
||||
object(MyDateTimeImmutable)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2014-03-02 16:24:08.000000"
|
||||
["timezone_type"]=>
|
||||
int(3)
|
||||
["timezone"]=>
|
||||
string(13) "Europe/London"
|
||||
}
|
||||
object(MyDateTimeImmutable)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:06:46.000000"
|
||||
["timezone_type"]=>
|
||||
int(2)
|
||||
["timezone"]=>
|
||||
string(3) "CET"
|
||||
}
|
||||
object(MyDateTimeImmutable)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:06:46.000000"
|
||||
["timezone_type"]=>
|
||||
int(2)
|
||||
["timezone"]=>
|
||||
string(3) "CET"
|
||||
}
|
||||
object(MyDateTimeImmutable)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:08:20.000000"
|
||||
["timezone_type"]=>
|
||||
int(1)
|
||||
["timezone"]=>
|
||||
string(6) "+01:00"
|
||||
}
|
||||
object(MyDateTimeImmutable)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:08:20.000000"
|
||||
["timezone_type"]=>
|
||||
int(1)
|
||||
["timezone"]=>
|
||||
string(6) "+01:00"
|
||||
}
|
29
ext/date/tests/DateTimeImmutable_createFromMutable-002.phpt
Normal file
29
ext/date/tests/DateTimeImmutable_createFromMutable-002.phpt
Normal file
@ -0,0 +1,29 @@
|
||||
--TEST--
|
||||
Tests for inherited DateTimeImmutable::createFromMutable
|
||||
--INI--
|
||||
date.timezone=Europe/London
|
||||
--FILE--
|
||||
<?php
|
||||
class MyDateTimeImmutable extends DateTimeImmutable {}
|
||||
|
||||
$current = "2014-03-02 16:24:08";
|
||||
|
||||
$i = MyDateTimeImmutable::createFromMutable( date_create( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
try {
|
||||
MyDateTimeImmutable::createFromMutable( date_create_immutable( $current ) );
|
||||
} catch (TypeError $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
object(MyDateTimeImmutable)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2014-03-02 16:24:08.000000"
|
||||
["timezone_type"]=>
|
||||
int(3)
|
||||
["timezone"]=>
|
||||
string(13) "Europe/London"
|
||||
}
|
||||
DateTimeImmutable::createFromMutable(): Argument #1 ($object) must be of type DateTime, DateTimeImmutable given
|
@ -1,5 +1,5 @@
|
||||
--TEST--
|
||||
Tests for DateTime::createFromImmutable.
|
||||
Tests for DateTime::createFromImmutable
|
||||
--INI--
|
||||
date.timezone=Europe/London
|
||||
--FILE--
|
35
ext/date/tests/DateTime_createFromImmutable-002.phpt
Normal file
35
ext/date/tests/DateTime_createFromImmutable-002.phpt
Normal file
@ -0,0 +1,35 @@
|
||||
--TEST--
|
||||
Tests for inherited DateTime::createFromImmutable
|
||||
--INI--
|
||||
date.timezone=Europe/London
|
||||
--FILE--
|
||||
<?php
|
||||
class MyDateTime extends DateTime {}
|
||||
|
||||
$current = "2014-03-02 16:24:08";
|
||||
$i = date_create_immutable( $current );
|
||||
|
||||
$m = MyDateTime::createFromImmutable( $i );
|
||||
var_dump( $m );
|
||||
|
||||
$m->modify('+ 1 hour');
|
||||
|
||||
var_dump( $i->format('Y-m-d H:i:s') === $current );
|
||||
|
||||
try {
|
||||
MyDateTime::createFromImmutable( date_create( $current ) );
|
||||
} catch (TypeError $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
object(MyDateTime)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2014-03-02 16:24:08.000000"
|
||||
["timezone_type"]=>
|
||||
int(3)
|
||||
["timezone"]=>
|
||||
string(13) "Europe/London"
|
||||
}
|
||||
bool(true)
|
||||
DateTime::createFromImmutable(): Argument #1 ($object) must be of type DateTimeImmutable, DateTime given
|
81
ext/date/tests/DateTime_createFromInterface-002.phpt
Normal file
81
ext/date/tests/DateTime_createFromInterface-002.phpt
Normal file
@ -0,0 +1,81 @@
|
||||
--TEST--
|
||||
Tests for DateTime::createFromInterface with inheritance
|
||||
--INI--
|
||||
date.timezone=Europe/London
|
||||
--FILE--
|
||||
<?php
|
||||
class MyDateTime extends DateTime {}
|
||||
|
||||
$current = "2014-03-02 16:24:08";
|
||||
|
||||
$i = MyDateTime::createFromInterface( date_create( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$i = MyDateTime::createFromInterface( date_create_immutable( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$current = "2019-12-16 15:06:46 CET";
|
||||
|
||||
$i = MyDateTime::createFromInterface( date_create( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$i = MyDateTime::createFromInterface( date_create_immutable( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$current = "2019-12-16 15:08:20 +0100";
|
||||
|
||||
$i = MyDateTime::createFromInterface( date_create( $current ) );
|
||||
var_dump( $i );
|
||||
|
||||
$i = MyDateTime::createFromInterface( date_create_immutable( $current ) );
|
||||
var_dump( $i );
|
||||
?>
|
||||
--EXPECTF--
|
||||
object(MyDateTime)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2014-03-02 16:24:08.000000"
|
||||
["timezone_type"]=>
|
||||
int(3)
|
||||
["timezone"]=>
|
||||
string(13) "Europe/London"
|
||||
}
|
||||
object(MyDateTime)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2014-03-02 16:24:08.000000"
|
||||
["timezone_type"]=>
|
||||
int(3)
|
||||
["timezone"]=>
|
||||
string(13) "Europe/London"
|
||||
}
|
||||
object(MyDateTime)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:06:46.000000"
|
||||
["timezone_type"]=>
|
||||
int(2)
|
||||
["timezone"]=>
|
||||
string(3) "CET"
|
||||
}
|
||||
object(MyDateTime)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:06:46.000000"
|
||||
["timezone_type"]=>
|
||||
int(2)
|
||||
["timezone"]=>
|
||||
string(3) "CET"
|
||||
}
|
||||
object(MyDateTime)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:08:20.000000"
|
||||
["timezone_type"]=>
|
||||
int(1)
|
||||
["timezone"]=>
|
||||
string(6) "+01:00"
|
||||
}
|
||||
object(MyDateTime)#%d (3) {
|
||||
["date"]=>
|
||||
string(26) "2019-12-16 15:08:20.000000"
|
||||
["timezone_type"]=>
|
||||
int(1)
|
||||
["timezone"]=>
|
||||
string(6) "+01:00"
|
||||
}
|
13
ext/date/tests/bug60302-001.phpt
Normal file
13
ext/date/tests/bug60302-001.phpt
Normal file
@ -0,0 +1,13 @@
|
||||
--TEST--
|
||||
Test for bug #60302: DateTime::createFromFormat should new static(), not new self()
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
class MyDateTime extends DateTime { }
|
||||
|
||||
$d = MyDateTime::createFromFormat('Y-m-d', '2011-01-01');
|
||||
|
||||
echo get_class($d);
|
||||
?>
|
||||
--EXPECT--
|
||||
MyDateTime
|
13
ext/date/tests/bug60302-002.phpt
Normal file
13
ext/date/tests/bug60302-002.phpt
Normal file
@ -0,0 +1,13 @@
|
||||
--TEST--
|
||||
Test for bug #60302: DateTimeImmutable::createFromFormat should new static(), not new self()
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
class MyDateTime extends DateTimeImmutable { }
|
||||
|
||||
$d = MyDateTime::createFromFormat('Y-m-d', '2011-01-01');
|
||||
|
||||
echo get_class($d);
|
||||
?>
|
||||
--EXPECT--
|
||||
MyDateTime
|
Loading…
Reference in New Issue
Block a user