Google-cloud-php: Spanner: select Timestamp with non-UTC timezone

Created on 23 Mar 2018  路  2Comments  路  Source: googleapis/google-cloud-php

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.

Test code

// 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'));

Result

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'

Proposal for fix

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()));

spanner question

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Google-K picture Google-K  路  6Comments

LoekvanKooten picture LoekvanKooten  路  6Comments

castaneai picture castaneai  路  7Comments

codeflorist picture codeflorist  路  5Comments

highstrike picture highstrike  路  5Comments