From 93028489294d8b8b1fdd60694e1f6b2fed5a5968 Mon Sep 17 00:00:00 2001 From: Jay Qi Date: Mon, 29 Jan 2024 23:20:53 -0500 Subject: [PATCH] Fix date_time to use UTC time --- repro_tarfile.py | 4 ++-- tests/test_core.py | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/repro_tarfile.py b/repro_tarfile.py index bbbbcc7..99d8480 100644 --- a/repro_tarfile.py +++ b/repro_tarfile.py @@ -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: diff --git a/tests/test_core.py b/tests/test_core.py index 149ad99..cbea715 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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, @@ -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.""" @@ -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)