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