From fe91b1762b5b641ab200bb471a6b9898abbd8502 Mon Sep 17 00:00:00 2001 From: Yannick Kirschen Date: Fri, 10 May 2024 18:20:28 +0200 Subject: [PATCH] fix: remove ceiling of change timestamps --- ddir/diff.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/ddir/diff.py b/ddir/diff.py index 1553c4d..3d7fab3 100644 --- a/ddir/diff.py +++ b/ddir/diff.py @@ -8,10 +8,9 @@ from __future__ import annotations from dataclasses import dataclass -from datetime import date +from datetime import date, datetime from enum import Enum from hashlib import md5 -from math import ceil from os import sep, listdir, mkdir, stat from os.path import exists, isfile, isdir from random import randint as rand @@ -259,21 +258,17 @@ def _create_for_file(diff: DiffFileCreator, source: str, target: str, fast: bool print(f'file not in target: {source} {target}') diff.add_diff(Diff(DiffType.POSITIVE, 'f', source, target)) else: - # It may occur that the source modification time has a higher/lower - # precision than the target modification time. As rounding to e.g. 2 - # decimal places may lead to a difference of .01, we round to the - # ceiling to avoid false positives. - source_last_changed = ceil(stat(source).st_mtime) - target_last_changed = ceil(stat(target).st_mtime) - - # Here, we check if the files are different by more than one second. - # This is because on some file systems, the modification time may be - # different by one second, even if the file has not been changed. - if source_last_changed - target_last_changed > 1: - print(f'files differ: {source} is newer than {target}') + source_last_changed = stat(source).st_mtime + target_last_changed = stat(target).st_mtime + + source_last_changed_str = datetime.fromtimestamp(source_last_changed).isoformat() + target_last_changed_str = datetime.fromtimestamp(target_last_changed).isoformat() + + if source_last_changed > target_last_changed: + print(f'[metadata] files differ: {source} is newer than {target} ({source_last_changed_str} > {target_last_changed_str})') diff.add_diff(Diff(DiffType.NEWER, 'f', source, target)) - elif target_last_changed - source_last_changed > 1: - print(f'files differ: {target} is newer than {source}') + elif target_last_changed > source_last_changed: + print(f'[metadata] files differ: {target} is newer than {source} ({target_last_changed_str} > {source_last_changed_str})') diff.add_diff(Diff(DiffType.OLDER, 'f', source, target)) elif not fast: with open(source, 'rb', encoding=ENCODING) as source_file, \ @@ -282,7 +277,7 @@ def _create_for_file(diff: DiffFileCreator, source: str, target: str, fast: bool target_md5 = md5(target_file.read()).hexdigest() if source_md5 != target_md5: - print(f'files differ: {source} (change dates are equal)') + print(f'[content ] files differ: {source}') diff.add_diff(Diff(DiffType.UNKNOWN, 'f', source, target))