Skip to content

Commit

Permalink
Fix date_time to use UTC time
Browse files Browse the repository at this point in the history
  • Loading branch information
jayqi committed Jan 30, 2024
1 parent ce6081f commit 9302848
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions repro_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

def date_time() -> int:
"""Returns date_time value used to force overwrite on all TarInfo objects. Defaults to
315550800 (corresponding to 1980-01-01 00:00:00 UTC). You can set this with the environment
315532800 (corresponding to 1980-01-01 00:00:00 UTC). You can set this with the environment
variable SOURCE_DATE_EPOCH as an integer value representing seconds since Epoch.
"""
source_date_epoch = os.environ.get("SOURCE_DATE_EPOCH", None)
if source_date_epoch is not None:
return int(source_date_epoch)
return int(datetime.datetime(1980, 1, 1, 0, 0, 0).timestamp())
return int(datetime.datetime(1980, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc).timestamp())


def file_mode() -> int:
Expand Down
24 changes: 21 additions & 3 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from tarfile import TarFile, TarInfo
from time import sleep

from repro_tarfile import ReproducibleTarFile
try:
from time import tzset
except ImportError:
tzset is None

import pytest

from repro_tarfile import ReproducibleTarFile, date_time
from tests.utils import (
assert_archive_contents_equals,
data_factory,
Expand Down Expand Up @@ -186,6 +193,18 @@ def test_add_single_file(base_path):
assert hash_file(tf_arc1) != hash_file(tf_arc2)


@pytest.mark.skipif(tzset is None, reason="tzset not available")
def test_date_time_not_affected_by_timezone(monkeypatch):
monkeypatch.setenv("TZ", "America/Chicago")
tzset()
dt1 = date_time()
monkeypatch.setenv("TZ", "America/Los_Angeles")
tzset()
dt2 = date_time()

assert dt1 == dt2


def test_add_single_file_source_date_epoch(base_path, monkeypatch):
"""Writing the same file with different mtime with SOURCE_DATE_EPOCH set produces the
same hash."""
Expand All @@ -196,9 +215,8 @@ def test_add_single_file_source_date_epoch(base_path, monkeypatch):
with ReproducibleTarFile.open(arc_base, "w") as tp:
tp.add(data_file)

monkeypatch.setenv("SOURCE_DATE_EPOCH", "1691732367")

# With SOURCE_DATE_EPOCH set
monkeypatch.setenv("SOURCE_DATE_EPOCH", "1691732367")
arc_sde1 = base_path / "with_sde1.tar"
with ReproducibleTarFile.open(arc_sde1, "w") as tp:
tp.add(data_file)
Expand Down

0 comments on commit 9302848

Please sign in to comment.