Skip to content

PHPC-2286 Implement UTCDateTime::toDateTimeImmutable #1611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions src/BSON/UTCDateTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -211,23 +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);
php_phongo_utcdatetime_to_php_date(return_value, getThis(), php_date_get_date_ce());
}

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

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)
Expand Down
2 changes: 2 additions & 0 deletions src/BSON/UTCDateTime.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
7 changes: 6 additions & 1 deletion src/BSON/UTCDateTime_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/bson/bson-utcdatetime-todatetime-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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===
<?php exit(0); ?>
--EXPECT--
string(8) "DateTime"
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
===DONE===
2 changes: 2 additions & 0 deletions tests/bson/bson-utcdatetime-todatetime-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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===
<?php exit(0); ?>
--EXPECT--
string(8) "DateTime"
1416445411.987000
===DONE===
19 changes: 19 additions & 0 deletions tests/bson/bson-utcdatetime-todatetimeimmutable-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
MongoDB\BSON\UTCDateTime::toDateTimeImmutable()
--INI--
date.timezone=America/Los_Angeles
--FILE--
<?php

$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987");
$datetime = $utcdatetime->toDateTimeImmutable();
var_dump(get_class($datetime));
var_dump($datetime->format(DATE_RSS));

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
string(17) "DateTimeImmutable"
string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
===DONE===
19 changes: 19 additions & 0 deletions tests/bson/bson-utcdatetime-todatetimeimmutable-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
MongoDB\BSON\UTCDateTime::toDateTimeImmutable() dumping seconds and microseconds
--INI--
date.timezone=UTC
--FILE--
<?php

$utcdatetime = new MongoDB\BSON\UTCDateTime("1416445411987");
$datetime = $utcdatetime->toDateTimeImmutable();
var_dump(get_class($datetime));
echo $datetime->format('U.u'), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
string(17) "DateTimeImmutable"
1416445411.987000
===DONE===
Loading