Skip to content

Commit

Permalink
PHPC-2429: Fix UTCDateTime with negative timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Aug 28, 2024
1 parent f47915e commit 62833b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/BSON/UTCDateTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,9 @@ 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);
sec_len = spprintf(&sec, 0, "@%.6f", (double) 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)
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===

0 comments on commit 62833b4

Please sign in to comment.