Skip to content

Commit

Permalink
Don't return naive datetimes from bucket/blob timestamp accessors.
Browse files Browse the repository at this point in the history
Addresses:
#835 (comment)
  • Loading branch information
tseaver committed Apr 16, 2015
1 parent 5d13cbe commit 1d35fe8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 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 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, _RFC3339_MICROS)
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, _RFC3339_MICROS)
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
return naive.replace(tzinfo=pytz.utc)


class _UploadConfig(object):
Expand Down
5 changes: 4 additions & 1 deletion 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 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, _RFC3339_MICROS)
naive = datetime.datetime.strptime(value, _RFC3339_MICROS)
return naive.replace(tzinfo=pytz.utc)

@property
def versioning_enabled(self):
Expand Down
6 changes: 4 additions & 2 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,12 @@ 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)
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)
Expand All @@ -1033,11 +1034,12 @@ 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)
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)
Expand Down
3 changes: 2 additions & 1 deletion gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,9 @@ def test_storage_class(self):

def test_time_created(self):
import datetime
from pytz import utc
from gcloud._helpers import _RFC3339_MICROS
TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37)
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)
Expand Down

0 comments on commit 1d35fe8

Please sign in to comment.