diff options
author | Christian Grothoff <grothoff@gnunet.org> | 2022-03-21 03:37:29 +0100 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2022-03-21 03:38:18 +0100 |
commit | ec8a825b0e56c692c4879db10c6b25cd26bb42e4 (patch) | |
tree | a08ab989b42aa01933410b8bf54ddfb8243151f7 /src | |
parent | e96a9eb00942666fceb2a3658ee7b6d2784a098c (diff) |
add new approximate time cmp function
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_time_lib.h | 17 | ||||
-rw-r--r-- | src/util/time.c | 16 |
2 files changed, 32 insertions, 1 deletions
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 @@ -517,6 +517,21 @@ 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) { |