diff --git a/src/Core/GrpcTrait.php b/src/Core/GrpcTrait.php index a3d28abf1332..355feeea0903 100644 --- a/src/Core/GrpcTrait.php +++ b/src/Core/GrpcTrait.php @@ -252,7 +252,7 @@ private function formatTimestampForApi($value) preg_match('/\.(\d{1,9})Z/', $value, $matches); $value = preg_replace('/\.(\d{1,9})Z/', '.000000Z', $value); $dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', $value); - $nanos = (isset($matches[1])) ? $matches[1] : 0; + $nanos = (isset($matches[1])) ? str_pad($matches[1], 9, '0') : 0; return [ 'seconds' => (int)$dt->format('U'), diff --git a/tests/unit/Core/GrpcTraitTest.php b/tests/unit/Core/GrpcTraitTest.php index c95d5329ce9d..511e01232181 100644 --- a/tests/unit/Core/GrpcTraitTest.php +++ b/tests/unit/Core/GrpcTraitTest.php @@ -213,16 +213,44 @@ public function testFormatsList() $this->assertEquals($expected, $this->implementation->call('formatListForApi', [$list])); } - public function testFormatTimestampForApi() + /** + * @dataProvider timestampProvider + */ + public function testFormatTimestampForApi($timestamp, $expectedSeconds, $expectedNanos) + { + $result = $this->implementation->call( + 'formatTimestampForApi', + [$timestamp] + ); + + $this->assertEquals($expectedSeconds, $result['seconds']); + $this->assertEquals($expectedNanos, $result['nanos']); + } + + public function timestampProvider() { - $seconds = '1491511965'; - $nanos = '989898989'; - $ts = '2017-04-06T20:52:45.'. $nanos .'Z'; - - $this->assertEquals([ - 'seconds' => $seconds, - 'nanos' => $nanos - ], $this->implementation->call('formatTimestampForApi', [$ts])); + return [ + [ + '2017-08-24T00:38:30.611529Z', + '1503535110', + '611529000' + ], + [ + '2017-08-24T00:38:30.000000000Z', + '1503535110', + '0' + ], + [ + '2017-08-24T00:38:30.000000001Z', + '1503535110', + '1' + ], + [ + '2017-04-06T20:52:45.989898989Z', + '1491511965', + '989898989' + ] + ]; } /**