Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit 0618996

Browse files
committed
HHVM-158: Change JSON encoding for UTCDateTime to use a date string
1 parent d37680b commit 0618996

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

ext_mongodb.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,8 +1471,13 @@ public function serialize() : string
14711471

14721472
public function jsonSerialize() : mixed
14731473
{
1474+
$seconds = (int) ( $this->milliseconds / 1000 );
1475+
$millis = (int) ( $this->milliseconds % 1000 );
1476+
1477+
$d = date_create( "@" . (string) $seconds );
1478+
14741479
return [
1475-
'$date' => (int) $this->milliseconds
1480+
'$date' => sprintf( "%s.%03d+0000", $d->format( 'Y-m-d\TH:i:s' ), $millis )
14761481
];
14771482
}
14781483

tests/json-serialize-utcdatetime.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ JsonSerializable: UTCDateTime
33
--FILE--
44
<?php
55
$doc = [
6-
'foo' => new MongoDB\BSON\UTCDateTime( new \DateTime( '2016-10-11 13:34:26 UTC' ) )
6+
'foo' => new MongoDB\BSON\UTCDateTime( new \DateTime( '2016-10-11 13:34:26.817 UTC' ) )
77
];
88

99
echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
@@ -13,12 +13,12 @@ echo $d, "\n";
1313
var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
1414
?>
1515
--EXPECTF--
16-
{ "foo" : { "$date" : 1476192866000 } }
17-
{"foo":{"$date":1476192866000}}
16+
{ "foo" : { "$date" : 1476192866817 } }
17+
{"foo":{"$date":"2016-10-11T13:34:26.817+0000"}}
1818
object(stdClass)#%d (%d) {
1919
["foo"]=>
2020
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
2121
["milliseconds"]=>
22-
string(13) "1476192866000"
22+
string(13) "1476192866817"
2323
}
2424
}

0 commit comments

Comments
 (0)