Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#818: unify RFC 3339 timestamp format strubg #835

Merged
merged 3 commits into from
Apr 16, 2015
Merged
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
2 changes: 2 additions & 0 deletions gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Local(object):
except ImportError:
app_identity = None

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


class _LocalStack(Local):
"""Manage a thread-local LIFO stack of resources.
Expand Down
2 changes: 1 addition & 1 deletion gcloud/pubsub/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pytz

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


class Message(object):
Expand Down
2 changes: 1 addition & 1 deletion gcloud/pubsub/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _to_fail():
def test_timestamp_w_timestamp_in_attributes(self):
from datetime import datetime
from pytz import utc
from gcloud.pubsub.message import _RFC3339_MICROS
from gcloud._helpers import _RFC3339_MICROS
DATA = b'DEADBEEF'
MESSAGE_ID = b'12345'
TIMESTAMP = '2015-04-10T18:42:27.131956Z'
Expand Down
13 changes: 3 additions & 10 deletions gcloud/pubsub/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def test_publish_single_bytes_wo_attrs_w_add_timestamp(self):
import base64
import datetime
from gcloud.pubsub import topic as MUT
from gcloud._helpers import _RFC3339_MICROS
from gcloud._testing import _Monkey
NOW = datetime.datetime.utcnow()

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

def test_publish_single_bytes_w_add_timestamp_w_ts_in_attrs(self):
import base64
import datetime
from gcloud.pubsub import topic as MUT
from gcloud._testing import _Monkey
NOW = datetime.datetime.utcnow()

def _utcnow(): # pragma: NO COVER
return NOW

TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
Expand All @@ -203,8 +197,7 @@ def _utcnow(): # pragma: NO COVER
conn = _Connection({'messageIds': [MSGID]})
topic = self._makeOne(TOPIC_NAME, project=PROJECT, connection=conn,
timestamp_messages=True)
with _Monkey(MUT, _NOW=_utcnow):
msgid = topic.publish(PAYLOAD, timestamp=OVERRIDE)
msgid = topic.publish(PAYLOAD, timestamp=OVERRIDE)
self.assertEqual(msgid, MSGID)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
Expand Down
3 changes: 2 additions & 1 deletion gcloud/pubsub/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import datetime

from gcloud._helpers import get_default_project
from gcloud._helpers import _RFC3339_MICROS
from gcloud.exceptions import NotFound
from gcloud.pubsub._implicit_environ import get_default_connection

Expand Down Expand Up @@ -123,7 +124,7 @@ def publish(self, message, **attrs):
:returns: message ID assigned by the server to the published message
"""
if self.timestamp_messages and 'timestamp' not in attrs:
attrs['timestamp'] = '%sZ' % _NOW().isoformat()
attrs['timestamp'] = _NOW().strftime(_RFC3339_MICROS)
message_b = base64.b64encode(message).decode('ascii')
message_data = {'data': message_b, 'attributes': attrs}
data = {'messages': [message_data]}
Expand Down
9 changes: 6 additions & 3 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import time

import pytz
import six
from six.moves.urllib.parse import quote # pylint: disable=F0401

Expand All @@ -35,10 +36,10 @@
from gcloud.storage._helpers import _scalar_property
from gcloud.storage import _implicit_environ
from gcloud.storage.acl import ObjectACL
from gcloud._helpers import _RFC3339_MICROS


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


class Blob(_PropertyMixin):
Expand Down Expand Up @@ -749,7 +750,8 @@ def time_deleted(self):
"""
value = self._properties.get('timeDeleted')
if value is not None:
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
return naive.replace(tzinfo=pytz.utc)

@property
def updated(self):
Expand All @@ -763,7 +765,8 @@ def updated(self):
"""
value = self._properties.get('updated')
if value is not None:
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
return naive.replace(tzinfo=pytz.utc)


class _UploadConfig(object):
Expand Down
7 changes: 5 additions & 2 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import datetime
import copy
import os

import pytz
import six

from gcloud._helpers import get_default_project
Expand All @@ -46,7 +48,7 @@
from gcloud.storage.acl import DefaultObjectACL
from gcloud.storage.iterator import Iterator
from gcloud.storage.blob import Blob
from gcloud.storage.blob import _GOOGLE_TIMESTAMP_FORMAT
from gcloud._helpers import _RFC3339_MICROS


class _BlobIterator(Iterator):
Expand Down Expand Up @@ -693,7 +695,8 @@ def time_created(self):
"""
value = self._properties.get('timeCreated')
if value is not None:
return datetime.datetime.strptime(value, _GOOGLE_TIMESTAMP_FORMAT)
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
return naive.replace(tzinfo=pytz.utc)

@property
def versioning_enabled(self):
Expand Down
12 changes: 8 additions & 4 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,13 @@ def test_storage_class(self):

def test_time_deleted(self):
import datetime
from pytz import utc
from gcloud._helpers import _RFC3339_MICROS
BLOB_NAME = 'blob-name'
connection = _Connection()
bucket = _Bucket(connection)
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
TIME_DELETED = TIMESTAMP.isoformat() + '.000Z'
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc)
TIME_DELETED = TIMESTAMP.strftime(_RFC3339_MICROS)
properties = {'timeDeleted': TIME_DELETED}
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
self.assertEqual(blob.time_deleted, TIMESTAMP)
Expand All @@ -1032,11 +1034,13 @@ def test_time_deleted_unset(self):

def test_updated(self):
import datetime
from pytz import utc
from gcloud._helpers import _RFC3339_MICROS
BLOB_NAME = 'blob-name'
connection = _Connection()
bucket = _Bucket(connection)
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
UPDATED = TIMESTAMP.isoformat() + '.000Z'
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc)
UPDATED = TIMESTAMP.strftime(_RFC3339_MICROS)
properties = {'updated': UPDATED}
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
self.assertEqual(blob.updated, TIMESTAMP)
Expand Down
6 changes: 4 additions & 2 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,10 @@ def test_storage_class(self):

def test_time_created(self):
import datetime
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
TIME_CREATED = TIMESTAMP.isoformat() + '.000Z'
from pytz import utc
from gcloud._helpers import _RFC3339_MICROS
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc)
TIME_CREATED = TIMESTAMP.strftime(_RFC3339_MICROS)
properties = {'timeCreated': TIME_CREATED}
bucket = self._makeOne(properties=properties)
self.assertEqual(bucket.time_created, TIMESTAMP)
Expand Down