diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php index cac2a4a4b3ec..4c1fadcd0301 100644 --- a/php/src/Google/Protobuf/Internal/GPBUtil.php +++ b/php/src/Google/Protobuf/Internal/GPBUtil.php @@ -462,6 +462,10 @@ public static function parseTimestamp($timestamp) $nanoseconds = substr($timestamp, $periodIndex + 1, $nanosecondsLength); $nanoseconds = intval($nanoseconds); + if ($nanosecondsLength < 9) { + $nanoseconds = $nanoseconds * pow(10, 9 - $nanosecondsLength); + } + // remove the nanoseconds and preceding period from the timestamp $date = substr($timestamp, 0, $periodIndex); $timezone = substr($timestamp, $periodIndex + $nanosecondsLength + 1); diff --git a/php/tests/EncodeDecodeTest.php b/php/tests/EncodeDecodeTest.php index 113f200afee7..276528d26e9a 100644 --- a/php/tests/EncodeDecodeTest.php +++ b/php/tests/EncodeDecodeTest.php @@ -992,6 +992,16 @@ public function testEncodeTimestamp() $m->serializeToJsonString()); } + public function testEncodeDecodeTimestampConsistency() + { + $m = new Google\Protobuf\Timestamp(); + $m->setSeconds(946684800); + $m->setNanos(123000000); + $m->mergeFromJsonString($m->serializeToJsonString()); + $this->assertEquals(946684800, $m->getSeconds()); + $this->assertEquals(123000000, $m->getNanos()); + } + public function testDecodeTopLevelValue() { $m = new Value();