From 59562802cbdf08279bf9e8243acc3a69cc16b12e Mon Sep 17 00:00:00 2001 From: Prince Kumar Date: Mon, 7 Sep 2026 18:39:09 +0000 Subject: [PATCH] timestamp optimization --- gcsfs/core.py | 8 +++---- gcsfs/tests/test_core.py | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/gcsfs/core.py b/gcsfs/core.py index f41c18bc6..8f3fa5e81 100644 --- a/gcsfs/core.py +++ b/gcsfs/core.py @@ -1081,11 +1081,9 @@ def modified(self, path): def created(self, path): return self.info(path)["ctime"] - def _parse_timestamp(self, timestamp): - assert timestamp.endswith("Z") - timestamp = timestamp[:-1] - timestamp = timestamp + "0" * (6 - len(timestamp.rsplit(".", 1)[-1])) - return datetime.fromisoformat(timestamp + "+00:00") + @staticmethod + def _parse_timestamp(timestamp): + return datetime.fromisoformat(timestamp.replace("Z", "+00:00")) async def _info(self, path, generation=None, **kwargs): """File information about this path.""" diff --git a/gcsfs/tests/test_core.py b/gcsfs/tests/test_core.py index 8ba63b829..7b40a74df 100644 --- a/gcsfs/tests/test_core.py +++ b/gcsfs/tests/test_core.py @@ -3710,3 +3710,49 @@ def test_user_agent_includes_cache_type_and_source_in_read(gcs): for call in mock_session_request.call_args_list ] assert any("cache_type/readahead:d" in ua for ua in user_agents) + + +@pytest.mark.parametrize( + "ts_str, expected_dt", + [ + ( + "2024-01-15T10:30:00.000Z", + datetime(2024, 1, 15, 10, 30, 0, 0, tzinfo=timezone.utc), + ), + ( + "2024-01-15T11:45:00.123456Z", + datetime(2024, 1, 15, 11, 45, 0, 123456, tzinfo=timezone.utc), + ), + ( + "2024-01-15T10:30:00Z", + datetime(2024, 1, 15, 10, 30, 0, 0, tzinfo=timezone.utc), + ), + ( + "2024-01-15T10:30:00.12Z", + datetime(2024, 1, 15, 10, 30, 0, 120000, tzinfo=timezone.utc), + ), + ( + "2024-01-15T10:30:00.1234Z", + datetime(2024, 1, 15, 10, 30, 0, 123400, tzinfo=timezone.utc), + ), + ], +) +def test_parse_timestamp_formats(gcs, ts_str, expected_dt): + assert gcs._parse_timestamp(ts_str) == expected_dt + assert GCSFileSystem._parse_timestamp(ts_str) == expected_dt + + +def test_process_object_timestamps(gcs): + metadata = { + "name": "test_obj.txt", + "size": "50", + "timeCreated": "2024-01-15T10:30:00.000Z", + "updated": "2024-01-15T11:45:00.123456Z", + } + processed = gcs._process_object("my-bucket", metadata) + assert processed["ctime"] == datetime( + 2024, 1, 15, 10, 30, 0, 0, tzinfo=timezone.utc + ) + assert processed["mtime"] == datetime( + 2024, 1, 15, 11, 45, 0, 123456, tzinfo=timezone.utc + )