Skip to content

Commit

Permalink
fix: use datetime timezone info when generating timestamp strings (#236)
Browse files Browse the repository at this point in the history
* fix: use datetime timezone when generating timestamp strings

* style: fix lint

Co-authored-by: larkee <larkee@users.noreply.github.com>
  • Loading branch information
larkee and larkee committed Feb 17, 2021
1 parent 0375914 commit 539f145
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion google/cloud/spanner_v1/_helpers.py
Expand Up @@ -118,7 +118,7 @@ def _make_value_pb(value):
if isinstance(value, datetime_helpers.DatetimeWithNanoseconds):
return Value(string_value=value.rfc3339())
if isinstance(value, datetime.datetime):
return Value(string_value=_datetime_to_rfc3339(value))
return Value(string_value=_datetime_to_rfc3339(value, ignore_zone=False))
if isinstance(value, datetime.date):
return Value(string_value=value.isoformat())
if isinstance(value, six.binary_type):
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test__helpers.py
Expand Up @@ -215,6 +215,18 @@ def test_w_datetime(self):
self.assertIsInstance(value_pb, Value)
self.assertEqual(value_pb.string_value, datetime_helpers.to_rfc3339(now))

def test_w_timestamp_w_tz(self):
import datetime
import pytz
from google.protobuf.struct_pb2 import Value

when = datetime.datetime(
2021, 2, 8, 0, 0, 0, tzinfo=pytz.timezone("US/Mountain")
)
value_pb = self._callFUT(when)
self.assertIsInstance(value_pb, Value)
self.assertEqual(value_pb.string_value, "2021-02-08T07:00:00.000000Z")

def test_w_numeric(self):
import decimal
from google.protobuf.struct_pb2 import Value
Expand Down

0 comments on commit 539f145

Please sign in to comment.