-dce
[oweals/gnunet.git] / src / util / time.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2006, 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 util/time.c
23  * @author Christian Grothoff
24  * @brief functions for handling time and time arithmetic
25  */
26 #include "platform.h"
27 #include "gnunet_time_lib.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
30
31 /**
32  * Variable used to simulate clock skew.  Used for testing, never in production.
33  */
34 static long long timestamp_offset;
35
36 /**
37  * Set the timestamp offset for this instance.
38  *
39  * @param offset the offset to skew the locale time by
40  */
41 void
42 GNUNET_TIME_set_offset (long long offset)
43 {
44   timestamp_offset = offset;
45 }
46
47 /**
48  * Get the current time (works just as "time", just that we use the
49  * unit of time that the cron-jobs use (and is 64 bit)).
50  *
51  * @return the current time
52  */
53 struct GNUNET_TIME_Absolute
54 GNUNET_TIME_absolute_get ()
55 {
56   struct GNUNET_TIME_Absolute ret;
57   struct timeval tv;
58
59   GETTIMEOFDAY (&tv, NULL);
60   ret.abs_value =
61       (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) +
62                   ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset;
63   return ret;
64 }
65
66
67 /**
68  * Return relative time of 0ms.
69  */
70 struct GNUNET_TIME_Relative
71 GNUNET_TIME_relative_get_zero ()
72 {
73   static struct GNUNET_TIME_Relative zero;
74
75   return zero;
76 }
77
78
79 /**
80  * Return absolute time of 0ms.
81  */
82 struct GNUNET_TIME_Absolute
83 GNUNET_TIME_absolute_get_zero ()
84 {
85   static struct GNUNET_TIME_Absolute zero;
86
87   return zero;
88 }
89
90 /**
91  * Return relative time of 1ms.
92  */
93 struct GNUNET_TIME_Relative
94 GNUNET_TIME_relative_get_unit ()
95 {
96   static struct GNUNET_TIME_Relative one = { 1 };
97   return one;
98 }
99
100 /**
101  * Return "forever".
102  */
103 struct GNUNET_TIME_Relative
104 GNUNET_TIME_relative_get_forever ()
105 {
106   static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
107   return forever;
108 }
109
110 /**
111  * Return "forever".
112  */
113 struct GNUNET_TIME_Absolute
114 GNUNET_TIME_absolute_get_forever ()
115 {
116   static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
117   return forever;
118 }
119
120 /**
121  * Convert relative time to an absolute time in the
122  * future.
123  *
124  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
125  */
126 struct GNUNET_TIME_Absolute
127 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
128 {
129   struct GNUNET_TIME_Absolute ret;
130
131   if (rel.rel_value == UINT64_MAX)
132     return GNUNET_TIME_absolute_get_forever ();
133   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
134
135   if (rel.rel_value + now.abs_value < rel.rel_value)
136   {
137     GNUNET_break (0);           /* overflow... */
138     return GNUNET_TIME_absolute_get_forever ();
139   }
140   ret.abs_value = rel.rel_value + now.abs_value;
141   return ret;
142 }
143
144
145 /**
146  * Return the minimum of two relative time values.
147  *
148  * @param t1 first timestamp
149  * @param t2 other timestamp
150  * @return timestamp that is smaller
151  */
152 struct GNUNET_TIME_Relative
153 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
154                           struct GNUNET_TIME_Relative t2)
155 {
156   return (t1.rel_value < t2.rel_value) ? t1 : t2;
157 }
158
159
160 /**
161  * Return the maximum of two relative time values.
162  *
163  * @param t1 first timestamp
164  * @param t2 other timestamp
165  * @return timestamp that is larger
166  */
167 struct GNUNET_TIME_Relative
168 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
169                           struct GNUNET_TIME_Relative t2)
170 {
171   return (t1.rel_value > t2.rel_value) ? t1 : t2;
172 }
173
174
175
176 /**
177  * Return the minimum of two relative time values.
178  *
179  * @param t1 first timestamp
180  * @param t2 other timestamp
181  * @return timestamp that is smaller
182  */
183 struct GNUNET_TIME_Absolute
184 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
185                           struct GNUNET_TIME_Absolute t2)
186 {
187   return (t1.abs_value < t2.abs_value) ? t1 : t2;
188 }
189
190
191 /**
192  * Return the maximum of two relative time values.
193  *
194  * @param t1 first timestamp
195  * @param t2 other timestamp
196  * @return timestamp that is smaller
197  */
198 struct GNUNET_TIME_Absolute
199 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
200                           struct GNUNET_TIME_Absolute t2)
201 {
202   return (t1.abs_value > t2.abs_value) ? t1 : t2;
203 }
204
205
206 /**
207  * Given a timestamp in the future, how much time
208  * remains until then?
209  *
210  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
211  */
212 struct GNUNET_TIME_Relative
213 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
214 {
215   struct GNUNET_TIME_Relative ret;
216
217   if (future.abs_value == UINT64_MAX)
218     return GNUNET_TIME_relative_get_forever ();
219   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
220
221   if (now.abs_value > future.abs_value)
222     return GNUNET_TIME_relative_get_zero ();
223   ret.rel_value = future.abs_value - now.abs_value;
224   return ret;
225 }
226
227 /**
228  * Compute the time difference between the given start and end times.
229  * Use this function instead of actual subtraction to ensure that
230  * "FOREVER" and overflows are handled correctly.
231  *
232  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
233  */
234 struct GNUNET_TIME_Relative
235 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
236                                      struct GNUNET_TIME_Absolute end)
237 {
238   struct GNUNET_TIME_Relative ret;
239
240   if (end.abs_value == UINT64_MAX)
241     return GNUNET_TIME_relative_get_forever ();
242   if (end.abs_value < start.abs_value)
243     return GNUNET_TIME_relative_get_zero ();
244   ret.rel_value = end.abs_value - start.abs_value;
245   return ret;
246 }
247
248 /**
249  * Get the duration of an operation as the
250  * difference of the current time and the given start time "whence".
251  *
252  * @return aborts if whence==FOREVER, 0 if whence > now, otherwise now-whence.
253  */
254 struct GNUNET_TIME_Relative
255 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
256 {
257   struct GNUNET_TIME_Absolute now;
258   struct GNUNET_TIME_Relative ret;
259
260   now = GNUNET_TIME_absolute_get ();
261   GNUNET_assert (whence.abs_value != UINT64_MAX);
262   if (whence.abs_value > now.abs_value)
263     return GNUNET_TIME_relative_get_zero ();
264   ret.rel_value = now.abs_value - whence.abs_value;
265   return ret;
266 }
267
268
269 /**
270  * Add a given relative duration to the
271  * given start time.
272  *
273  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
274  */
275 struct GNUNET_TIME_Absolute
276 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
277                           struct GNUNET_TIME_Relative duration)
278 {
279   struct GNUNET_TIME_Absolute ret;
280
281   if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX))
282     return GNUNET_TIME_absolute_get_forever ();
283   if (start.abs_value + duration.rel_value < start.abs_value)
284   {
285     GNUNET_break (0);
286     return GNUNET_TIME_absolute_get_forever ();
287   }
288   ret.abs_value = start.abs_value + duration.rel_value;
289   return ret;
290 }
291
292
293 /**
294  * Subtract a given relative duration from the
295  * given start time.
296  *
297  * @param start some absolute time
298  * @param duration some relative time to subtract
299  * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
300  */
301 struct GNUNET_TIME_Absolute
302 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
303                                struct GNUNET_TIME_Relative duration)
304 {
305   struct GNUNET_TIME_Absolute ret;
306
307   if (start.abs_value <= duration.rel_value)
308     return GNUNET_TIME_UNIT_ZERO_ABS;
309   if (start.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
310     return GNUNET_TIME_UNIT_FOREVER_ABS;
311   ret.abs_value = start.abs_value - duration.rel_value;
312   return ret;
313 }
314
315
316 /**
317  * Multiply relative time by a given factor.
318  *
319  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
320  */
321 struct GNUNET_TIME_Relative
322 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
323                                unsigned int factor)
324 {
325   struct GNUNET_TIME_Relative ret;
326
327   if (factor == 0)
328     return GNUNET_TIME_relative_get_zero ();
329   ret.rel_value = rel.rel_value * (unsigned long long) factor;
330   if (ret.rel_value / factor != rel.rel_value)
331   {
332     GNUNET_break (0);
333     return GNUNET_TIME_relative_get_forever ();
334   }
335   return ret;
336 }
337
338
339 /**
340  * Divide relative time by a given factor.
341  *
342  * @param rel some duration
343  * @param factor integer to divide by
344  * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
345  */
346 struct GNUNET_TIME_Relative
347 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
348                              unsigned int factor)
349 {
350   struct GNUNET_TIME_Relative ret;
351
352   if ((factor == 0) ||
353       (rel.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value))
354     return GNUNET_TIME_UNIT_FOREVER_REL;
355   ret.rel_value = rel.rel_value / (unsigned long long) factor;
356   return ret;
357 }
358
359
360 /**
361  * Calculate the estimate time of arrival/completion
362  * for an operation.
363  *
364  * @param start when did the operation start?
365  * @param finished how much has been done?
366  * @param total how much must be done overall (same unit as for "finished")
367  * @return remaining duration for the operation,
368  *        assuming it continues at the same speed
369  */
370 struct GNUNET_TIME_Relative
371 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
372                            uint64_t total)
373 {
374   struct GNUNET_TIME_Relative dur;
375   double exp;
376   struct GNUNET_TIME_Relative ret;
377
378   GNUNET_break (finished <= total);
379   if (finished >= total)
380     return GNUNET_TIME_UNIT_ZERO;
381   if (finished == 0)
382     return GNUNET_TIME_UNIT_FOREVER_REL;
383   dur = GNUNET_TIME_absolute_get_duration (start);
384   exp = ((double) dur.rel_value) * ((double) total) / ((double) finished);
385   ret.rel_value = ((uint64_t) exp) - dur.rel_value;
386   return ret;
387 }
388
389
390 /**
391  * Add relative times together.
392  *
393  * @param a1 first timestamp
394  * @param a2 second timestamp
395  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
396  */
397 struct GNUNET_TIME_Relative
398 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
399                           struct GNUNET_TIME_Relative a2)
400 {
401   struct GNUNET_TIME_Relative ret;
402
403   if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX))
404     return GNUNET_TIME_relative_get_forever ();
405   if (a1.rel_value + a2.rel_value < a1.rel_value)
406   {
407     GNUNET_break (0);
408     return GNUNET_TIME_relative_get_forever ();
409   }
410   ret.rel_value = a1.rel_value + a2.rel_value;
411   return ret;
412 }
413
414
415 /**
416  * Subtract relative timestamp from the other.
417  *
418  * @param a1 first timestamp
419  * @param a2 second timestamp
420  * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
421  */
422 struct GNUNET_TIME_Relative
423 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
424                                struct GNUNET_TIME_Relative a2)
425 {
426   struct GNUNET_TIME_Relative ret;
427
428   if (a2.rel_value >= a1.rel_value)
429     return GNUNET_TIME_relative_get_zero ();
430   if (a1.rel_value == UINT64_MAX)
431     return GNUNET_TIME_relative_get_forever ();
432   ret.rel_value = a1.rel_value - a2.rel_value;
433   return ret;
434 }
435
436
437 /**
438  * Convert relative time to network byte order.
439  *
440  * @param a time to convert
441  * @return time in network byte order
442  */
443 struct GNUNET_TIME_RelativeNBO
444 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
445 {
446   struct GNUNET_TIME_RelativeNBO ret;
447
448   ret.rel_value__ = GNUNET_htonll (a.rel_value);
449   return ret;
450 }
451
452 /**
453  * Convert relative time from network byte order.
454  *
455  * @param a time to convert
456  * @return time in host byte order
457  */
458 struct GNUNET_TIME_Relative
459 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
460 {
461   struct GNUNET_TIME_Relative ret;
462
463   ret.rel_value = GNUNET_ntohll (a.rel_value__);
464   return ret;
465
466 }
467
468 /**
469  * Convert absolute time to network byte order.
470  *
471  * @param a time to convert
472  * @return time in network byte order
473  */
474 struct GNUNET_TIME_AbsoluteNBO
475 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
476 {
477   struct GNUNET_TIME_AbsoluteNBO ret;
478
479   ret.abs_value__ = GNUNET_htonll (a.abs_value);
480   return ret;
481 }
482
483 /**
484  * Convert absolute time from network byte order.
485  *
486  * @param a time to convert
487  * @return time in host byte order
488  */
489 struct GNUNET_TIME_Absolute
490 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
491 {
492   struct GNUNET_TIME_Absolute ret;
493
494   ret.abs_value = GNUNET_ntohll (a.abs_value__);
495   return ret;
496
497 }
498
499 /**
500  * Convert a relative time to a string.
501  * This is one of the very few calls in the entire API that is
502  * NOT reentrant!
503  *
504  * @param time the time to print
505  *
506  * @return string form of the time (as milliseconds)
507  */
508 const char *
509 GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time)
510 {
511   static char time_string[21];
512
513   memset (time_string, 0, sizeof (time_string));
514
515   sprintf (time_string, "%llu", (unsigned long long) time.rel_value);
516   return (const char *) time_string;
517 }
518
519
520
521 /* end of time.c */