Skip to content

Commit 76bd652

Browse files
HemangChothanicrwilcoxfrankyn
authored
fix(storage): fix incorrect mtime by UTC offset (#42)
Co-authored-by: Christopher Wilcox <[email protected]> Co-authored-by: Frank Natividad <[email protected]>
1 parent cecc7ac commit 76bd652

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

google/cloud/storage/_helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import base64
2121
from hashlib import md5
22+
from datetime import datetime
2223
import os
2324

2425
from google.cloud.storage.constants import _DEFAULT_TIMEOUT
@@ -297,3 +298,17 @@ def _base64_md5hash(buffer_object):
297298
_write_buffer_to_hash(buffer_object, hash_obj)
298299
digest_bytes = hash_obj.digest()
299300
return base64.b64encode(digest_bytes)
301+
302+
303+
def _convert_to_timestamp(value):
304+
"""Convert non-none datetime to timestamp.
305+
306+
:type value: :class:`datetime.datetime`
307+
:param value: The datetime to convert.
308+
309+
:rtype: int
310+
:returns: The timestamp.
311+
"""
312+
utc_naive = value.replace(tzinfo=None) - value.utcoffset()
313+
mtime = (utc_naive - datetime(1970, 1, 1)).total_seconds()
314+
return mtime

google/cloud/storage/blob.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
from io import BytesIO
3232
import mimetypes
3333
import os
34-
import time
3534
import warnings
35+
import six
3636

3737
from six.moves.urllib.parse import parse_qsl
3838
from six.moves.urllib.parse import quote
@@ -57,6 +57,7 @@
5757
from google.cloud.storage._helpers import _get_storage_host
5858
from google.cloud.storage._helpers import _PropertyMixin
5959
from google.cloud.storage._helpers import _scalar_property
60+
from google.cloud.storage._helpers import _convert_to_timestamp
6061
from google.cloud.storage._signing import generate_signed_url_v2
6162
from google.cloud.storage._signing import generate_signed_url_v4
6263
from google.cloud.storage.acl import ACL
@@ -846,7 +847,10 @@ def download_to_filename(
846847

847848
updated = self.updated
848849
if updated is not None:
849-
mtime = time.mktime(updated.timetuple())
850+
if six.PY2:
851+
mtime = _convert_to_timestamp(updated)
852+
else:
853+
mtime = updated.timestamp()
850854
os.utime(file_obj.name, (mtime, mtime))
851855

852856
def download_as_string(self, client=None, start=None, end=None, raw_download=False):

tests/unit/test_blob.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ def test_download_to_file_w_chunks_w_raw(self):
10601060

10611061
def _download_to_filename_helper(self, updated, raw_download):
10621062
import os
1063-
import time
1063+
from google.cloud.storage._helpers import _convert_to_timestamp
10641064
from google.cloud._testing import _NamedTemporaryFile
10651065

10661066
blob_name = "blob-name"
@@ -1080,7 +1080,10 @@ def _download_to_filename_helper(self, updated, raw_download):
10801080
self.assertIsNone(blob.updated)
10811081
else:
10821082
mtime = os.path.getmtime(temp.name)
1083-
updated_time = time.mktime(blob.updated.timetuple())
1083+
if six.PY2:
1084+
updated_time = _convert_to_timestamp(blob.updated)
1085+
else:
1086+
updated_time = blob.updated.timestamp()
10841087
self.assertEqual(mtime, updated_time)
10851088

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

0 commit comments

Comments
 (0)