Fix inconsistent timestamp json encode/decode

pull/12396/head
Maksym Kindratko 2 years ago
parent 02cdbd7202
commit df47c96567
  1. 4
      php/src/Google/Protobuf/Internal/GPBUtil.php
  2. 10
      php/tests/EncodeDecodeTest.php

@ -485,6 +485,10 @@ class GPBUtil
$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);

@ -992,6 +992,16 @@ class EncodeDecodeTest extends TestBase
$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();

Loading…
Cancel
Save