Skip to content

Commit dd6afab

Browse files
committed
Implement UTCDateTime::toDateTimeImmutable
1 parent 03feb57 commit dd6afab

7 files changed

+72
-1
lines changed

src/BSON/UTCDateTime.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,28 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime)
230230
datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
231231
}
232232

233+
/* Returns a DateTime object representing this UTCDateTime */
234+
static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTimeImmutable)
235+
{
236+
php_phongo_utcdatetime_t* intern;
237+
php_date_obj* datetime_obj;
238+
char* sec;
239+
size_t sec_len;
240+
241+
intern = Z_UTCDATETIME_OBJ_P(getThis());
242+
243+
PHONGO_PARSE_PARAMETERS_NONE();
244+
245+
object_init_ex(return_value, php_date_get_immutable_ce());
246+
datetime_obj = Z_PHPDATE_P(return_value);
247+
248+
sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000);
249+
php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0);
250+
efree(sec);
251+
252+
datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
253+
}
254+
233255
static PHP_METHOD(MongoDB_BSON_UTCDateTime, jsonSerialize)
234256
{
235257
php_phongo_utcdatetime_t* intern;

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)