-note duplicate API
[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  * Return relative time of 0ms.
155  */
156 struct GNUNET_TIME_Relative
157 GNUNET_TIME_relative_get_zero_ (void);
158
159
160 /**
161  * Return absolute time of 0ms.
162  */
163 struct GNUNET_TIME_Absolute
164 GNUNET_TIME_absolute_get_zero_ (void);
165
166
167 /**
168  * Return relative time of 1ms.
169  */
170 struct GNUNET_TIME_Relative
171 GNUNET_TIME_relative_get_unit_ (void);
172
173
174 /**
175  * Return relative time of 1s.
176  */
177 struct GNUNET_TIME_Relative
178 GNUNET_TIME_relative_get_second_ (void);
179
180
181 /**
182  * Return relative time of 1 minute.
183  */
184 struct GNUNET_TIME_Relative
185 GNUNET_TIME_relative_get_minute_ (void);
186
187
188 /**
189  * Return relative time of 1 hour.
190  */
191 struct GNUNET_TIME_Relative
192 GNUNET_TIME_relative_get_hour_ (void);
193
194
195 /**
196  * Return "forever".
197  */
198 struct GNUNET_TIME_Relative
199 GNUNET_TIME_relative_get_forever_ (void);
200
201
202 /**
203  * Return "forever".
204  */
205 struct GNUNET_TIME_Absolute
206 GNUNET_TIME_absolute_get_forever_ (void);
207
208
209 /**
210  * Get the current time.
211  *
212  * @return the current time
213  */
214 struct GNUNET_TIME_Absolute
215 GNUNET_TIME_absolute_get (void);
216
217
218 /**
219  * Convert relative time to an absolute time in the
220  * future.
221  *
222  * @param rel relative time to convert
223  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
224  */
225 struct GNUNET_TIME_Absolute
226 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
227
228
229 /**
230  * Return the minimum of two relative time values.
231  *
232  * @param t1 first timestamp
233  * @param t2 other timestamp
234  * @return timestamp that is smaller
235  */
236 struct GNUNET_TIME_Relative
237 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
238                           struct GNUNET_TIME_Relative t2);
239
240
241
242 /**
243  * Return the maximum of two relative time values.
244  *
245  * @param t1 first timestamp
246  * @param t2 other timestamp
247  * @return timestamp that is larger
248  */
249 struct GNUNET_TIME_Relative
250 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
251                           struct GNUNET_TIME_Relative t2);
252
253
254 /**
255  * Return the minimum of two absolute time values.
256  *
257  * @param t1 first timestamp
258  * @param t2 other timestamp
259  * @return timestamp that is smaller
260  */
261 struct GNUNET_TIME_Absolute
262 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
263                           struct GNUNET_TIME_Absolute t2);
264
265
266 /**
267  * Return the maximum of two absolute time values.
268  *
269  * @param t1 first timestamp
270  * @param t2 other timestamp
271  * @return timestamp that is smaller
272  */
273 struct GNUNET_TIME_Absolute
274 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
275                           struct GNUNET_TIME_Absolute t2);
276
277
278 /**
279  * Given a timestamp in the future, how much time
280  * remains until then?
281  *
282  * @param future some absolute time, typically in the future
283  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
284  */
285 struct GNUNET_TIME_Relative
286 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
287
288
289 /**
290  * Calculate the estimate time of arrival/completion
291  * for an operation.
292  *
293  * @param start when did the operation start?
294  * @param finished how much has been done?
295  * @param total how much must be done overall (same unit as for "finished")
296  * @return remaining duration for the operation,
297  *        assuming it continues at the same speed
298  */
299 struct GNUNET_TIME_Relative
300 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
301                            uint64_t total);
302
303
304 /**
305  * Compute the time difference between the given start and end times.
306  * Use this function instead of actual subtraction to ensure that
307  * "FOREVER" and overflows are handeled correctly.
308  *
309  * @param start some absolute time
310  * @param end some absolute time (typically larger or equal to start)
311  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
312  */
313 struct GNUNET_TIME_Relative
314 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
315                                      struct GNUNET_TIME_Absolute end);
316
317
318 /**
319  * Get the duration of an operation as the
320  * difference of the current time and the given start time "hence".
321  *
322  * @param whence some absolute time, typically in the past
323  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
324  */
325 struct GNUNET_TIME_Relative
326 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
327
328
329 /**
330  * Add a given relative duration to the
331  * given start time.
332  *
333  * @param start some absolute time
334  * @param duration some relative time to add
335  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
336  */
337 struct GNUNET_TIME_Absolute
338 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
339                           struct GNUNET_TIME_Relative duration);
340
341
342 /**
343  * Subtract a given relative duration from the
344  * given start time.
345  *
346  * @param start some absolute time
347  * @param duration some relative time to subtract
348  * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
349  */
350 struct GNUNET_TIME_Absolute
351 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
352                                struct GNUNET_TIME_Relative duration);
353
354
355 /**
356  * Multiply relative time by a given factor.
357  *
358  * @param rel some duration
359  * @param factor integer to multiply with
360  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
361  */
362 struct GNUNET_TIME_Relative
363 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
364                                unsigned int factor);
365
366
367 /**
368  * Divide relative time by a given factor.
369  *
370  * @param rel some duration
371  * @param factor integer to divide by
372  * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
373  */
374 struct GNUNET_TIME_Relative
375 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
376                              unsigned int factor);
377
378
379 /**
380  * Add relative times together.
381  *
382  * @param a1 some relative time
383  * @param a2 some other relative time
384  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
385  */
386 struct GNUNET_TIME_Relative
387 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
388                           struct GNUNET_TIME_Relative a2);
389
390
391 /**
392  * Subtract relative timestamp from the other.
393  *
394  * @param a1 first timestamp
395  * @param a2 second timestamp
396  * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
397  */
398 struct GNUNET_TIME_Relative
399 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
400                                struct GNUNET_TIME_Relative a2);
401
402
403 /**
404  * Convert relative time to network byte order.
405  *
406  * @param a time to convert
407  * @return converted time value
408  */
409 struct GNUNET_TIME_RelativeNBO
410 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
411
412
413 /**
414  * Convert relative time from network byte order.
415  *
416  * @param a time to convert
417  * @return converted time value
418  */
419 struct GNUNET_TIME_Relative
420 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
421
422
423 /**
424  * Convert relative time to network byte order.
425  *
426  * @param a time to convert
427  * @return converted time value
428  */
429 struct GNUNET_TIME_AbsoluteNBO
430 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
431
432
433 /**
434  * Convert relative time from network byte order.
435  *
436  * @param a time to convert
437  * @return converted time value
438  */
439 struct GNUNET_TIME_Absolute
440 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
441
442
443 /**
444  * Convert a relative time to a string.
445  * NOT reentrant!  FIXME: duplicate with GNUNET_STRINGS_relative_time_to_string!
446  *
447  * @param time the time to print
448  *
449  * @return string form of the time (as milliseconds)
450  */
451 const char *
452 GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time);
453
454
455 /**
456  * Set the timestamp offset for this instance.
457  *
458  * @param offset the offset to skew the locale time by
459  */
460 void
461 GNUNET_TIME_set_offset (long long offset);
462
463
464 #if 0                           /* keep Emacsens' auto-indent happy */
465 {
466 #endif
467 #ifdef __cplusplus
468 }
469 #endif
470
471 /* ifndef GNUNET_TIME_LIB_H */
472 #endif
473 /* end of gnunet_time_lib.h */