Skip to content

Commit

Permalink
Fix inconsistent timestamp json encode/decode
Browse files Browse the repository at this point in the history
  • Loading branch information
kindratmakc committed Apr 3, 2023
1 parent 02cdbd7 commit df47c96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions php/src/Google/Protobuf/Internal/GPBUtil.php
Expand Up @@ -485,6 +485,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);
Expand Down
10 changes: 10 additions & 0 deletions php/tests/EncodeDecodeTest.php
Expand Up @@ -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();
Expand Down

0 comments on commit df47c96

Please sign in to comment.