Skip to content

Commit 286207b

Browse files
committed
Merge pull request #835 from tseaver/818-unify_rfc_3339_format
#818: unify RFC 3339 timestamp format strubg
2 parents 2c1e1ac + 1d35fe8 commit 286207b

File tree

9 files changed

+32
-24
lines changed

9 files changed

+32
-24
lines changed

gcloud/_helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Local(object):
3131
except ImportError:
3232
app_identity = None
3333

34+
_RFC3339_MICROS = '%Y-%m-%dT%H:%M:%S.%fZ'
35+
3436

3537
class _LocalStack(Local):
3638
"""Manage a thread-local LIFO stack of resources.

gcloud/pubsub/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import pytz
2121

22-
_RFC3339_MICROS = '%Y-%m-%dT%H:%M:%S.%fZ'
22+
from gcloud._helpers import _RFC3339_MICROS
2323

2424

2525
class Message(object):

gcloud/pubsub/test_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _to_fail():
9292
def test_timestamp_w_timestamp_in_attributes(self):
9393
from datetime import datetime
9494
from pytz import utc
95-
from gcloud.pubsub.message import _RFC3339_MICROS
95+
from gcloud._helpers import _RFC3339_MICROS
9696
DATA = b'DEADBEEF'
9797
MESSAGE_ID = b'12345'
9898
TIMESTAMP = '2015-04-10T18:42:27.131956Z'

gcloud/pubsub/test_topic.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def test_publish_single_bytes_wo_attrs_w_add_timestamp(self):
155155
import base64
156156
import datetime
157157
from gcloud.pubsub import topic as MUT
158+
from gcloud._helpers import _RFC3339_MICROS
158159
from gcloud._testing import _Monkey
159160
NOW = datetime.datetime.utcnow()
160161

@@ -167,7 +168,7 @@ def _utcnow():
167168
B64 = base64.b64encode(PAYLOAD).decode('ascii')
168169
MSGID = 'DEADBEEF'
169170
MESSAGE = {'data': B64,
170-
'attributes': {'timestamp': '%sZ' % NOW.isoformat()}}
171+
'attributes': {'timestamp': NOW.strftime(_RFC3339_MICROS)}}
171172
PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
172173
conn = _Connection({'messageIds': [MSGID]})
173174
topic = self._makeOne(TOPIC_NAME, project=PROJECT, connection=conn,
@@ -183,13 +184,6 @@ def _utcnow():
183184

184185
def test_publish_single_bytes_w_add_timestamp_w_ts_in_attrs(self):
185186
import base64
186-
import datetime
187-
from gcloud.pubsub import topic as MUT
188-
from gcloud._testing import _Monkey
189-
NOW = datetime.datetime.utcnow()
190-
191-
def _utcnow(): # pragma: NO COVER
192-
return NOW
193187

194188
TOPIC_NAME = 'topic_name'
195189
PROJECT = 'PROJECT'
@@ -203,8 +197,7 @@ def _utcnow(): # pragma: NO COVER
203197
conn = _Connection({'messageIds': [MSGID]})
204198
topic = self._makeOne(TOPIC_NAME, project=PROJECT, connection=conn,
205199
timestamp_messages=True)
206-
with _Monkey(MUT, _NOW=_utcnow):
207-
msgid = topic.publish(PAYLOAD, timestamp=OVERRIDE)
200+
msgid = topic.publish(PAYLOAD, timestamp=OVERRIDE)
208201
self.assertEqual(msgid, MSGID)
209202
self.assertEqual(len(conn._requested), 1)
210203
req = conn._requested[0]

gcloud/pubsub/topic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import datetime
1919

2020
from gcloud._helpers import get_default_project
21+
from gcloud._helpers import _RFC3339_MICROS
2122
from gcloud.exceptions import NotFound
2223
from gcloud.pubsub._implicit_environ import get_default_connection
2324

@@ -123,7 +124,7 @@ def publish(self, message, **attrs):
123124
:returns: message ID assigned by the server to the published message
124125
"""
125126
if self.timestamp_messages and 'timestamp' not in attrs:
126-
attrs['timestamp'] = '%sZ' % _NOW().isoformat()
127+
attrs['timestamp'] = _NOW().strftime(_RFC3339_MICROS)
127128
message_b = base64.b64encode(message).decode('ascii')
128129
message_data = {'data': message_b, 'attributes': attrs}
129130
data = {'messages': [message_data]}

