Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 0de2a21

Browse files
committed
fix: custom_time decoding without microseconds
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
1 parent f048be1 commit 0de2a21

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

google/cloud/storage/blob.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from google.cloud._helpers import _bytes_to_unicode
5757
from google.cloud._helpers import _datetime_to_rfc3339
5858
from google.cloud._helpers import _rfc3339_to_datetime
59+
from google.cloud._helpers import _rfc3339_nanos_to_datetime
5960
from google.cloud._helpers import _to_bytes
6061
from google.cloud.exceptions import NotFound
6162
from google.cloud.storage._helpers import _add_generation_match_parameters
@@ -3775,7 +3776,7 @@ def custom_time(self):
37753776
"""
37763777
value = self._properties.get("customTime")
37773778
if value is not None:
3778-
return _rfc3339_to_datetime(value)
3779+
return _rfc3339_nanos_to_datetime(value)
37793780

37803781
@custom_time.setter
37813782
def custom_time(self, value):

tests/system/test_system.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,21 @@ def test_upload_blob_custom_time(self):
10401040
custom_time = same_blob.custom_time.replace(tzinfo=None)
10411041
self.assertEqual(custom_time, current_time)
10421042

1043+
def test_blob_custom_time_no_micros(self):
1044+
# Test that timestamps without microseconds are treated correctly by
1045+
# custom_time encoding/decoding.
1046+
blob = self.bucket.blob("CustomTimeNoMicrosBlob")
1047+
file_contents = b"Hello World"
1048+
time_without_micros = datetime.datetime(2021, 2, 10, 12, 30)
1049+
blob.custom_time = time_without_micros
1050+
blob.upload_from_string(file_contents)
1051+
self.case_blobs_to_delete.append(blob)
1052+
1053+
same_blob = self.bucket.blob(("CustomTimeNoMicrosBlob"))
1054+
same_blob.reload(projection="full")
1055+
custom_time = same_blob.custom_time.replace(tzinfo=None)
1056+
self.assertEqual(custom_time, time_without_micros)
1057+
10431058
def test_blob_crc32_md5_hash(self):
10441059
blob = self.bucket.blob("MyBuffer")
10451060
file_contents = b"Hello World"

0 commit comments

Comments
 (0)