diff --git a/google/cloud/storage/blob.py b/google/cloud/storage/blob.py index f1cb5666b..f1abadbdf 100644 --- a/google/cloud/storage/blob.py +++ b/google/cloud/storage/blob.py @@ -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 @@ -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): diff --git a/tests/system/test_system.py b/tests/system/test_system.py index ec05b0c72..4615c62e6 100644 --- a/tests/system/test_system.py +++ b/tests/system/test_system.py @@ -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"