Skip to content

Commit df47c96

Browse files
committed
Fix inconsistent timestamp json encode/decode
1 parent 02cdbd7 commit df47c96

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

php/src/Google/Protobuf/Internal/GPBUtil.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ public static function parseTimestamp($timestamp)
485485
$nanoseconds = substr($timestamp, $periodIndex + 1, $nanosecondsLength);
486486
$nanoseconds = intval($nanoseconds);
487487

488+
if ($nanosecondsLength < 9) {
489+
$nanoseconds = $nanoseconds * pow(10, 9 - $nanosecondsLength);
490+
}
491+
488492
// remove the nanoseconds and preceding period from the timestamp
489493
$date = substr($timestamp, 0, $periodIndex);
490494
$timezone = substr($timestamp, $periodIndex + $nanosecondsLength + 1);

php/tests/EncodeDecodeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,16 @@ public function testEncodeTimestamp()
992992
$m->serializeToJsonString());
993993
}
994994

995+
public function testEncodeDecodeTimestampConsistency()
996+
{
997+
$m = new Google\Protobuf\Timestamp();
998+
$m->setSeconds(946684800);
999+
$m->setNanos(123000000);
1000+
$m->mergeFromJsonString($m->serializeToJsonString());
1001+
$this->assertEquals(946684800, $m->getSeconds());
1002+
$this->assertEquals(123000000, $m->getNanos());
1003+
}
1004+
9951005
public function testDecodeTopLevelValue()
9961006
{
9971007
$m = new Value();

0 commit comments

Comments
 (0)