diff options
author | mike <mike.simonson@gmail.com> | 2019-12-16 14:31:14 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-01-03 16:55:12 +0100 |
commit | 1658b5babc34c46b3b78b852e7a5f134845ace7c (patch) | |
tree | edb3122b32957995bb4a658d3be6644f25403973 | |
parent | fb8ffda690b0a13d1ee4f464c1aeb1e129feedf4 (diff) | |
download | php-git-1658b5babc34c46b3b78b852e7a5f134845ace7c.tar.gz |
Adding DateTime(Immutable)::createFromInterface()
These are like
DateTime::createFromImmutable()
DateTimeImmutable::createFromMutable()
but accept any DateTimeInterface instead.
Closes GH-5016.
-rw-r--r-- | UPGRADING | 4 | ||||
-rw-r--r-- | ext/date/php_date.c | 44 | ||||
-rw-r--r-- | ext/date/php_date.h | 2 | ||||
-rw-r--r-- | ext/date/php_date.stub.php | 4 | ||||
-rw-r--r-- | ext/date/php_date_arginfo.h | 8 | ||||
-rw-r--r-- | ext/date/tests/DateTimeImmutable_createFromInterface.phpt | 79 | ||||
-rw-r--r-- | ext/date/tests/DateTime_createFromInterface.phpt | 79 |
7 files changed, 220 insertions, 0 deletions
@@ -334,6 +334,10 @@ PHP 8.0 UPGRADE NOTES RFC: https://wiki.php.net/rfc/weak_maps . Added ValueError class. +- Date: + . Added DateTime::createFromInterface() and + DateTimeImmutable::createFromInterface(). + ======================================== 3. Changes in SAPI modules ======================================== diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 2f5d9af22a..362bd711b6 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -153,6 +153,7 @@ static const zend_function_entry date_funcs_date[] = { PHP_ME(DateTime, __wakeup, arginfo_class_DateTimeInterface___wakeup, ZEND_ACC_PUBLIC) PHP_ME(DateTime, __set_state, arginfo_class_DateTime___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(DateTime, createFromImmutable, arginfo_class_DateTime_createFromImmutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(DateTime, createFromInterface, arginfo_class_DateTime_createFromInterface, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_class_DateTime_createFromFormat, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_class_DateTime_getLastErrors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(format, date_format, arginfo_class_DateTimeInterface_format, 0) @@ -191,6 +192,7 @@ static const zend_function_entry date_funcs_immutable[] = { PHP_ME(DateTimeImmutable, setISODate, arginfo_class_DateTimeImmutable_setISODate, 0) PHP_ME(DateTimeImmutable, setTimestamp, arginfo_class_DateTimeImmutable_setTimestamp, 0) PHP_ME(DateTimeImmutable, createFromMutable, arginfo_class_DateTimeImmutable_createFromMutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(DateTimeImmutable, createFromInterface, arginfo_class_DateTimeImmutable_createFromInterface, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; @@ -2545,6 +2547,27 @@ PHP_METHOD(DateTime, createFromImmutable) } /* }}} */ +/* {{{ proto DateTime::createFromInterface(DateTimeInterface object) + Creates new DateTime object from an existing DateTimeInterface object. +*/ +PHP_METHOD(DateTime, createFromInterface) +{ + zval *datetimeinterface_object = NULL; + php_date_obj *new_obj = NULL; + php_date_obj *old_obj = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface) + ZEND_PARSE_PARAMETERS_END(); + + php_date_instantiate(date_ce_date, return_value); + old_obj = Z_PHPDATE_P(datetimeinterface_object); + new_obj = Z_PHPDATE_P(return_value); + + new_obj->time = timelib_time_clone(old_obj->time); +} +/* }}} */ + /* {{{ proto DateTimeImmutable::createFromMutable(DateTime object) Creates new DateTimeImmutable object from an existing mutable DateTime object. */ @@ -2566,6 +2589,27 @@ PHP_METHOD(DateTimeImmutable, createFromMutable) } /* }}} */ +/* {{{ proto DateTimeImmutable::createFromInterface(DateTimeInterface object) + Creates new DateTimeImmutable object from an existing DateTimeInterface object. +*/ +PHP_METHOD(DateTimeImmutable, createFromInterface) +{ + zval *datetimeinterface_object = NULL; + php_date_obj *new_obj = NULL; + php_date_obj *old_obj = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface) + ZEND_PARSE_PARAMETERS_END(); + + php_date_instantiate(date_ce_immutable, return_value); + old_obj = Z_PHPDATE_P(datetimeinterface_object); + new_obj = Z_PHPDATE_P(return_value); + + new_obj->time = timelib_time_clone(old_obj->time); +} +/* }}} */ + static int php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht) { zval *z_date; diff --git a/ext/date/php_date.h b/ext/date/php_date.h index 57a9e28d88..8458b0187e 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -46,6 +46,7 @@ PHP_METHOD(DateTime, __construct); PHP_METHOD(DateTime, __wakeup); PHP_METHOD(DateTime, __set_state); PHP_METHOD(DateTime, createFromImmutable); +PHP_METHOD(DateTime, createFromInterface); PHP_FUNCTION(date_create); PHP_FUNCTION(date_create_immutable); PHP_FUNCTION(date_create_from_format); @@ -79,6 +80,7 @@ PHP_METHOD(DateTimeImmutable, setDate); PHP_METHOD(DateTimeImmutable, setISODate); PHP_METHOD(DateTimeImmutable, setTimestamp); PHP_METHOD(DateTimeImmutable, createFromMutable); +PHP_METHOD(DateTimeImmutable, createFromInterface); PHP_METHOD(DateTimeZone, __construct); PHP_METHOD(DateTimeZone, __wakeup); diff --git a/ext/date/php_date.stub.php b/ext/date/php_date.stub.php index 17d9a848f0..e9985dd6b2 100644 --- a/ext/date/php_date.stub.php +++ b/ext/date/php_date.stub.php @@ -146,6 +146,8 @@ class DateTime implements DateTimeInterface { /** @return DateTime */ public static function createFromImmutable(DateTimeImmutable $object); + public static function createFromInterface(DateTimeInterface $object): DateTime; + /** @return DateTime|false */ public static function createFromFormat( string $format, string $time, ?DateTimeZone $timezone = null); @@ -187,6 +189,8 @@ class DateTimeImmutable implements DateTimeInterface { /** @return DateTimeImmutable */ public static function createFromMutable(DateTime $object); + public static function createFromInterface(DateTimeInterface $object): DateTimeImmutable; + /** @return DateTimeImmutable|false */ public static function createFromFormat( string $format, string $time, ?DateTimeZone $timezone = null); diff --git a/ext/date/php_date_arginfo.h b/ext/date/php_date_arginfo.h index 183e3e0d30..acf6ddd663 100644 --- a/ext/date/php_date_arginfo.h +++ b/ext/date/php_date_arginfo.h @@ -257,6 +257,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTime_createFromImmutable, 0, 0, 1) ZEND_ARG_OBJ_INFO(0, object, DateTimeImmutable, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_DateTime_createFromInterface, 0, 1, DateTime, 0) + ZEND_ARG_OBJ_INFO(0, object, DateTimeInterface, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTime_createFromFormat, 0, 0, 2) ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, time, IS_STRING, 0) @@ -310,6 +314,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTimeImmutable_createFromMutable, 0, 0, ZEND_ARG_OBJ_INFO(0, object, DateTime, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_DateTimeImmutable_createFromInterface, 0, 1, DateTimeImmutable, 0) + ZEND_ARG_OBJ_INFO(0, object, DateTimeInterface, 0) +ZEND_END_ARG_INFO() + #define arginfo_class_DateTimeImmutable_createFromFormat arginfo_class_DateTime_createFromFormat #define arginfo_class_DateTimeImmutable_getLastErrors arginfo_class_DateTimeInterface_getTimezone diff --git a/ext/date/tests/DateTimeImmutable_createFromInterface.phpt b/ext/date/tests/DateTimeImmutable_createFromInterface.phpt new file mode 100644 index 0000000000..31e3847ef7 --- /dev/null +++ b/ext/date/tests/DateTimeImmutable_createFromInterface.phpt @@ -0,0 +1,79 @@ +--TEST-- +Tests for DateTimeImmutable::createFromInterface +--INI-- +date.timezone=Europe/London +--FILE-- +<?php +$current = "2014-03-02 16:24:08"; + +$i = DateTimeImmutable::createFromInterface( date_create( $current ) ); +var_dump( $i ); + +$i = DateTimeImmutable::createFromInterface( date_create_immutable( $current ) ); +var_dump( $i ); + +$current = "2019-12-16 15:06:46 CET"; + +$i = DateTimeImmutable::createFromInterface( date_create( $current ) ); +var_dump( $i ); + +$i = DateTimeImmutable::createFromInterface( date_create_immutable( $current ) ); +var_dump( $i ); + +$current = "2019-12-16 15:08:20 +0100"; + +$i = DateTimeImmutable::createFromInterface( date_create( $current ) ); +var_dump( $i ); + +$i = DateTimeImmutable::createFromInterface( date_create_immutable( $current ) ); +var_dump( $i ); +?> +--EXPECTF-- +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} +object(DateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} diff --git a/ext/date/tests/DateTime_createFromInterface.phpt b/ext/date/tests/DateTime_createFromInterface.phpt new file mode 100644 index 0000000000..a1ad5a3784 --- /dev/null +++ b/ext/date/tests/DateTime_createFromInterface.phpt @@ -0,0 +1,79 @@ +--TEST-- +Tests for DateTime::createFromInterface +--INI-- +date.timezone=Europe/London +--FILE-- +<?php +$current = "2014-03-02 16:24:08"; + +$i = DateTime::createFromInterface( date_create( $current ) ); +var_dump( $i ); + +$i = DateTime::createFromInterface( date_create_immutable( $current ) ); +var_dump( $i ); + +$current = "2019-12-16 15:06:46 CET"; + +$i = DateTime::createFromInterface( date_create( $current ) ); +var_dump( $i ); + +$i = DateTime::createFromInterface( date_create_immutable( $current ) ); +var_dump( $i ); + +$current = "2019-12-16 15:08:20 +0100"; + +$i = DateTime::createFromInterface( date_create( $current ) ); +var_dump( $i ); + +$i = DateTime::createFromInterface( date_create_immutable( $current ) ); +var_dump( $i ); +?> +--EXPECTF-- +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} +object(DateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} |