Skip to content
Closed
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
7 changes: 4 additions & 3 deletions src/BSON/UTCDateTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,12 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime)
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);
/* Initialize a DateTime using "Unix Timestamp with microseconds" notation.
* PHP 7.4 expects exactly six points of precision to denote microseconds.
* PHP 8.0+ accepts between zero and six points of precision. */
sec_len = spprintf(&sec, 0, "@%" PRId64 ".%.6d", intern->milliseconds / 1000, abs((int) (intern->milliseconds % 1000) * 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)
Expand Down
22 changes: 22 additions & 0 deletions tests/bson/bson-utcdatetime-todatetime-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
MongoDB\BSON\UTCDateTime::toDateTime() with dates before the Unix epoch
--INI--
date.timezone=UTC
--FILE--
<?php

$date = new \DateTimeImmutable('1960-01-01 12:12:12.1');
echo $date->format('Y-m-d H:i:s.u'), PHP_EOL;

$utcdatetime = new MongoDB\BSON\UTCDateTime($date);

$newDate = $utcdatetime->toDateTime();
echo $newDate->format('Y-m-d H:i:s.u'), PHP_EOL;

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
1960-01-01 12:12:12.100000
1960-01-01 12:12:12.100000
===DONE===