From 2ef09eaa777a499df32d646f79fbd015643f380e Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 10 Feb 2023 17:58:46 +0100 Subject: [PATCH] same_ts_ns: add a timestamp comparison function also: HAVE_UTIMES means 1us resolution (*) (*) at least as far as the utimes() call is concerned. Whether there are other limitations has to be seen. --- src/borg/testsuite/__init__.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/borg/testsuite/__init__.py b/src/borg/testsuite/__init__.py index 350b64cae..3d01fd619 100644 --- a/src/borg/testsuite/__init__.py +++ b/src/borg/testsuite/__init__.py @@ -42,14 +42,21 @@ except OSError: # The mtime get/set precision varies on different OS and Python versions if posix and "HAVE_FUTIMENS" in getattr(posix, "_have_functions", []): - st_mtime_ns_round = 0 + st_mtime_ns_round = 0 # 1ns resolution elif "HAVE_UTIMES" in sysconfig.get_config_vars(): - st_mtime_ns_round = -6 + st_mtime_ns_round = -3 # 1us resolution else: - st_mtime_ns_round = -9 + st_mtime_ns_round = -9 # 1s resolution if sys.platform.startswith("netbsd"): - st_mtime_ns_round = -4 # only >1 microsecond resolution here? + st_mtime_ns_round = -4 # 10us - strange: only >1 microsecond resolution here? + + +def same_ts_ns(ts_ns1, ts_ns2): + """compare 2 timestamps (both in nanoseconds) whether they are (roughly) equal""" + diff_ts = int(abs(ts_ns1 - ts_ns2)) + diff_max = 10 ** (-st_mtime_ns_round) + return diff_ts <= diff_max @contextmanager