-fixes
[oweals/gnunet.git] / src / include / gnunet_time_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_time_lib.h
23  * @brief functions related to time
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_TIME_LIB_H
29 #define GNUNET_TIME_LIB_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_common.h"
40
41 /**
42  * Time for absolute times used by GNUnet, in milliseconds.
43  */
44 struct GNUNET_TIME_Absolute
45 {
46   /**
47    * The actual value.
48    */
49   uint64_t abs_value;
50 };
51
52 /**
53  * Time for relative time used by GNUnet, in milliseconds.
54  * Always positive, so we can only refer to future time.
55  */
56 struct GNUNET_TIME_Relative
57 {
58   /**
59    * The actual value.
60    */
61   uint64_t rel_value;
62 };
63
64 GNUNET_NETWORK_STRUCT_BEGIN
65
66 /**
67  * Time for relative time used by GNUnet, in milliseconds and in network byte order.
68  */
69 struct GNUNET_TIME_RelativeNBO
70 {
71   /**
72    * The actual value (in network byte order).
73    */
74   uint64_t rel_value__ GNUNET_PACKED;
75 };
76
77
78 /**
79  * Time for absolute time used by GNUnet, in milliseconds and in network byte order.
80  */
81 struct GNUNET_TIME_AbsoluteNBO
82 {
83   /**
84    * The actual value (in network byte order).
85    */
86   uint64_t abs_value__ GNUNET_PACKED;
87 };
88 GNUNET_NETWORK_STRUCT_END
89
90 /**
91  * Relative time zero.
92  */
93 #define GNUNET_TIME_UNIT_ZERO     GNUNET_TIME_relative_get_zero_()
94
95 /**
96  * Absolute time zero.
97  */
98 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
99
100 /**
101  * One millisecond, our basic time unit.
102  */
103 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit_()
104
105 /**
106  * One second.
107  */
108 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
109
110 /**
111  * One minute.
112  */
113 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
114
115 /**
116  * One hour.
117  */
118 #define GNUNET_TIME_UNIT_HOURS   GNUNET_TIME_relative_get_hour_()
119
120 /**
121  * One day.
122  */
123 #define GNUNET_TIME_UNIT_DAYS    GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS,   24)
124
125 /**
126  * One week.
127  */
128 #define GNUNET_TIME_UNIT_WEEKS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,     7)
129
130 /**
131  * One month (30 days).
132  */
133 #define GNUNET_TIME_UNIT_MONTHS  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,    30)
134
135 /**
136  * One year (365 days).
137  */
138 #define GNUNET_TIME_UNIT_YEARS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,   365)
139
140 /**
141  * Constant used to specify "forever".  This constant
142  * will be treated specially in all time operations.
143  */
144 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
145
146 /**
147  * Constant used to specify "forever".  This constant
148  * will be treated specially in all time operations.
149  */
150 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
151
152
153
154 /**
155  * Threshold after which exponential backoff should not increase (15 m).
156  */
157 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
158
159
160 /**
161  * Perform our standard exponential back-off calculation, starting at 1mst
162  * and then going by a factor of 2 up unto a maximum of 1s.
163  *
164  * @param r current backoff time, initially zero
165  */
166 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
167    GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
168
169 /**
170  * Return relative time of 0ms.
171  */
172 struct GNUNET_TIME_Relative
173 GNUNET_TIME_relative_get_zero_ (void);
174
175
176 /**
177  * Return absolute time of 0ms.
178  */
179 struct GNUNET_TIME_Absolute
180 GNUNET_TIME_absolute_get_zero_ (void);
181
182
183 /**
184  * Return relative time of 1ms.
185  */
186 struct GNUNET_TIME_Relative
187 GNUNET_TIME_relative_get_unit_ (void);
188
189
190 /**
191  * Return relative time of 1s.
192  */
193 struct GNUNET_TIME_Relative
194 GNUNET_TIME_relative_get_second_ (void);
195
196
197 /**
198  * Return relative time of 1 minute.
199  */
200 struct GNUNET_TIME_Relative
201 GNUNET_TIME_relative_get_minute_ (void);
202
203
204 /**
205  * Return relative time of 1 hour.
206  */
207 struct GNUNET_TIME_Relative
208 GNUNET_TIME_relative_get_hour_ (void);
209
210
211 /**
212  * Return "forever".
213  */
214 struct GNUNET_TIME_Relative
215 GNUNET_TIME_relative_get_forever_ (void);
216
217
218 /**
219  * Return "forever".
220  */
221 struct GNUNET_TIME_Absolute
222 GNUNET_TIME_absolute_get_forever_ (void);
223
224
225 /**
226  * Get the current time.
227  *
228  * @return the current time
229  */
230 struct GNUNET_TIME_Absolute
231 GNUNET_TIME_absolute_get (void);
232
233
234 /**
235  * Convert relative time to an absolute time in the
236  * future.
237  *
238  * @param rel relative time to convert
239  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
240  */
241 struct GNUNET_TIME_Absolute
242 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
243
244
245 /**
246  * Return the minimum of two relative time values.
247  *
248  * @param t1 first timestamp
249  * @param t2 other timestamp
250  * @return timestamp that is smaller
251  */
252 struct GNUNET_TIME_Relative
253 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
254                           struct GNUNET_TIME_Relative t2);
255
256
257
258 /**
259  * Return the maximum of two relative time values.
260  *
261  * @param t1 first timestamp
262  * @param t2 other timestamp
263  * @return timestamp that is larger
264  */
265 struct GNUNET_TIME_Relative
266 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
267                           struct GNUNET_TIME_Relative t2);
268
269
270 /**
271  * Return the minimum of two absolute time values.
272  *
273  * @param t1 first timestamp
274  * @param t2 other timestamp
275  * @return timestamp that is smaller
276  */
277 struct GNUNET_TIME_Absolute
278 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
279                           struct GNUNET_TIME_Absolute t2);
280
281
282 /**
283  * Return the maximum of two absolute time values.
284  *
285  * @param t1 first timestamp
286  * @param t2 other timestamp
287  * @return timestamp that is smaller
288  */
289 struct GNUNET_TIME_Absolute
290 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
291                           struct GNUNET_TIME_Absolute t2);
292
293
294 /**
295  * Given a timestamp in the future, how much time
296  * remains until then?
297  *
298  * @param future some absolute time, typically in the future
299  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
300  */
301 struct GNUNET_TIME_Relative
302 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
303
304
305 /**
306  * Calculate the estimate time of arrival/completion
307  * for an operation.
308  *
309  * @param start when did the operation start?
310  * @param finished how much has been done?
311  * @param total how much must be done overall (same unit as for "finished")
312  * @return remaining duration for the operation,
313  *        assuming it continues at the same speed
314  */
315 struct GNUNET_TIME_Relative
316 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
317                            uint64_t total);
318
319
320 /**
321  * Compute the time difference between the given start and end times.
322  * Use this function instead of actual subtraction to ensure that
323  * "FOREVER" and overflows are handeled correctly.
324  *
325  * @param start some absolute time
326  * @param end some absolute time (typically larger or equal to start)
327  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
328  */
329 struct GNUNET_TIME_Relative
330 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
331                                      struct GNUNET_TIME_Absolute end);
332
333
334 /**
335  * Get the duration of an operation as the
336  * difference of the current time and the given start time "hence".
337  *
338  * @param whence some absolute time, typically in the past
339  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
340  */
341 struct GNUNET_TIME_Relative
342 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
343
344
345 /**
346  * Add a given relative duration to the
347  * given start time.
348  *
349  * @param start some absolute time
350  * @param duration some relative time to add
351  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
352  */
353 struct GNUNET_TIME_Absolute
354 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
355                           struct GNUNET_TIME_Relative duration);
356
357
358 /**
359  * Subtract a given relative duration from the
360  * given start time.
361  *
362  * @param start some absolute time
363  * @param duration some relative time to subtract
364  * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
365  */
366 struct GNUNET_TIME_Absolute
367 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
368                                struct GNUNET_TIME_Relative duration);
369
370
371 /**
372  * Multiply relative time by a given factor.
373  *
374  * @param rel some duration
375  * @param factor integer to multiply with
376  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
377  */
378 struct GNUNET_TIME_Relative
379 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
380                                unsigned int factor);
381
382
383 /**
384  * Divide relative time by a given factor.
385  *
386  * @param rel some duration
387  * @param factor integer to divide by
388  * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
389  */
390 struct GNUNET_TIME_Relative
391 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
392                              unsigned int factor);
393
394
395 /**
396  * Add relative times together.
397  *
398  * @param a1 some relative time
399  * @param a2 some other relative time
400  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
401  */
402 struct GNUNET_TIME_Relative
403 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
404                           struct GNUNET_TIME_Relative a2);
405
406
407 /**
408  * Subtract relative timestamp from the other.
409  *
410  * @param a1 first timestamp
411  * @param a2 second timestamp
412  * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
413  */
414 struct GNUNET_TIME_Relative
415 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
416                                struct GNUNET_TIME_Relative a2);
417
418
419 /**
420  * Convert relative time to network byte order.
421  *
422  * @param a time to convert
423  * @return converted time value
424  */
425 struct GNUNET_TIME_RelativeNBO
426 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
427
428
429 /**
430  * Convert relative time from network byte order.
431  *
432  * @param a time to convert
433  * @return converted time value
434  */
435 struct GNUNET_TIME_Relative
436 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
437
438
439 /**
440  * Convert relative time to network byte order.
441  *
442  * @param a time to convert
443  * @return converted time value
444  */
445 struct GNUNET_TIME_AbsoluteNBO
446 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
447
448
449 /**
450  * Convert relative time from network byte order.
451  *
452  * @param a time to convert
453  * @return converted time value
454  */
455 struct GNUNET_TIME_Absolute
456 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
457
458
459 /**
460  * Set the timestamp offset for this instance.
461  *
462  * @param offset the offset to skew the locale time by
463  */
464 void
465 GNUNET_TIME_set_offset (long long offset);
466
467
468 #if 0                           /* keep Emacsens' auto-indent happy */
469 {
470 #endif
471 #ifdef __cplusplus
472 }
473 #endif
474
475 /* ifndef GNUNET_TIME_LIB_H */
476 #endif
477 /* end of gnunet_time_lib.h */