indentation
[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
65 /**
66  * Time for relative time used by GNUnet, in milliseconds and in network byte order.
67  */
68 struct GNUNET_TIME_RelativeNBO
69 {
70   /**
71    * The actual value (in network byte order).
72    */
73   uint64_t rel_value__ GNUNET_PACKED;
74 };
75
76
77 /**
78  * Time for absolute time used by GNUnet, in milliseconds and in network byte order.
79  */
80 struct GNUNET_TIME_AbsoluteNBO
81 {
82   /**
83    * The actual value (in network byte order).
84    */
85   uint64_t abs_value__ GNUNET_PACKED;
86 };
87
88
89 /**
90  * Relative time zero.
91  */
92 #define GNUNET_TIME_UNIT_ZERO     GNUNET_TIME_relative_get_zero()
93
94 /**
95  * Absolute time zero.
96  */
97 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero()
98
99 /**
100  * One millisecond, our basic time unit.
101  */
102 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit()
103
104 /**
105  * One second.
106  */
107 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1000)
108
109 /**
110  * One minute.
111  */
112 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
113
114 /**
115  * One hour.
116  */
117 #define GNUNET_TIME_UNIT_HOURS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
118
119 /**
120  * One day.
121  */
122 #define GNUNET_TIME_UNIT_DAYS    GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS,   24)
123
124 /**
125  * One week.
126  */
127 #define GNUNET_TIME_UNIT_WEEKS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,     7)
128
129 /**
130  * One month (30 days).
131  */
132 #define GNUNET_TIME_UNIT_MONTHS  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,    30)
133
134 /**
135  * One year (365 days).
136  */
137 #define GNUNET_TIME_UNIT_YEARS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,   365)
138
139 /**
140  * Constant used to specify "forever".  This constant
141  * will be treated specially in all time operations.
142  */
143 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever ()
144
145 /**
146  * Constant used to specify "forever".  This constant
147  * will be treated specially in all time operations.
148  */
149 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever ()
150
151 /**
152  * Return relative time of 0ms.
153  */
154 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero (void);
155
156 /**
157  * Return absolute time of 0ms.
158  */
159 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero (void);
160
161 /**
162  * Return relative time of 1ms.
163  */
164 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit (void);
165
166 /**
167  * Return "forever".
168  */
169 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever (void);
170
171 /**
172  * Return "forever".
173  */
174 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever (void);
175
176 /**
177  * Get the current time.
178  *
179  * @return the current time
180  */
181 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get (void);
182
183 /**
184  * Convert relative time to an absolute time in the
185  * future.
186  *
187  * @param rel relative time to convert
188  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
189  */
190 struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute (struct
191                                                               GNUNET_TIME_Relative
192                                                               rel);
193
194 /**
195  * Return the minimum of two relative time values.
196  *
197  * @param t1 first timestamp
198  * @param t2 other timestamp
199  * @return timestamp that is smaller
200  */
201 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
202                                                       GNUNET_TIME_Relative
203                                                       t1,
204                                                       struct
205                                                       GNUNET_TIME_Relative t2);
206
207
208 /**
209  * Return the maximum of two relative time values.
210  *
211  * @param t1 first timestamp
212  * @param t2 other timestamp
213  * @return timestamp that is larger
214  */
215 struct GNUNET_TIME_Relative GNUNET_TIME_relative_max (struct
216                                                       GNUNET_TIME_Relative
217                                                       t1,
218                                                       struct
219                                                       GNUNET_TIME_Relative t2);
220
221 /**
222  * Return the minimum of two absolute time values.
223  *
224  * @param t1 first timestamp
225  * @param t2 other timestamp
226  * @return timestamp that is smaller
227  */
228 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_min (struct
229                                                       GNUNET_TIME_Absolute
230                                                       t1,
231                                                       struct
232                                                       GNUNET_TIME_Absolute t2);
233
234 /**
235  * Return the maximum of two absolute time values.
236  *
237  * @param t1 first timestamp
238  * @param t2 other timestamp
239  * @return timestamp that is smaller
240  */
241 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_max (struct
242                                                       GNUNET_TIME_Absolute
243                                                       t1,
244                                                       struct
245                                                       GNUNET_TIME_Absolute t2);
246
247 /**
248  * Given a timestamp in the future, how much time
249  * remains until then?
250  *
251  * @param future some absolute time, typically in the future
252  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
253  */
254 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining (struct
255                                                                 GNUNET_TIME_Absolute
256                                                                 future);
257
258
259 /**
260  * Calculate the estimate time of arrival/completion 
261  * for an operation.
262  *
263  * @param start when did the operation start?
264  * @param finished how much has been done?
265  * @param total how much must be done overall (same unit as for "finished")
266  * @return remaining duration for the operation,
267  *        assuming it continues at the same speed
268  */
269 struct GNUNET_TIME_Relative GNUNET_TIME_calculate_eta (struct
270                                                        GNUNET_TIME_Absolute
271                                                        start, uint64_t finished,
272                                                        uint64_t total);
273
274
275 /**
276  * Compute the time difference between the given start and end times.
277  * Use this function instead of actual subtraction to ensure that
278  * "FOREVER" and overflows are handeled correctly.
279  *
280  * @param start some absolute time
281  * @param end some absolute time (typically larger or equal to start)
282  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
283  */
284 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference (struct
285                                                                  GNUNET_TIME_Absolute
286                                                                  start,
287                                                                  struct
288                                                                  GNUNET_TIME_Absolute
289                                                                  end);
290
291 /**
292  * Get the duration of an operation as the
293  * difference of the current time and the given start time "hence".
294  *
295  * @param whence some absolute time, typically in the past
296  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
297  */
298 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration (struct
299                                                                GNUNET_TIME_Absolute
300                                                                whence);
301
302
303 /**
304  * Add a given relative duration to the
305  * given start time.
306  *
307  * @param start some absolute time
308  * @param duration some relative time to add
309  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
310  */
311 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add (struct
312                                                       GNUNET_TIME_Absolute
313                                                       start,
314                                                       struct
315                                                       GNUNET_TIME_Relative
316                                                       duration);
317
318
319 /**
320  * Subtract a given relative duration from the
321  * given start time.
322  *
323  * @param start some absolute time
324  * @param duration some relative time to subtract
325  * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
326  */
327 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_subtract (struct
328                                                            GNUNET_TIME_Absolute
329                                                            start,
330                                                            struct
331                                                            GNUNET_TIME_Relative
332                                                            duration);
333
334 /**
335  * Multiply relative time by a given factor.
336  *
337  * @param rel some duration
338  * @param factor integer to multiply with
339  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
340  */
341 struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply (struct
342                                                            GNUNET_TIME_Relative
343                                                            rel,
344                                                            unsigned int factor);
345
346 /**
347  * Divide relative time by a given factor.
348  *
349  * @param rel some duration
350  * @param factor integer to divide by
351  * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
352  */
353 struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide (struct
354                                                          GNUNET_TIME_Relative
355                                                          rel,
356                                                          unsigned int factor);
357
358 /**
359  * Add relative times together.
360  *
361  * @param a1 some relative time
362  * @param a2 some other relative time
363  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
364  */
365 struct GNUNET_TIME_Relative GNUNET_TIME_relative_add (struct
366                                                       GNUNET_TIME_Relative a1,
367                                                       struct
368                                                       GNUNET_TIME_Relative a2);
369
370 /**
371  * Subtract relative timestamp from the other.
372  *
373  * @param a1 first timestamp
374  * @param a2 second timestamp
375  * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
376  */
377 struct GNUNET_TIME_Relative
378 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
379                                struct GNUNET_TIME_Relative a2);
380
381
382 /**
383  * Convert relative time to network byte order.
384  * 
385  * @param a time to convert
386  * @return converted time value
387  */
388 struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton (struct
389                                                           GNUNET_TIME_Relative
390                                                           a);
391
392 /**
393  * Convert relative time from network byte order.
394  *
395  * @param a time to convert
396  * @return converted time value
397  */
398 struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh (struct
399                                                        GNUNET_TIME_RelativeNBO
400                                                        a);
401
402 /**
403  * Convert relative time to network byte order.
404  *
405  * @param a time to convert
406  * @return converted time value
407  */
408 struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton (struct
409                                                           GNUNET_TIME_Absolute
410                                                           a);
411
412 /**
413  * Convert relative time from network byte order.
414  *
415  * @param a time to convert
416  * @return converted time value
417  */
418 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh (struct
419                                                        GNUNET_TIME_AbsoluteNBO
420                                                        a);
421
422 /**
423  * Convert a relative time to a string.
424  * NOT reentrant!
425  *
426  * @param time the time to print
427  *
428  * @return string form of the time (as milliseconds)
429  */
430 const char *GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time);
431
432 /**
433  * Set the timestamp offset for this instance.
434  *
435  * @param offset the offset to skew the locale time by
436  */
437 void GNUNET_TIME_set_offset (long long offset);
438
439 #if 0                           /* keep Emacsens' auto-indent happy */
440 {
441 #endif
442 #ifdef __cplusplus
443 }
444 #endif
445
446 /* ifndef GNUNET_TIME_LIB_H */
447 #endif
448 /* end of gnunet_time_lib.h */