On Cloud Spanner RPC API, UTC timezone is used and represented as format ends with "Z", But on PHP client library, ValueMapperTrait uses \DateTimeImmutable::createFromFormat and may ignore "Z" in timestamp string from gRPC.
// Asia/Tokyo GMT+09:00
date_default_timezone_set('Asia/Tokyo');
/** @var Timestamp $ts */
$ts = $db->execute('SELECT TIMESTAMP("2018-01-01T00:00:00.000000Z")')->rows()->current()[0];
// OK
$this->assertEquals('Asia/Tokyo', $ts->get()->getTimezone()->getName());
// NG
$this->assertEquals('2018-01-01 09:00:00.000000+09:00', $ts->get()->format('Y-m-d H:i:s.uP'));
Failed asserting that two strings are equal.
Expected :'2018-01-01 09:00:00.000000+09:00'
Actual :'2018-01-01 00:00:00.000000+09:00'
class Timestamp
{
- const FORMAT = 'Y-m-d\TH:i:s.u\Z';
+ const FORMAT = 'Y-m-d\TH:i:s.uP';
$dt = \DateTimeImmutable::createFromFormat(Timestamp::FORMAT, str_replace('..', '.', $timestamp));
+ $dt = $dt->setTimezone(new \DateTimeZone(date_default_timezone_get()));
Hi @castaneai, thank you for bringing this up. We've taken note of it, and I'm working on figuring out how to proceed in the most correct fashion.
This was fixed in #1002 and will be included in the next release.
Most helpful comment
Hi @castaneai, thank you for bringing this up. We've taken note of it, and I'm working on figuring out how to proceed in the most correct fashion.