Skip to content

Commit

Permalink
Ensure that the timezone doesn't leak into zips.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thatch committed Jan 28, 2024
1 parent be33cff commit 5100fe4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion repro_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
6 changes: 5 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand Down

0 comments on commit 5100fe4

Please sign in to comment.