Skip to content
Permalink
Browse files
fix(storage): fix incorrect mtime by UTC offset (#42)
Co-authored-by: Christopher Wilcox <crwilcox@google.com>
Co-authored-by: Frank Natividad <frankyn@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 17, 2020
1 parent cecc7ac commit 76bd652a3078d94e03e566b6a387fc488ab26910
Showing with 26 additions and 4 deletions.
  1. +15 −0 google/cloud/storage/_helpers.py
  2. +6 −2 google/cloud/storage/blob.py
  3. +5 −2 tests/unit/test_blob.py
@@ -19,6 +19,7 @@

import base64
from hashlib import md5
from datetime import datetime
import os

from google.cloud.storage.constants import _DEFAULT_TIMEOUT
@@ -297,3 +298,17 @@ def _base64_md5hash(buffer_object):
_write_buffer_to_hash(buffer_object, hash_obj)
digest_bytes = hash_obj.digest()
return base64.b64encode(digest_bytes)


def _convert_to_timestamp(value):
"""Convert non-none datetime to timestamp.
:type value: :class:`datetime.datetime`
:param value: The datetime to convert.
:rtype: int
:returns: The timestamp.
"""
utc_naive = value.replace(tzinfo=None) - value.utcoffset()
mtime = (utc_naive - datetime(1970, 1, 1)).total_seconds()
return mtime
@@ -31,8 +31,8 @@
from io import BytesIO
import mimetypes
import os
import time
import warnings
import six

from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import quote
@@ -57,6 +57,7 @@
from google.cloud.storage._helpers import _get_storage_host
from google.cloud.storage._helpers import _PropertyMixin
from google.cloud.storage._helpers import _scalar_property
from google.cloud.storage._helpers import _convert_to_timestamp
from google.cloud.storage._signing import generate_signed_url_v2
from google.cloud.storage._signing import generate_signed_url_v4
from google.cloud.storage.acl import ACL
@@ -846,7 +847,10 @@ def download_to_filename(

updated = self.updated
if updated is not None:
mtime = time.mktime(updated.timetuple())
if six.PY2:
mtime = _convert_to_timestamp(updated)
else:
mtime = updated.timestamp()
os.utime(file_obj.name, (mtime, mtime))

def download_as_string(self, client=None, start=None, end=None, raw_download=False):
@@ -1060,7 +1060,7 @@ def test_download_to_file_w_chunks_w_raw(self):

def _download_to_filename_helper(self, updated, raw_download):
import os
import time
from google.cloud.storage._helpers import _convert_to_timestamp
from google.cloud._testing import _NamedTemporaryFile

blob_name = "blob-name"
@@ -1080,7 +1080,10 @@ def _download_to_filename_helper(self, updated, raw_download):
self.assertIsNone(blob.updated)
else:
mtime = os.path.getmtime(temp.name)
updated_time = time.mktime(blob.updated.timetuple())
if six.PY2:
updated_time = _convert_to_timestamp(blob.updated)
else:
updated_time = blob.updated.timestamp()
self.assertEqual(mtime, updated_time)

headers = {"accept-encoding": "gzip"}

0 comments on commit 76bd652

Please sign in to comment.