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