Skip to content

Commit

Permalink
fix: custom_time decoding without microseconds
Browse files Browse the repository at this point in the history
Currently custom_time is not being decoded correctly if the value
has a zero in the microseconds field. This fixes the issue by
using a different helper method to decode.

Fixes #363
  • Loading branch information
tritone committed Feb 9, 2021
1 parent f048be1 commit 0de2a21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion google/cloud/storage/blob.py
Expand Up @@ -56,6 +56,7 @@
from google.cloud._helpers import _bytes_to_unicode
from google.cloud._helpers import _datetime_to_rfc3339
from google.cloud._helpers import _rfc3339_to_datetime
from google.cloud._helpers import _rfc3339_nanos_to_datetime
from google.cloud._helpers import _to_bytes
from google.cloud.exceptions import NotFound
from google.cloud.storage._helpers import _add_generation_match_parameters
Expand Down Expand Up @@ -3775,7 +3776,7 @@ def custom_time(self):
"""
value = self._properties.get("customTime")
if value is not None:
return _rfc3339_to_datetime(value)
return _rfc3339_nanos_to_datetime(value)

@custom_time.setter
def custom_time(self, value):
Expand Down
15 changes: 15 additions & 0 deletions tests/system/test_system.py
Expand Up @@ -1040,6 +1040,21 @@ def test_upload_blob_custom_time(self):
custom_time = same_blob.custom_time.replace(tzinfo=None)
self.assertEqual(custom_time, current_time)

def test_blob_custom_time_no_micros(self):
# Test that timestamps without microseconds are treated correctly by
# custom_time encoding/decoding.
blob = self.bucket.blob("CustomTimeNoMicrosBlob")
file_contents = b"Hello World"
time_without_micros = datetime.datetime(2021, 2, 10, 12, 30)
blob.custom_time = time_without_micros
blob.upload_from_string(file_contents)
self.case_blobs_to_delete.append(blob)

same_blob = self.bucket.blob(("CustomTimeNoMicrosBlob"))
same_blob.reload(projection="full")
custom_time = same_blob.custom_time.replace(tzinfo=None)
self.assertEqual(custom_time, time_without_micros)

def test_blob_crc32_md5_hash(self):
blob = self.bucket.blob("MyBuffer")
file_contents = b"Hello World"
Expand Down

0 comments on commit 0de2a21

Please sign in to comment.