Skip to content

Commit 62833b4

Browse files
committed
PHPC-2429: Fix UTCDateTime with negative timestamps
1 parent f47915e commit 62833b4

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/BSON/UTCDateTime.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,9 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime)
223223
object_init_ex(return_value, php_date_get_date_ce());
224224
datetime_obj = Z_PHPDATE_P(return_value);
225225

226-
sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000);
226+
sec_len = spprintf(&sec, 0, "@%.6f", (double) intern->milliseconds / 1000);
227227
php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0);
228228
efree(sec);
229-
230-
datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
231229
}
232230

233231
static PHP_METHOD(MongoDB_BSON_UTCDateTime, jsonSerialize)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime::toDateTime() with dates before the Unix epoch
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
8+
$date = new \DateTimeImmutable('1960-01-01 12:12:12.1');
9+
echo $date->format('Y-m-d H:i:s.u'), PHP_EOL;
10+
11+
$utcdatetime = new MongoDB\BSON\UTCDateTime($date);
12+
13+
$newDate = $utcdatetime->toDateTime();
14+
echo $newDate->format('Y-m-d H:i:s.u'), PHP_EOL;
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
1960-01-01 12:12:12.100000
21+
1960-01-01 12:12:12.100000
22+
===DONE===

0 commit comments

Comments
 (0)