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