From ec8a825b0e56c692c4879db10c6b25cd26bb42e4 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 21 Mar 2022 03:37:29 +0100 Subject: add new approximate time cmp function --- src/include/gnunet_time_lib.h | 17 ++++++++++++++++- src/util/time.c | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/include/gnunet_time_lib.h b/src/include/gnunet_time_lib.h index 96413c3cc..55af62b72 100644 --- a/src/include/gnunet_time_lib.h +++ b/src/include/gnunet_time_lib.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2001-2013 GNUnet e.V. + Copyright (C) 2001-2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -516,6 +516,21 @@ struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future); +/** + * Test if @a a1 and @a a2 are equal within a margin of + * error of @a t. + * + * @param a1 time to compare + * @param a2 time to compare + * @param t tolerance to apply + * @return true if "|a1-a2|<=t" holds. + */ +bool +GNUNET_TIME_absolute_approx_eq (struct GNUNET_TIME_Absolute a1, + struct GNUNET_TIME_Absolute a2, + struct GNUNET_TIME_Relative t); + + /** * Calculate the estimate time of arrival/completion * for an operation. diff --git a/src/util/time.c b/src/util/time.c index 68a6937a0..aeec2b3f9 100644 --- a/src/util/time.c +++ b/src/util/time.c @@ -58,6 +58,22 @@ GNUNET_TIME_get_offset () } +bool +GNUNET_TIME_absolute_approx_eq (struct GNUNET_TIME_Absolute a1, + struct GNUNET_TIME_Absolute a2, + struct GNUNET_TIME_Relative t) +{ + struct GNUNET_TIME_Relative delta; + + delta = GNUNET_TIME_relative_min ( + GNUNET_TIME_absolute_get_difference (a1, a2), + GNUNET_TIME_absolute_get_difference (a2, a1)); + return GNUNET_TIME_relative_cmp (delta, + <=, + t); +} + + struct GNUNET_TIME_Timestamp GNUNET_TIME_absolute_to_timestamp (struct GNUNET_TIME_Absolute at) { -- cgit v1.2.3