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