Skip to content

Commit

Permalink
Ensure that the timezone doesn't leak into zips. (#8)
Browse files Browse the repository at this point in the history
* 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.

* Fix test for Windows

* Update changelog

---------

Co-authored-by: Jay Qi <jayqi@users.noreply.github.com>
  • Loading branch information
thatch and jayqi committed Feb 2, 2024
1 parent b89b7df commit 7bc64a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog — repro-zipfile

## v0.3.1 (2024-02-02)

- Fixed bug that caused timestamps set by `SOURCE_DATE_EPOCH` to be affected by the local system timezone. It now always uses UTC. ([PR #8](https://github.com/drivendataorg/repro-zipfile/pull/8) from [@thatch](https://github.com/thatch))

## v0.3.0 (2024-01-27)

- Added a `cli` installation extra for installing the rpzip package, which includes a command-line program
Expand Down
4 changes: 2 additions & 2 deletions repro_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
except ImportError:
_MASK_COMPRESS_OPTION_1 = 0x02

__version__ = "0.3.0"
__version__ = "0.3.1"


def date_time() -> Union[time.struct_time, Tuple[int, int, int, int, int, int]]:
Expand All @@ -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
12 changes: 12 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
from time import sleep
from zipfile import ZipFile, ZipInfo

try:
from time import tzset
except ImportError:
tzset = None

from repro_zipfile import ReproducibleZipFile
from tests.utils import (
assert_archive_contents_equals,
Expand Down Expand Up @@ -195,6 +200,9 @@ def test_write_single_file_source_date_epoch(base_path, monkeypatch):
zp.write(data_file)

monkeypatch.setenv("SOURCE_DATE_EPOCH", "1691732367")
if tzset:
monkeypatch.setenv("TZ", "America/Chicago")
tzset()

# With SOURCE_DATE_EPOCH set
arc_sde1 = base_path / "with_sde1.zip"
Expand All @@ -203,6 +211,10 @@ def test_write_single_file_source_date_epoch(base_path, monkeypatch):

sleep(2)
data_file.touch()
if tzset:
# Set a different timezone to make sure it doesn't affect the set time
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 7bc64a8

Please sign in to comment.