Skip to content

Commit 539f145

Browse files
authored
fix: use datetime timezone info when generating timestamp strings (#236)
* fix: use datetime timezone when generating timestamp strings * style: fix lint Co-authored-by: larkee <[email protected]>
1 parent 0375914 commit 539f145

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

google/cloud/spanner_v1/_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _make_value_pb(value):
118118
if isinstance(value, datetime_helpers.DatetimeWithNanoseconds):
119119
return Value(string_value=value.rfc3339())
120120
if isinstance(value, datetime.datetime):
121-
return Value(string_value=_datetime_to_rfc3339(value))
121+
return Value(string_value=_datetime_to_rfc3339(value, ignore_zone=False))
122122
if isinstance(value, datetime.date):
123123
return Value(string_value=value.isoformat())
124124
if isinstance(value, six.binary_type):

tests/unit/test__helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ def test_w_datetime(self):
215215
self.assertIsInstance(value_pb, Value)
216216
self.assertEqual(value_pb.string_value, datetime_helpers.to_rfc3339(now))
217217

218+
def test_w_timestamp_w_tz(self):
219+
import datetime
220+
import pytz
221+
from google.protobuf.struct_pb2 import Value
222+
223+
when = datetime.datetime(
224+
2021, 2, 8, 0, 0, 0, tzinfo=pytz.timezone("US/Mountain")
225+
)
226+
value_pb = self._callFUT(when)
227+
self.assertIsInstance(value_pb, Value)
228+
self.assertEqual(value_pb.string_value, "2021-02-08T07:00:00.000000Z")
229+
218230
def test_w_numeric(self):
219231
import decimal
220232
from google.protobuf.struct_pb2 import Value

0 commit comments

Comments
 (0)