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