Skip to content

Commit 0ab1737

Browse files
authored
PHPC-2286 Implement UTCDateTime::toDateTimeImmutable (#1611)
* Implement UTCDateTime::toDateTimeImmutable * Refactor UTCDateTime::toDateTime functions
1 parent 21dde8d commit 0ab1737

7 files changed

+76
-14
lines changed

src/BSON/UTCDateTime.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ static HashTable* php_phongo_utcdatetime_get_properties_hash(phongo_compat_objec
126126
return props;
127127
}
128128

129+
static void php_phongo_utcdatetime_to_php_date(zval* return_value, const zval* this, zend_class_entry* ce)
130+
{
131+
php_phongo_utcdatetime_t* intern;
132+
php_date_obj* datetime_obj;
133+
char* sec;
134+
size_t sec_len;
135+
136+
intern = Z_UTCDATETIME_OBJ_P(this);
137+
138+
object_init_ex(return_value, ce);
139+
datetime_obj = Z_PHPDATE_P(return_value);
140+
141+
sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000);
142+
php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0);
143+
efree(sec);
144+
145+
datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
146+
}
147+
129148
/* Construct a new BSON UTCDateTime type from either the current time,
130149
milliseconds since the epoch, or a DateTimeInterface object. Defaults to the
131150
current time. */
@@ -211,23 +230,17 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __toString)
211230
/* Returns a DateTime object representing this UTCDateTime */
212231
static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime)
213232
{
214-
php_phongo_utcdatetime_t* intern;
215-
php_date_obj* datetime_obj;
216-
char* sec;
217-
size_t sec_len;
218-
219-
intern = Z_UTCDATETIME_OBJ_P(getThis());
220-
221233
PHONGO_PARSE_PARAMETERS_NONE();
222234

223-
object_init_ex(return_value, php_date_get_date_ce());
224-
datetime_obj = Z_PHPDATE_P(return_value);
235+
php_phongo_utcdatetime_to_php_date(return_value, getThis(), php_date_get_date_ce());
236+
}
225237

226-
sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000);
227-
php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0);
228-
efree(sec);
238+
/* Returns a DateTimeImmutable object representing this UTCDateTime */
239+
static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTimeImmutable)
240+
{
241+
PHONGO_PARSE_PARAMETERS_NONE();
229242

230-
datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
243+
php_phongo_utcdatetime_to_php_date(return_value, getThis(), php_date_get_immutable_ce());
231244
}
232245

233246
static PHP_METHOD(MongoDB_BSON_UTCDateTime, jsonSerialize)

src/BSON/UTCDateTime.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ final public function __construct($milliseconds = null) {}
1818

1919
final public function toDateTime(): \DateTime {}
2020

21+
final public function toDateTimeImmutable(): \DateTimeImmutable {}
22+
2123
final public function __toString(): string {}
2224

2325
final public static function __set_state(array $properties): UTCDateTime {}

src/BSON/UTCDateTime_arginfo.h

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson/bson-utcdatetime-todatetime-001.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ date.timezone=America/Los_Angeles
77

88
$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987");
99
$datetime = $utcdatetime->toDateTime();
10+
var_dump(get_class($datetime));
1011
var_dump($datetime->format(DATE_RSS));
1112

1213
?>
1314
===DONE===
1415
<?php exit(0); ?>
1516
--EXPECT--
17+
string(8) "DateTime"
1618
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
1719
===DONE===

tests/bson/bson-utcdatetime-todatetime-002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ date.timezone=UTC
77

88
$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987");
99
$datetime = $utcdatetime->toDateTime();
10+
var_dump(get_class($datetime));
1011
echo $datetime->format('U.u'), "\n";
1112

1213
?>
1314
===DONE===
1415
<?php exit(0); ?>
1516
--EXPECT--
17+
string(8) "DateTime"
1618
1416445411.987000
1719
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime::toDateTimeImmutable()
3+
--INI--
4+
date.timezone=America/Los_Angeles
5+
--FILE--
6+
<?php
7+
8+
$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987");
9+
$datetime = $utcdatetime->toDateTimeImmutable();
10+
var_dump(get_class($datetime));
11+
var_dump($datetime->format(DATE_RSS));
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
string(17) "DateTimeImmutable"
18+
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
19+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime::toDateTimeImmutable() dumping seconds and microseconds
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
8+
$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987");
9+
$datetime = $utcdatetime->toDateTimeImmutable();
10+
var_dump(get_class($datetime));
11+
echo $datetime->format('U.u'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
string(17) "DateTimeImmutable"
18+
1416445411.987000
19+
===DONE===

0 commit comments

Comments
 (0)