From 792bb1942ec1772bd3ae0f594d6467d2134c9ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 17 Jul 2024 11:06:15 +0200 Subject: [PATCH 1/2] Implement UTCDateTime::toDateTimeImmutable --- src/BSON/UTCDateTime.c | 22 +++++++++++++++++++ src/BSON/UTCDateTime.stub.php | 2 ++ src/BSON/UTCDateTime_arginfo.h | 7 +++++- .../bson/bson-utcdatetime-todatetime-001.phpt | 2 ++ .../bson/bson-utcdatetime-todatetime-002.phpt | 2 ++ ...n-utcdatetime-todatetimeimmutable-001.phpt | 19 ++++++++++++++++ ...n-utcdatetime-todatetimeimmutable-002.phpt | 19 ++++++++++++++++ 7 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt create mode 100644 tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index c9f8d1eb4..593e87e2a 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -230,6 +230,28 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime) datetime_obj->time->us = (intern->milliseconds % 1000) * 1000; } +/* Returns a DateTime object representing this UTCDateTime */ +static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTimeImmutable) +{ + php_phongo_utcdatetime_t* intern; + php_date_obj* datetime_obj; + char* sec; + size_t sec_len; + + intern = Z_UTCDATETIME_OBJ_P(getThis()); + + PHONGO_PARSE_PARAMETERS_NONE(); + + object_init_ex(return_value, php_date_get_immutable_ce()); + datetime_obj = Z_PHPDATE_P(return_value); + + sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000); + php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0); + efree(sec); + + datetime_obj->time->us = (intern->milliseconds % 1000) * 1000; +} + static PHP_METHOD(MongoDB_BSON_UTCDateTime, jsonSerialize) { php_phongo_utcdatetime_t* intern; diff --git a/src/BSON/UTCDateTime.stub.php b/src/BSON/UTCDateTime.stub.php index d2aa3d80c..ac9143a33 100644 --- a/src/BSON/UTCDateTime.stub.php +++ b/src/BSON/UTCDateTime.stub.php @@ -18,6 +18,8 @@ final public function __construct($milliseconds = null) {} final public function toDateTime(): \DateTime {} + final public function toDateTimeImmutable(): \DateTimeImmutable {} + final public function __toString(): string {} final public static function __set_state(array $properties): UTCDateTime {} diff --git a/src/BSON/UTCDateTime_arginfo.h b/src/BSON/UTCDateTime_arginfo.h index 04fd59cd3..df008acda 100644 --- a/src/BSON/UTCDateTime_arginfo.h +++ b/src/BSON/UTCDateTime_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: c4d05eaa04380f7a4adcb8f9c1562d7013c37121 */ + * Stub hash: 2b7ac84585a74a210af3cb2061541977cc309e2d */ #if PHP_VERSION_ID >= 80000 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_UTCDateTime___construct, 0, 0, 0) @@ -16,6 +16,9 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_MongoDB_BSON_UTCDateTime_toDateTime, 0, 0, DateTime, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_MongoDB_BSON_UTCDateTime_toDateTimeImmutable, 0, 0, DateTimeImmutable, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_MongoDB_BSON_UTCDateTime___toString, 0, 0, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -62,6 +65,7 @@ static ZEND_METHOD(MongoDB_BSON_UTCDateTime, __construct); static ZEND_METHOD(MongoDB_BSON_UTCDateTime, __construct); #endif static ZEND_METHOD(MongoDB_BSON_UTCDateTime, toDateTime); +static ZEND_METHOD(MongoDB_BSON_UTCDateTime, toDateTimeImmutable); static ZEND_METHOD(MongoDB_BSON_UTCDateTime, __toString); static ZEND_METHOD(MongoDB_BSON_UTCDateTime, __set_state); static ZEND_METHOD(MongoDB_BSON_UTCDateTime, serialize); @@ -89,6 +93,7 @@ static const zend_function_entry class_MongoDB_BSON_UTCDateTime_methods[] = { ZEND_ME(MongoDB_BSON_UTCDateTime, __construct, arginfo_class_MongoDB_BSON_UTCDateTime___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) #endif ZEND_ME(MongoDB_BSON_UTCDateTime, toDateTime, arginfo_class_MongoDB_BSON_UTCDateTime_toDateTime, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + ZEND_ME(MongoDB_BSON_UTCDateTime, toDateTimeImmutable, arginfo_class_MongoDB_BSON_UTCDateTime_toDateTimeImmutable, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) ZEND_ME(MongoDB_BSON_UTCDateTime, __toString, arginfo_class_MongoDB_BSON_UTCDateTime___toString, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) ZEND_ME(MongoDB_BSON_UTCDateTime, __set_state, arginfo_class_MongoDB_BSON_UTCDateTime___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) ZEND_ME(MongoDB_BSON_UTCDateTime, serialize, arginfo_class_MongoDB_BSON_UTCDateTime_serialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) diff --git a/tests/bson/bson-utcdatetime-todatetime-001.phpt b/tests/bson/bson-utcdatetime-todatetime-001.phpt index a0fdf878b..4c34167c5 100644 --- a/tests/bson/bson-utcdatetime-todatetime-001.phpt +++ b/tests/bson/bson-utcdatetime-todatetime-001.phpt @@ -7,11 +7,13 @@ date.timezone=America/Los_Angeles $utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); $datetime = $utcdatetime->toDateTime(); +var_dump(get_class($datetime)); var_dump($datetime->format(DATE_RSS)); ?> ===DONE=== --EXPECT-- +string(8) "DateTime" string(31) "Thu, 20 Nov 2014 01:03:31 +0000" ===DONE=== diff --git a/tests/bson/bson-utcdatetime-todatetime-002.phpt b/tests/bson/bson-utcdatetime-todatetime-002.phpt index 6afd62b41..e003cd188 100644 --- a/tests/bson/bson-utcdatetime-todatetime-002.phpt +++ b/tests/bson/bson-utcdatetime-todatetime-002.phpt @@ -7,11 +7,13 @@ date.timezone=UTC $utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987"); $datetime = $utcdatetime->toDateTime(); +var_dump(get_class($datetime)); echo $datetime->format('U.u'), "\n"; ?> ===DONE=== --EXPECT-- +string(8) "DateTime" 1416445411.987000 ===DONE=== diff --git a/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt b/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt new file mode 100644 index 000000000..4d7bd8385 --- /dev/null +++ b/tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt @@ -0,0 +1,19 @@ +--TEST-- +MongoDB\BSON\UTCDateTime::toDateTimeImmutable() +--INI-- +date.timezone=America/Los_Angeles +--FILE-- +toDateTimeImmutable(); +var_dump(get_class($datetime)); +var_dump($datetime->format(DATE_RSS)); + +?> +===DONE=== + +--EXPECT-- +string(17) "DateTimeImmutable" +string(31) "Thu, 20 Nov 2014 01:03:31 +0000" +===DONE=== diff --git a/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt b/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt new file mode 100644 index 000000000..b959d0bc1 --- /dev/null +++ b/tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt @@ -0,0 +1,19 @@ +--TEST-- +MongoDB\BSON\UTCDateTime::toDateTimeImmutable() dumping seconds and microseconds +--INI-- +date.timezone=UTC +--FILE-- +toDateTimeImmutable(); +var_dump(get_class($datetime)); +echo $datetime->format('U.u'), "\n"; + +?> +===DONE=== + +--EXPECT-- +string(17) "DateTimeImmutable" +1416445411.987000 +===DONE=== From 93ec0ce153620be0af357f40a76f3f0214d3a78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 17 Jul 2024 11:15:43 +0200 Subject: [PATCH 2/2] Refactor UTCDateTime::toDateTime functions --- src/BSON/UTCDateTime.c | 53 ++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/src/BSON/UTCDateTime.c b/src/BSON/UTCDateTime.c index 593e87e2a..dab962051 100644 --- a/src/BSON/UTCDateTime.c +++ b/src/BSON/UTCDateTime.c @@ -126,6 +126,25 @@ static HashTable* php_phongo_utcdatetime_get_properties_hash(phongo_compat_objec return props; } +static void php_phongo_utcdatetime_to_php_date(zval* return_value, const zval* this, zend_class_entry* ce) +{ + php_phongo_utcdatetime_t* intern; + php_date_obj* datetime_obj; + char* sec; + size_t sec_len; + + intern = Z_UTCDATETIME_OBJ_P(this); + + object_init_ex(return_value, ce); + datetime_obj = Z_PHPDATE_P(return_value); + + sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000); + php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0); + efree(sec); + + datetime_obj->time->us = (intern->milliseconds % 1000) * 1000; +} + /* Construct a new BSON UTCDateTime type from either the current time, milliseconds since the epoch, or a DateTimeInterface object. Defaults to the current time. */ @@ -211,45 +230,17 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __toString) /* Returns a DateTime object representing this UTCDateTime */ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime) { - php_phongo_utcdatetime_t* intern; - php_date_obj* datetime_obj; - char* sec; - size_t sec_len; - - intern = Z_UTCDATETIME_OBJ_P(getThis()); - PHONGO_PARSE_PARAMETERS_NONE(); - object_init_ex(return_value, php_date_get_date_ce()); - datetime_obj = Z_PHPDATE_P(return_value); - - sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000); - php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0); - efree(sec); - - datetime_obj->time->us = (intern->milliseconds % 1000) * 1000; + php_phongo_utcdatetime_to_php_date(return_value, getThis(), php_date_get_date_ce()); } -/* Returns a DateTime object representing this UTCDateTime */ +/* Returns a DateTimeImmutable object representing this UTCDateTime */ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTimeImmutable) { - php_phongo_utcdatetime_t* intern; - php_date_obj* datetime_obj; - char* sec; - size_t sec_len; - - intern = Z_UTCDATETIME_OBJ_P(getThis()); - PHONGO_PARSE_PARAMETERS_NONE(); - object_init_ex(return_value, php_date_get_immutable_ce()); - datetime_obj = Z_PHPDATE_P(return_value); - - sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000); - php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0); - efree(sec); - - datetime_obj->time->us = (intern->milliseconds % 1000) * 1000; + php_phongo_utcdatetime_to_php_date(return_value, getThis(), php_date_get_immutable_ce()); } static PHP_METHOD(MongoDB_BSON_UTCDateTime, jsonSerialize)