RECLAIM/OIDC: code cleanup
[oweals/gnunet.git] / src / include / gnunet_time_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 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
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * Functions related to time
26  *
27  * @defgroup time  Time library
28  * Time and time calculations.
29  * @{
30  */
31
32 #ifndef GNUNET_TIME_LIB_H
33 #define GNUNET_TIME_LIB_H
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
43 #include "gnunet_common.h"
44
45 /**
46  * Time for absolute times used by GNUnet, in microseconds.
47  */
48 struct GNUNET_TIME_Absolute
49 {
50   /**
51    * The actual value.
52    */
53   uint64_t abs_value_us;
54 };
55
56 /**
57  * Time for relative time used by GNUnet, in microseconds.
58  * Always positive, so we can only refer to future time.
59  */
60 struct GNUNET_TIME_Relative
61 {
62   /**
63    * The actual value.
64    */
65   uint64_t rel_value_us;
66 };
67
68 GNUNET_NETWORK_STRUCT_BEGIN
69
70 /**
71  * Time for relative time used by GNUnet, in microseconds and in network byte order.
72  */
73 struct GNUNET_TIME_RelativeNBO
74 {
75   /**
76    * The actual value (in network byte order).
77    */
78   uint64_t rel_value_us__ GNUNET_PACKED;
79 };
80
81
82 /**
83  * Time for absolute time used by GNUnet, in microseconds and in network byte order.
84  */
85 struct GNUNET_TIME_AbsoluteNBO
86 {
87   /**
88    * The actual value (in network byte order).
89    */
90   uint64_t abs_value_us__ GNUNET_PACKED;
91 };
92 GNUNET_NETWORK_STRUCT_END
93
94 /**
95  * Relative time zero.
96  */
97 #define GNUNET_TIME_UNIT_ZERO     GNUNET_TIME_relative_get_zero_()
98
99 /**
100  * Absolute time zero.
101  */
102 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
103
104 /**
105  * One microsecond, our basic time unit.
106  */
107 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_()
108
109 /**
110  * One millisecond.
111  */
112 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_()
113
114 /**
115  * One second.
116  */
117 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
118
119 /**
120  * One minute.
121  */
122 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
123
124 /**
125  * One hour.
126  */
127 #define GNUNET_TIME_UNIT_HOURS   GNUNET_TIME_relative_get_hour_()
128
129 /**
130  * One day.
131  */
132 #define GNUNET_TIME_UNIT_DAYS    GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS,   24)
133
134 /**
135  * One week.
136  */
137 #define GNUNET_TIME_UNIT_WEEKS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,     7)
138
139 /**
140  * One month (30 days).
141  */
142 #define GNUNET_TIME_UNIT_MONTHS  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,    30)
143
144 /**
145  * One year (365 days).
146  */
147 #define GNUNET_TIME_UNIT_YEARS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,   365)
148
149 /**
150  * Constant used to specify "forever".  This constant
151  * will be treated specially in all time operations.
152  */
153 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
154
155 /**
156  * Constant used to specify "forever".  This constant
157  * will be treated specially in all time operations.
158  */
159 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
160
161
162
163 /**
164  * Threshold after which exponential backoff should not increase (15 m).
165  */
166 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
167
168
169 /**
170  * Perform our standard exponential back-off calculation, starting at 1 ms
171  * and then going by a factor of 2 up unto a maximum of 15 m.
172  *
173  * @param r current backoff time, initially zero
174  */
175 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
176    GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
177
178
179 /**
180  * Randomized exponential back-off, starting at 1 ms
181  * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
182  * to a maximum of the given threshold.
183  *
184  * @param rt current backoff time, initially zero
185  * @param threshold maximum value for backoff
186  * @return the next backoff time
187  */
188 struct GNUNET_TIME_Relative
189 GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold);
190
191
192 /**
193  * Return a random time value between 0.5*r and 1.5*r.
194  * 
195  * @param r input time for scaling
196  * @return randomized time
197  */ 
198 struct GNUNET_TIME_Relative
199 GNUNET_TIME_randomize(struct GNUNET_TIME_Relative r);
200
201
202 /**
203  * Return relative time of 0ms.
204  */
205 struct GNUNET_TIME_Relative
206 GNUNET_TIME_relative_get_zero_ (void);
207
208
209 /**
210  * Return absolute time of 0ms.
211  */
212 struct GNUNET_TIME_Absolute
213 GNUNET_TIME_absolute_get_zero_ (void);
214
215
216 /**
217  * Return relative time of 1 microsecond.
218  */
219 struct GNUNET_TIME_Relative
220 GNUNET_TIME_relative_get_unit_ (void);
221
222
223 /**
224  * Return relative time of 1ms.
225  */
226 struct GNUNET_TIME_Relative
227 GNUNET_TIME_relative_get_millisecond_ (void);
228
229
230 /**
231  * Return relative time of 1s.
232  */
233 struct GNUNET_TIME_Relative
234 GNUNET_TIME_relative_get_second_ (void);
235
236
237 /**
238  * Return relative time of 1 minute.
239  */
240 struct GNUNET_TIME_Relative
241 GNUNET_TIME_relative_get_minute_ (void);
242
243
244 /**
245  * Return relative time of 1 hour.
246  */
247 struct GNUNET_TIME_Relative
248 GNUNET_TIME_relative_get_hour_ (void);
249
250
251 /**
252  * Return "forever".
253  */
254 struct GNUNET_TIME_Relative
255 GNUNET_TIME_relative_get_forever_ (void);
256
257
258 /**
259  * Return "forever".
260  */
261 struct GNUNET_TIME_Absolute
262 GNUNET_TIME_absolute_get_forever_ (void);
263
264
265 /**
266  * Get the current time.
267  *
268  * @return the current time
269  */
270 struct GNUNET_TIME_Absolute
271 GNUNET_TIME_absolute_get (void);
272
273
274 /**
275  * Convert relative time to an absolute time in the
276  * future.
277  *
278  * @param rel relative time to convert
279  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
280  */
281 struct GNUNET_TIME_Absolute
282 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
283
284
285 /**
286  * Round a time value so that it is suitable for transmission
287  * via JSON encodings.
288  *
289  * @param at time to round
290  * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
291  *         it was just now rounded
292  */
293 int
294 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at);
295
296
297 /**
298  * Round a time value so that it is suitable for transmission
299  * via JSON encodings.
300  *
301  * @param rt time to round
302  * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
303  *         it was just now rounded
304  */
305 int
306 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt);
307
308
309 /**
310  * Return the minimum of two relative time values.
311  *
312  * @param t1 first timestamp
313  * @param t2 other timestamp
314  * @return timestamp that is smaller
315  */
316 struct GNUNET_TIME_Relative
317 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
318                           struct GNUNET_TIME_Relative t2);
319
320
321
322 /**
323  * Return the maximum of two relative time values.
324  *
325  * @param t1 first timestamp
326  * @param t2 other timestamp
327  * @return timestamp that is larger
328  */
329 struct GNUNET_TIME_Relative
330 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
331                           struct GNUNET_TIME_Relative t2);
332
333
334 /**
335  * Return the minimum of two absolute time values.
336  *
337  * @param t1 first timestamp
338  * @param t2 other timestamp
339  * @return timestamp that is smaller
340  */
341 struct GNUNET_TIME_Absolute
342 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
343                           struct GNUNET_TIME_Absolute t2);
344
345
346 /**
347  * Return the maximum of two absolute time values.
348  *
349  * @param t1 first timestamp
350  * @param t2 other timestamp
351  * @return timestamp that is smaller
352  */
353 struct GNUNET_TIME_Absolute
354 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
355                           struct GNUNET_TIME_Absolute t2);
356
357
358 /**
359  * Given a timestamp in the future, how much time
360  * remains until then?
361  *
362  * @param future some absolute time, typically in the future
363  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
364  */
365 struct GNUNET_TIME_Relative
366 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
367
368
369 /**
370  * Calculate the estimate time of arrival/completion
371  * for an operation.
372  *
373  * @param start when did the operation start?
374  * @param finished how much has been done?
375  * @param total how much must be done overall (same unit as for "finished")
376  * @return remaining duration for the operation,
377  *        assuming it continues at the same speed
378  */
379 struct GNUNET_TIME_Relative
380 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
381                            uint64_t finished,
382                            uint64_t total);
383
384
385 /**
386  * Compute the time difference between the given start and end times.
387  * Use this function instead of actual subtraction to ensure that
388  * "FOREVER" and overflows are handeled correctly.
389  *
390  * @param start some absolute time
391  * @param end some absolute time (typically larger or equal to start)
392  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
393  */
394 struct GNUNET_TIME_Relative
395 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
396                                      struct GNUNET_TIME_Absolute end);
397
398
399 /**
400  * Get the duration of an operation as the
401  * difference of the current time and the given start time "hence".
402  *
403  * @param whence some absolute time, typically in the past
404  * @return 0 if hence > now, otherwise now-hence.
405  */
406 struct GNUNET_TIME_Relative
407 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
408
409
410 /**
411  * Add a given relative duration to the
412  * given start time.
413  *
414  * @param start some absolute time
415  * @param duration some relative time to add
416  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
417  */
418 struct GNUNET_TIME_Absolute
419 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
420                           struct GNUNET_TIME_Relative duration);
421
422
423 /**
424  * Subtract a given relative duration from the
425  * given start time.
426  *
427  * @param start some absolute time
428  * @param duration some relative time to subtract
429  * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
430  */
431 struct GNUNET_TIME_Absolute
432 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
433                                struct GNUNET_TIME_Relative duration);
434
435
436 /**
437  * Multiply relative time by a given factor.
438  *
439  * @param rel some duration
440  * @param factor integer to multiply with
441  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
442  */
443 struct GNUNET_TIME_Relative
444 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
445                                unsigned long long factor);
446
447
448 /**
449  * Saturating multiply relative time by a given factor.
450  *
451  * @param rel some duration
452  * @param factor integer to multiply with
453  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
454  */
455 struct GNUNET_TIME_Relative
456 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
457                                           unsigned long long factor);
458
459
460 /**
461  * Divide relative time by a given factor.
462  *
463  * @param rel some duration
464  * @param factor integer to divide by
465  * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
466  */
467 struct GNUNET_TIME_Relative
468 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
469                              unsigned long long factor);
470
471
472 /**
473  * Add relative times together.
474  *
475  * @param a1 some relative time
476  * @param a2 some other relative time
477  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
478  */
479 struct GNUNET_TIME_Relative
480 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
481                           struct GNUNET_TIME_Relative a2);
482
483
484 /**
485  * Subtract relative timestamp from the other.
486  *
487  * @param a1 first timestamp
488  * @param a2 second timestamp
489  * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
490  */
491 struct GNUNET_TIME_Relative
492 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
493                                struct GNUNET_TIME_Relative a2);
494
495
496 /**
497  * Convert relative time to network byte order.
498  *
499  * @param a time to convert
500  * @return converted time value
501  */
502 struct GNUNET_TIME_RelativeNBO
503 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
504
505
506 /**
507  * Convert relative time from network byte order.
508  *
509  * @param a time to convert
510  * @return converted time value
511  */
512 struct GNUNET_TIME_Relative
513 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
514
515
516 /**
517  * Convert absolute time to network byte order.
518  *
519  * @param a time to convert
520  * @return converted time value
521  */
522 struct GNUNET_TIME_AbsoluteNBO
523 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
524
525
526 /**
527  * Convert absolute time from network byte order.
528  *
529  * @param a time to convert
530  * @return converted time value
531  */
532 struct GNUNET_TIME_Absolute
533 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
534
535
536 /**
537  * Set the timestamp offset for this instance.
538  *
539  * @param offset the offset to skew the locale time by
540  */
541 void
542 GNUNET_TIME_set_offset (long long offset);
543
544
545 /**
546  * Get the timestamp offset for this instance.
547  *
548  * @return the offset we currently skew the locale time by
549  */
550 long long
551 GNUNET_TIME_get_offset (void);
552
553
554 /**
555  * Return the current year (i.e. '2011').
556  */
557 unsigned int
558 GNUNET_TIME_get_current_year (void);
559
560
561 /**
562  * Convert a year to an expiration time of January 1st of that year.
563  *
564  * @param year a year (after 1970, please ;-)).
565  * @return absolute time for January 1st of that year.
566  */
567 struct GNUNET_TIME_Absolute
568 GNUNET_TIME_year_to_time (unsigned int year);
569
570
571 /**
572  * Convert an expiration time to the respective year (rounds)
573  *
574  * @param at absolute time
575  * @return year a year (after 1970), 0 on error
576  */
577 unsigned int
578 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
579
580
581 /**
582  * A configuration object.
583  */
584 struct GNUNET_CONFIGURATION_Handle;
585
586
587 /**
588  * Obtain the current time and make sure it is monotonically
589  * increasing.  Guards against systems without an RTC or
590  * clocks running backwards and other nasty surprises. Does
591  * not guarantee that the returned time is near the current
592  * time returned by #GNUNET_TIME_absolute_get().  Two
593  * subsequent calls (within a short time period) may return the
594  * same value. Persists the last returned time on disk to
595  * ensure that time never goes backwards. As a result, the
596  * resulting value can be used to check if a message is the
597  * "most recent" value and replays of older messages (from
598  * the same origin) would be discarded.
599  *
600  * @param cfg configuration, used to determine where to
601  *   store the time; user can also insist RTC is working
602  *   nicely and disable the feature
603  * @return monotonically increasing time
604  */
605 struct GNUNET_TIME_Absolute
606 GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cfg);
607
608
609 #if 0                           /* keep Emacsens' auto-indent happy */
610 {
611 #endif
612 #ifdef __cplusplus
613 }
614 #endif
615
616 /* ifndef GNUNET_TIME_LIB_H */
617 #endif
618
619 /** @} */ /* end of group time */
620
621 /* end of gnunet_time_lib.h */