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