From 5100fe4e6c98177cdb4cce30a280be5524837d84 Mon Sep 17 00:00:00 2001 From: Tim Hatch Date: Sat, 27 Jan 2024 21:57:06 -0800 Subject: [PATCH] Ensure that the timezone doesn't leak into zips. When using SOURCE_DATE_EPOCH, previous code was using time.localtime which is affected by the system timezone. The test code here with tzset likely does not demonstrate the problem on Windows, but hopefully is just a no-op there. --- repro_zipfile.py | 2 +- tests/test_core.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/repro_zipfile.py b/repro_zipfile.py index 22bef7e..a73de8c 100644 --- a/repro_zipfile.py +++ b/repro_zipfile.py @@ -21,7 +21,7 @@ def date_time() -> Union[time.struct_time, Tuple[int, int, int, int, int, int]]: """ source_date_epoch = os.environ.get("SOURCE_DATE_EPOCH", None) if source_date_epoch is not None: - return time.localtime(int(source_date_epoch)) + return time.gmtime(int(source_date_epoch)) return (1980, 1, 1, 0, 0, 0) diff --git a/tests/test_core.py b/tests/test_core.py index 44f8af3..ab04bb8 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,5 +1,5 @@ import platform -from time import sleep +from time import sleep, tzset from zipfile import ZipFile, ZipInfo from repro_zipfile import ReproducibleZipFile @@ -195,6 +195,8 @@ def test_write_single_file_source_date_epoch(base_path, monkeypatch): zp.write(data_file) monkeypatch.setenv("SOURCE_DATE_EPOCH", "1691732367") + monkeypatch.setenv("TZ", "America/Chicago") + tzset() # With SOURCE_DATE_EPOCH set arc_sde1 = base_path / "with_sde1.zip" @@ -203,6 +205,8 @@ def test_write_single_file_source_date_epoch(base_path, monkeypatch): sleep(2) data_file.touch() + monkeypatch.setenv("TZ", "America/Los_Angeles") + tzset() arc_sde2 = base_path / "with_sde2.zip" with ReproducibleZipFile(arc_sde2, "w") as zp: