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