Skip to content

Commit

Permalink
Make 'Message.publish_time' return a datetime.
Browse files Browse the repository at this point in the history
Rather than a 'google.protobuf.timestamp_pb2.Timestamp' instance.

Closes #5598.
  • Loading branch information
tseaver committed Jul 16, 2018
1 parent c2e7318 commit 9d4ab5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
8 changes: 7 additions & 1 deletion pubsub/google/cloud/pubsub_v1/subscriber/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

from __future__ import absolute_import

import datetime
import json
import math
import time

from google.api_core import datetime_helpers
from google.cloud.pubsub_v1.subscriber._protocol import requests


Expand Down Expand Up @@ -151,7 +153,11 @@ def publish_time(self):
Returns:
datetime: The date and time that the message was published.
"""
return self._message.publish_time
timestamp = self._message.publish_time
delta = datetime.timedelta(
seconds=timestamp.seconds,
microseconds=timestamp.nanos // 1000)
return datetime_helpers._UTC_EPOCH + delta

@property
def size(self):
Expand Down
22 changes: 13 additions & 9 deletions pubsub/tests/unit/pubsub_v1/subscriber/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import mock
import pytz
from six.moves import queue
from google.protobuf import timestamp_pb2

from google.api_core import datetime_helpers
from google.cloud.pubsub_v1 import types
Expand All @@ -27,7 +28,9 @@

RECEIVED = datetime.datetime(2012, 4, 21, 15, 0, tzinfo=pytz.utc)
RECEIVED_SECONDS = datetime_helpers.to_milliseconds(RECEIVED) // 1000
PUBLISHED = RECEIVED + datetime.timedelta(days=1)
PUBLISHED_MICROS = 123456
PUBLISHED = RECEIVED + datetime.timedelta(
days=1, microseconds=PUBLISHED_MICROS)
PUBLISHED_SECONDS = datetime_helpers.to_milliseconds(PUBLISHED) // 1000


Expand All @@ -39,8 +42,10 @@ def create_message(data, ack_id='ACKID', **attrs):
attributes=attrs,
data=data,
message_id='message_id',
publish_time=types.Timestamp(
seconds=PUBLISHED_SECONDS),
publish_time=timestamp_pb2.Timestamp(
seconds=PUBLISHED_SECONDS,
nanos=PUBLISHED_MICROS * 1000,
),
), ack_id, queue.Queue())
lease.assert_called_once_with()
return msg
Expand All @@ -58,8 +63,7 @@ def test_data():

def test_publish_time():
msg = create_message(b'foo')
assert msg.publish_time == types.Timestamp(
seconds=PUBLISHED_SECONDS)
assert msg.publish_time == PUBLISHED


def check_call_types(mock, *args, **kwargs):
Expand Down Expand Up @@ -91,7 +95,7 @@ def test_ack():
msg.ack()
put.assert_called_once_with(requests.AckRequest(
ack_id='bogus_ack_id',
byte_size=25,
byte_size=30,
time_to_ack=mock.ANY,
))
check_call_types(put, requests.AckRequest)
Expand All @@ -103,7 +107,7 @@ def test_drop():
msg.drop()
put.assert_called_once_with(requests.DropRequest(
ack_id='bogus_ack_id',
byte_size=25,
byte_size=30,
))
check_call_types(put, requests.DropRequest)

Expand All @@ -114,7 +118,7 @@ def test_lease():
msg.lease()
put.assert_called_once_with(requests.LeaseRequest(
ack_id='bogus_ack_id',
byte_size=25,
byte_size=30,
))
check_call_types(put, requests.LeaseRequest)

Expand All @@ -136,7 +140,7 @@ def test_nack():
msg.nack()
put.assert_called_once_with(requests.NackRequest(
ack_id='bogus_ack_id',
byte_size=25,
byte_size=30,
))
check_call_types(put, requests.NackRequest)

Expand Down

0 comments on commit 9d4ab5f

Please sign in to comment.