GNUnet  0.17.6
gnunet_time_lib.h
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2001-2022 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
36 #ifndef GNUNET_TIME_LIB_H
37 #define GNUNET_TIME_LIB_H
38 
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #if 0 /* keep Emacsens' auto-indent happy */
43 }
44 #endif
45 #endif
46 
47 #include "gnunet_common.h"
48 
53 {
57  uint64_t abs_value_us;
58 };
59 
64 {
69 };
70 
76 {
80  uint64_t rel_value_us;
81 };
82 
84 
89 {
94 };
95 
96 
101 {
106 };
107 
112 {
117 };
118 
120 
124 #define GNUNET_TIME_UNIT_ZERO ((struct GNUNET_TIME_Relative){0})
125 
129 #define GNUNET_TIME_UNIT_ZERO_ABS ((struct GNUNET_TIME_Absolute){0})
130 
134 #define GNUNET_TIME_UNIT_ZERO_TS ((struct GNUNET_TIME_Timestamp){{0}})
135 
139 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_ ()
140 
144 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_ ()
145 
149 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_ ()
150 
154 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_ ()
155 
159 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_ ()
160 
164 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply ( \
165  GNUNET_TIME_UNIT_HOURS, 24)
166 
170 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply ( \
171  GNUNET_TIME_UNIT_DAYS, 7)
172 
176 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply ( \
177  GNUNET_TIME_UNIT_DAYS, 30)
178 
182 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply ( \
183  GNUNET_TIME_UNIT_DAYS, 365)
184 
189 #define GNUNET_TIME_UNIT_FOREVER_REL \
190  ((struct GNUNET_TIME_Relative){UINT64_MAX})
191 
196 #define GNUNET_TIME_UNIT_FOREVER_ABS \
197  ((struct GNUNET_TIME_Absolute){UINT64_MAX})
198 
203 #define GNUNET_TIME_UNIT_FOREVER_TS \
204  ((struct GNUNET_TIME_Timestamp){{UINT64_MAX}})
205 
206 
210 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD \
211  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
212 
213 
220 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min ( \
221  GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
222  GNUNET_TIME_relative_multiply ( \
223  GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2))
224 
225 
234 const char *
236 
237 
248 const char *
250 
251 
262 const char *
264  bool do_round);
265 
266 
279 
280 
289 
290 
296 
297 
303 
304 
310 
311 
317 
318 
324 
325 
331 
332 
338 
339 
345 
346 
352 
353 
361 
362 
372 
373 
383 
384 
393 
394 
402 
403 
412 #define GNUNET_TIME_absolute_cmp(t1,op,t2) \
413  ((void) (1 op 2), (t1).abs_value_us op (t2).abs_value_us)
414 
415 
424 #define GNUNET_TIME_timestamp_cmp(t1,op,t2) \
425  GNUNET_TIME_absolute_cmp ((t1).abs_time,op,(t2).abs_time)
426 
427 
436 #define GNUNET_TIME_relative_cmp(t1,op,t2) \
437  ((void) (1 op 2), (t1).rel_value_us op (t2).rel_value_us)
438 
439 
449  struct GNUNET_TIME_Relative t2);
450 
451 
461  struct GNUNET_TIME_Relative t2);
462 
463 
473  struct GNUNET_TIME_Absolute t2);
474 
475 
485  struct GNUNET_TIME_Absolute t2);
486 
487 
497  struct GNUNET_TIME_Relative rt);
498 
499 
509  struct GNUNET_TIME_Timestamp t2);
510 
511 
521  struct GNUNET_TIME_Timestamp t2);
522 
523 
533 
534 
544 bool
546  struct GNUNET_TIME_Absolute a2,
547  struct GNUNET_TIME_Relative t);
548 
549 
562  uint64_t finished,
563  uint64_t total);
564 
565 
577  struct GNUNET_TIME_Absolute end);
578 
579 
589 
590 
602 
603 
615 
616 
626  unsigned long long factor);
627 
628 
638  unsigned long long factor);
639 
640 
650  unsigned long long factor);
651 
652 
662  struct GNUNET_TIME_Relative a2);
663 
664 
674  struct GNUNET_TIME_Relative a2);
675 
676 
685 
686 
695 
696 
705 
706 
715 
716 
724 GNUNET_TIME_absolute_from_ms (uint64_t ms_after_epoch);
725 
726 
732 bool
734 
735 
741 bool
743 
744 
750 bool
752 
753 
759 bool
761 
762 
768 bool
770 
771 
777 bool
779 
780 
788 GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch);
789 
790 
798 GNUNET_TIME_timestamp_from_s (uint64_t s_after_epoch);
799 
800 
807 uint64_t
809 
810 
819 
820 
829 
830 
836 void
837 GNUNET_TIME_set_offset (long long offset);
838 
839 
845 long long
847 
848 
852 unsigned int
854 
855 
863 GNUNET_TIME_year_to_time (unsigned int year);
864 
865 
872 unsigned int
874 
875 
880 
881 
902  const struct GNUNET_CONFIGURATION_Handle *cfg);
903 
904 
905 #if 0 /* keep Emacsens' auto-indent happy */
906 {
907 #endif
908 #ifdef __cplusplus
909 }
910 #endif
911 
912 /* ifndef GNUNET_TIME_LIB_H */
913 #endif
914  /* end of group time */
916  /* end of group addition */
918 
919 /* end of gnunet_time_lib.h */
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static struct GNUNET_TIME_Relative duration
How long do we run the test?
static unsigned int threshold
What should the threshold for then key be?
static void do_round(void *cls)
Send out PUSHes and PULLs, possibly update #view, samplers.
static struct GNUNET_SCHEDULER_Task * t
Main task.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
#define GNUNET_PACKED
gcc-ism to get packed structs.
struct GNUNET_TIME_Absolute GNUNET_TIME_year_to_time(unsigned int year)
Convert a year to an expiration time of January 1st of that year.
Definition: time.c:800
struct GNUNET_TIME_Relative GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the minimum of two relative time values.
Definition: time.c:342
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero_(void)
Return relative time of 0ms.
Definition: time.c:132
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_hour_(void)
Return relative time of 1 hour.
Definition: time.c:186
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_from_s(uint64_t s_after_epoch)
Convert seconds after the UNIX epoch to absolute time.
Definition: time.c:702
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:627
bool GNUNET_TIME_absolute_is_future(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the future (excluding now).
Definition: time.c:678
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_millisecond_(void)
Return relative time of 1ms.
Definition: time.c:159
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:435
const char * GNUNET_TIME_absolute2s(struct GNUNET_TIME_Absolute ts)
Like asctime, except for GNUnet time.
Definition: time.c:238
struct GNUNET_TIME_Relative GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Saturating multiply relative time by a given factor.
Definition: time.c:530
struct GNUNET_TIME_Relative GNUNET_TIME_relative_max(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the maximum of two relative time values.
Definition: time.c:350
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_minute_(void)
Return relative time of 1 minute.
Definition: time.c:177
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_min(struct GNUNET_TIME_Timestamp t1, struct GNUNET_TIME_Timestamp t2)
Return the minimum of two timestamps.
Definition: time.c:382
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero_(void)
Return absolute time of 0ms.
Definition: time.c:141
struct GNUNET_TIME_Timestamp GNUNET_TIME_relative_to_timestamp(struct GNUNET_TIME_Relative rel)
Convert relative time to a timestamp in the future.
Definition: time.c:334
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_from_s(uint64_t s_after_epoch)
Convert seconds after the UNIX epoch to timestamp.
Definition: time.c:715
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:404
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_max(struct GNUNET_TIME_Absolute t1, struct GNUNET_TIME_Absolute t2)
Return the maximum of two absolute time values.
Definition: time.c:366
unsigned int GNUNET_TIME_get_current_year(void)
Return the current year (e.g.
Definition: time.c:746
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:843
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_from_ms(uint64_t ms_after_epoch)
Convert milliseconds after the UNIX epoch to absolute time.
Definition: time.c:688
bool GNUNET_TIME_relative_is_zero(struct GNUNET_TIME_Relative rel)
Test if rel is zero.
Definition: time.c:661
bool GNUNET_TIME_absolute_is_never(struct GNUNET_TIME_Absolute abs)
Test if abs is never.
Definition: time.c:647
struct GNUNET_TIME_Relative GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold)
Randomized exponential back-off, starting at 1 ms and going up by a factor of 2+r,...
Definition: time.c:829
struct GNUNET_TIME_Relative GNUNET_TIME_relative_subtract(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Subtract relative timestamp from the other.
Definition: time.c:602
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever_(void)
Return "forever".
Definition: time.c:204
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:110
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever_(void)
Return "forever".
Definition: time.c:195
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_get(void)
Get timestamp representing the current time.
Definition: time.c:124
uint64_t GNUNET_TIME_timestamp_to_s(struct GNUNET_TIME_Timestamp ts)
Convert timestamp to number of seconds after the UNIX epoch.
Definition: time.c:729
bool GNUNET_TIME_relative_is_forever(struct GNUNET_TIME_Relative rel)
Test if rel is forever.
Definition: time.c:654
long long GNUNET_TIME_get_offset(void)
Get the timestamp offset for this instance.
Definition: time.c:55
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:736
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_round_down(struct GNUNET_TIME_Absolute at, struct GNUNET_TIME_Relative rt)
Round down absolute time at to multiple of rt.
Definition: time.c:390
struct GNUNET_TIME_Relative GNUNET_TIME_relative_add(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Add relative times together.
Definition: time.c:584
void GNUNET_TIME_set_offset(long long offset)
Set the timestamp offset for this instance.
Definition: time.c:48
struct GNUNET_TIME_Relative GNUNET_TIME_randomize(struct GNUNET_TIME_Relative r)
Return a random time value between 0.5*r and 1.5*r.
Definition: time.c:850
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_subtract(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Subtract a given relative duration from the given start time.
Definition: time.c:468
unsigned int GNUNET_TIME_time_to_year(struct GNUNET_TIME_Absolute at)
Convert an expiration time to the respective year (rounds)
Definition: time.c:760
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_second_(void)
Return relative time of 1s.
Definition: time.c:168
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit_(void)
Return relative time of 1 microsecond.
Definition: time.c:150
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:315
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:483
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_min(struct GNUNET_TIME_Absolute t1, struct GNUNET_TIME_Absolute t2)
Return the minimum of two absolute time values.
Definition: time.c:358
struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a)
Convert relative time to network byte order.
Definition: time.c:617
bool GNUNET_TIME_absolute_approx_eq(struct GNUNET_TIME_Absolute a1, struct GNUNET_TIME_Absolute a2, struct GNUNET_TIME_Relative t)
Test if a1 and a2 are equal within a margin of error of t.
Definition: time.c:62
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:449
const char * GNUNET_TIME_timestamp2s(struct GNUNET_TIME_Timestamp ts)
Convert ts to human-readable timestamp.
Definition: time.c:213
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_max(struct GNUNET_TIME_Timestamp t1, struct GNUNET_TIME_Timestamp t2)
Return the maximum of two timestamps.
Definition: time.c:374
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_monotonic(const struct GNUNET_CONFIGURATION_Handle *cfg)
Obtain the current time and make sure it is monotonically increasing.
Definition: time.c:859
struct GNUNET_TIME_TimestampNBO GNUNET_TIME_timestamp_hton(struct GNUNET_TIME_Timestamp t)
Convert timestamp to network byte order.
Definition: time.c:90
struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Divide relative time by a given factor.
Definition: time.c:549
const char * GNUNET_TIME_relative2s(struct GNUNET_TIME_Relative delta, bool do_round)
Give relative time in human-readable fancy format.
Definition: time.c:263
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute end)
Compute the time difference between the given start and end times.
Definition: time.c:420
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:637
bool GNUNET_TIME_absolute_is_past(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the past (excluding now).
Definition: time.c:668
struct GNUNET_TIME_Relative GNUNET_TIME_calculate_eta(struct GNUNET_TIME_Absolute start, uint64_t finished, uint64_t total)
Calculate the estimate time of arrival/completion for an operation.
Definition: time.c:563
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition: time.c:100
struct GNUNET_TIME_Timestamp GNUNET_TIME_absolute_to_timestamp(struct GNUNET_TIME_Absolute at)
Round an absolute time to a timestamp.
Definition: time.c:78
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
Time for absolute time used by GNUnet, in microseconds and in network byte order.
uint64_t abs_value_us__
The actual value (in network byte order).
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds and in network byte order.
uint64_t rel_value_us__
The actual value (in network byte order).
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Time for timestamps used by GNUnet, in seconds and in network byte order.
struct GNUNET_TIME_AbsoluteNBO abs_time_nbo
The actual value.
Rounded time for timestamps used by GNUnet, in seconds.
struct GNUNET_TIME_Absolute abs_time
The actual value.