Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def _pb_timestamp_to_datetime(timestamp_pb):
:returns: A UTC datetime object converted from a protobuf timestamp.
"""
return _EPOCH + datetime.timedelta(
seconds=timestamp_pb.seconds, microseconds=(timestamp_pb.nanos / 1000.0)
seconds=timestamp_pb.seconds, microseconds=(timestamp_pb.nanos // 1000)
)


Expand Down
17 changes: 17 additions & 0 deletions packages/google-cloud-core/tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,23 @@ def test_it(self):
timestamp = Timestamp(seconds=61, nanos=1234000)
self.assertEqual(self._call_fut(timestamp), dt_stamp)

def test_nanos_are_truncated_not_rounded(self):
import datetime
from google.protobuf.timestamp_pb2 import Timestamp
from google.cloud._helpers import UTC

timestamp = Timestamp(seconds=0, nanos=123456789)
expected = datetime.datetime(
1970, 1, 1, 0, 0, 0, microsecond=123456, tzinfo=UTC
)
self.assertEqual(self._call_fut(timestamp), expected)

timestamp = Timestamp(seconds=0, nanos=999999999)
expected = datetime.datetime(
1970, 1, 1, 0, 0, 0, microsecond=999999, tzinfo=UTC
)
self.assertEqual(self._call_fut(timestamp), expected)


class Test__from_any_pb(unittest.TestCase):
def _call_fut(self, pb_type, any_pb):
Expand Down
Loading