gcloud/storage/blob.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import os
2323
import time
2424

25+
import pytz
2526
import six
2627
from six.moves.urllib.parse import quote # pylint: disable=F0401
2728

@@ -35,10 +36,10 @@
3536
from gcloud.storage._helpers import _scalar_property
3637
from gcloud.storage import _implicit_environ
3738
from gcloud.storage.acl import ObjectACL
39+
from gcloud._helpers import _RFC3339_MICROS
3840

3941

4042
_API_ACCESS_ENDPOINT = 'https://storage.googleapis.com'
41-
_GOOGLE_TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
4243

4344

4445
class Blob(_PropertyMixin):
@@ -749,7 +750,8 @@ def time_deleted(self):
749750
"""
750751
value = self._properties.get('timeDeleted')
751752
if value is not None:
752-
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)
753+
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
754+
return naive.replace(tzinfo=pytz.utc)
753755

754756
@property
755757
def updated(self):
@@ -763,7 +765,8 @@ def updated(self):
763765
"""
764766
value = self._properties.get('updated')
765767
if value is not None:
766-
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)
768+
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
769+
return naive.replace(tzinfo=pytz.utc)
767770

768771

769772
class _UploadConfig(object):

gcloud/storage/bucket.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import datetime
3737
import copy
3838
import os
39+
40+
import pytz
3941
import six
4042

4143
from gcloud._helpers import get_default_project
@@ -46,7 +48,7 @@
4648
from gcloud.storage.acl import DefaultObjectACL
4749
from gcloud.storage.iterator import Iterator
4850
from gcloud.storage.blob import Blob
49-
from gcloud.storage.blob import _GOOGLE_TIMESTAMP_FORMAT
51+
from gcloud._helpers import _RFC3339_MICROS
5052

5153

5254
class _BlobIterator(Iterator):
@@ -693,7 +695,8 @@ def time_created(self):
693695
"""
694696
value = self._properties.get('timeCreated')
695697
if value is not None:
696-
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)
698+
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
699+
return naive.replace(tzinfo=pytz.utc)
697700

698701
@property
699702
def versioning_enabled(self):

gcloud/storage/test_blob.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,11 +1016,13 @@ def test_storage_class(self):
10161016

10171017
def test_time_deleted(self):
10181018
import datetime
1019+
from pytz import utc
1020+
from gcloud._helpers import _RFC3339_MICROS
10191021
BLOB_NAME = 'blob-name'
10201022
connection = _Connection()
10211023
bucket = _Bucket(connection)
1022-
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
1023-
TIME_DELETED = TIMESTAMP.isoformat() + '.000Z'
1024+
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc)
1025+
TIME_DELETED = TIMESTAMP.strftime(_RFC3339_MICROS)
10241026
properties = {'timeDeleted': TIME_DELETED}
10251027
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
10261028
self.assertEqual(blob.time_deleted, TIMESTAMP)
@@ -1032,11 +1034,13 @@ def test_time_deleted_unset(self):
10321034

10331035
def test_updated(self):
10341036
import datetime
1037+
from pytz import utc
1038+
from gcloud._helpers import _RFC3339_MICROS
10351039
BLOB_NAME = 'blob-name'
10361040
connection = _Connection()
10371041
bucket = _Bucket(connection)
1038-
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
1039-
UPDATED = TIMESTAMP.isoformat() + '.000Z'
1042+
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc)
1043+
UPDATED = TIMESTAMP.strftime(_RFC3339_MICROS)
10401044
properties = {'updated': UPDATED}
10411045
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
10421046
self.assertEqual(blob.updated, TIMESTAMP)

gcloud/storage/test_bucket.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,10 @@ def test_storage_class(self):
790790

791791
def test_time_created(self):
792792
import datetime
793-
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
794-
TIME_CREATED = TIMESTAMP.isoformat() + '.000Z'
793+
from pytz import utc
794+
from gcloud._helpers import _RFC3339_MICROS
795+
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc)
796+
TIME_CREATED = TIMESTAMP.strftime(_RFC3339_MICROS)
795797
properties = {'timeCreated': TIME_CREATED}
796798
bucket = self._makeOne(properties=properties)
797799
self.assertEqual(bucket.time_created, TIMESTAMP)

0 commit comments

Comments
 (0)