Skip to content

Commit

Permalink
Rename strptime format constant:
Browse files Browse the repository at this point in the history
- Ref. correct RFC.
- Private
- Add '_MICROS' to indicate which format from the sheaf allowed by the RFC.

Addresses points 1 and 2 from:
#816 (comment).

and:

#816 (comment)
  • Loading branch information
tseaver committed Apr 10, 2015
1 parent 492917b commit ddb06db
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions gcloud/pubsub/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pytz

RFC3369 = '%Y-%m-%dT%H:%M:%S.%fZ'
_RFC3339_MICROS = '%Y-%m-%dT%H:%M:%S.%fZ'


class Message(object):
Expand Down Expand Up @@ -54,13 +54,13 @@ def timestamp(self):
"""Return timestamp from attributes, if passed.
:rtype: datetime
:returns: timestamp (in UTC timezone) parsed from RFC 3369 timestamp
:returns: timestamp (in UTC timezone) parsed from RFC 3339 timestamp
:raises: ValueError if timestamp not in ``attributes``, or malformed
"""
stamp = self.attributes.get('timestamp')
if stamp is None:
raise ValueError('No timestamp')
return datetime.datetime.strptime(stamp, RFC3369).replace(
return datetime.datetime.strptime(stamp, _RFC3339_MICROS).replace(
tzinfo=pytz.UTC)

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions gcloud/pubsub/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ def _to_fail():

def test_timestamp_w_timestamp_in_attributes(self):
from datetime import datetime
import pytz
from pytz import utc
from gcloud.pubsub.message import _RFC3339_MICROS
DATA = b'DEADBEEF'
MESSAGE_ID = b'12345'
TIMESTAMP = '2015-04-10T18:42:27.131956Z'
RFC3369 = '%Y-%m-%dT%H:%M:%S.%fZ'
naive = datetime.strptime(TIMESTAMP, RFC3369)
timestamp = naive.replace(tzinfo=pytz.utc)
naive = datetime.strptime(TIMESTAMP, _RFC3339_MICROS)
timestamp = naive.replace(tzinfo=utc)
ATTRS = {'timestamp': TIMESTAMP}
message = self._makeOne(data=DATA, message_id=MESSAGE_ID,
attributes=ATTRS)
Expand Down

0 comments on commit ddb06db

Please sign in to comment.