diff --git a/path/__init__.py b/path/__init__.py index 33dd979..dadb4bd 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -1047,7 +1047,8 @@ def getatime(self) -> float: return self.module.getatime(self) def set_atime(self, value: float | datetime.datetime) -> None: - mtime_ns = self.stat().st_atime_ns + # Preserve mtime; st_atime_ns was a copy-paste bug (clobbered mtime). + mtime_ns = self.stat().st_mtime_ns self.utime(ns=(_make_timestamp_ns(value), mtime_ns)) @property diff --git a/tests/test_path.py b/tests/test_path.py index b424bb6..b52b3bf 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -269,6 +269,15 @@ def test_utime(self, tmpdir): new_time = (time.time() - 600,) * 2 assert Path(tmpfile).utime(new_time).stat().st_atime == new_time[0] + def test_set_atime_preserves_mtime(self, tmpdir): + tmpfile = Path(tmpdir) / 'file' + tmpfile.touch() + os.utime(tmpfile, (1000.0, 2000.0)) + tmpfile.set_atime(3000.0) + st = tmpfile.stat() + assert st.st_atime == 3000.0 + assert st.st_mtime == 2000.0 + def test_chmod_str(self, tmpdir): tmpfile = Path(tmpdir) / 'file' tmpfile.touch()