diff --git a/newsfragments/+atime-preserves-mtime.bugfix.rst b/newsfragments/+atime-preserves-mtime.bugfix.rst new file mode 100644 index 0000000..e60aa1b --- /dev/null +++ b/newsfragments/+atime-preserves-mtime.bugfix.rst @@ -0,0 +1 @@ +Preserve the file's modification time when setting its access time. diff --git a/path/__init__.py b/path/__init__.py index 731e88e..0ae883e 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -1045,7 +1045,7 @@ 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 + 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 55d733c..51d774d 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -268,6 +268,16 @@ 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): + target = (Path(tmpdir) / 'file').touch() + target.utime(ns=(1_600_000_000_000_000_000, 1_700_000_000_123_456_700)) + original_mtime = target.stat().st_mtime_ns + + target.atime = 1_500_000_000.0 + + assert target.stat().st_atime == 1_500_000_000.0 + assert target.stat().st_mtime_ns == original_mtime + def test_chmod_str(self, tmpdir): tmpfile = Path(tmpdir) / 'file' tmpfile.touch()