2 This file is part of GNUnet.
3 (C) 2001, 2002, 2006, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @author Christian Grothoff
24 * @brief functions for handling time and time arithmetic
27 #include "gnunet_time_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 * Variable used to simulate clock skew. Used for testing, never in production.
34 static long long timestamp_offset;
37 * Set the timestamp offset for this instance.
39 * @param offset the offset to skew the locale time by
42 GNUNET_TIME_set_offset (long long offset)
44 timestamp_offset = offset;
49 * Get the timestamp offset for this instance.
51 * @return the offset we currently skew the locale time by
54 GNUNET_TIME_get_offset ()
56 return timestamp_offset;
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)).
64 * @return the current time
66 struct GNUNET_TIME_Absolute
67 GNUNET_TIME_absolute_get ()
69 struct GNUNET_TIME_Absolute ret;
72 GETTIMEOFDAY (&tv, NULL);
74 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) +
75 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset;
81 * Return relative time of 0ms.
83 struct GNUNET_TIME_Relative
84 GNUNET_TIME_relative_get_zero_ ()
86 static struct GNUNET_TIME_Relative zero;
93 * Return absolute time of 0ms.
95 struct GNUNET_TIME_Absolute
96 GNUNET_TIME_absolute_get_zero_ ()
98 static struct GNUNET_TIME_Absolute zero;
105 * Return relative time of 1ms.
107 struct GNUNET_TIME_Relative
108 GNUNET_TIME_relative_get_unit_ ()
110 static struct GNUNET_TIME_Relative one = { 1 };
116 * Return relative time of 1s.
118 struct GNUNET_TIME_Relative
119 GNUNET_TIME_relative_get_second_ ()
121 static struct GNUNET_TIME_Relative one = { 1000 };
127 * Return relative time of 1 minute.
129 struct GNUNET_TIME_Relative
130 GNUNET_TIME_relative_get_minute_ ()
132 static struct GNUNET_TIME_Relative one = { 60 * 1000 };
138 * Return relative time of 1 hour.
140 struct GNUNET_TIME_Relative
141 GNUNET_TIME_relative_get_hour_ ()
143 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 };
151 struct GNUNET_TIME_Relative
152 GNUNET_TIME_relative_get_forever_ ()
154 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
161 struct GNUNET_TIME_Absolute
162 GNUNET_TIME_absolute_get_forever_ ()
164 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
169 * Convert relative time to an absolute time in the
172 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
174 struct GNUNET_TIME_Absolute
175 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
177 struct GNUNET_TIME_Absolute ret;
179 if (rel.rel_value == UINT64_MAX)
180 return GNUNET_TIME_UNIT_FOREVER_ABS;
181 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
183 if (rel.rel_value + now.abs_value < rel.rel_value)
185 GNUNET_break (0); /* overflow... */
186 return GNUNET_TIME_UNIT_FOREVER_ABS;
188 ret.abs_value = rel.rel_value + now.abs_value;
194 * Return the minimum of two relative time values.
196 * @param t1 first timestamp
197 * @param t2 other timestamp
198 * @return timestamp that is smaller
200 struct GNUNET_TIME_Relative
201 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
202 struct GNUNET_TIME_Relative t2)
204 return (t1.rel_value < t2.rel_value) ? t1 : t2;
209 * Return the maximum of two relative time values.
211 * @param t1 first timestamp
212 * @param t2 other timestamp
213 * @return timestamp that is larger
215 struct GNUNET_TIME_Relative
216 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
217 struct GNUNET_TIME_Relative t2)
219 return (t1.rel_value > t2.rel_value) ? t1 : t2;
225 * Return the minimum of two relative time values.
227 * @param t1 first timestamp
228 * @param t2 other timestamp
229 * @return timestamp that is smaller
231 struct GNUNET_TIME_Absolute
232 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
233 struct GNUNET_TIME_Absolute t2)
235 return (t1.abs_value < t2.abs_value) ? t1 : t2;
240 * Return the maximum of two relative time values.
242 * @param t1 first timestamp
243 * @param t2 other timestamp
244 * @return timestamp that is bigger
246 struct GNUNET_TIME_Absolute
247 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
248 struct GNUNET_TIME_Absolute t2)
250 return (t1.abs_value > t2.abs_value) ? t1 : t2;
255 * Given a timestamp in the future, how much time
256 * remains until then?
258 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
260 struct GNUNET_TIME_Relative
261 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
263 struct GNUNET_TIME_Relative ret;
265 if (future.abs_value == UINT64_MAX)
266 return GNUNET_TIME_UNIT_FOREVER_REL;
267 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
269 if (now.abs_value > future.abs_value)
270 return GNUNET_TIME_UNIT_ZERO;
271 ret.rel_value = future.abs_value - now.abs_value;
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.
280 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
282 struct GNUNET_TIME_Relative
283 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
284 struct GNUNET_TIME_Absolute end)
286 struct GNUNET_TIME_Relative ret;
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;
297 * Get the duration of an operation as the
298 * difference of the current time and the given start time "whence".
300 * @return aborts if whence==FOREVER, 0 if whence > now, otherwise now-whence.
302 struct GNUNET_TIME_Relative
303 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
305 struct GNUNET_TIME_Absolute now;
306 struct GNUNET_TIME_Relative ret;
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;
318 * Add a given relative duration to the
321 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
323 struct GNUNET_TIME_Absolute
324 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
325 struct GNUNET_TIME_Relative duration)
327 struct GNUNET_TIME_Absolute ret;
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)
334 return GNUNET_TIME_UNIT_FOREVER_ABS;
336 ret.abs_value = start.abs_value + duration.rel_value;
342 * Subtract a given relative duration from the
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
349 struct GNUNET_TIME_Absolute
350 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
351 struct GNUNET_TIME_Relative duration)
353 struct GNUNET_TIME_Absolute ret;
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;
365 * Multiply relative time by a given factor.
367 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
369 struct GNUNET_TIME_Relative
370 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
373 struct GNUNET_TIME_Relative ret;
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)
381 return GNUNET_TIME_UNIT_FOREVER_REL;
388 * Divide relative time by a given factor.
390 * @param rel some duration
391 * @param factor integer to divide by
392 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
394 struct GNUNET_TIME_Relative
395 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
398 struct GNUNET_TIME_Relative ret;
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;
409 * Calculate the estimate time of arrival/completion
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
418 struct GNUNET_TIME_Relative
419 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
422 struct GNUNET_TIME_Relative dur;
424 struct GNUNET_TIME_Relative ret;
426 GNUNET_break (finished <= total);
427 if (finished >= total)
428 return GNUNET_TIME_UNIT_ZERO;
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;
439 * Add relative times together.
441 * @param a1 first timestamp
442 * @param a2 second timestamp
443 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
445 struct GNUNET_TIME_Relative
446 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
447 struct GNUNET_TIME_Relative a2)
449 struct GNUNET_TIME_Relative ret;
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)
456 return GNUNET_TIME_UNIT_FOREVER_REL;
458 ret.rel_value = a1.rel_value + a2.rel_value;
464 * Subtract relative timestamp from the other.
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
470 struct GNUNET_TIME_Relative
471 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
472 struct GNUNET_TIME_Relative a2)
474 struct GNUNET_TIME_Relative ret;
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;
486 * Convert relative time to network byte order.
488 * @param a time to convert
489 * @return time in network byte order
491 struct GNUNET_TIME_RelativeNBO
492 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
494 struct GNUNET_TIME_RelativeNBO ret;
496 ret.rel_value__ = GNUNET_htonll (a.rel_value);
502 * Convert relative time from network byte order.
504 * @param a time to convert
505 * @return time in host byte order
507 struct GNUNET_TIME_Relative
508 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
510 struct GNUNET_TIME_Relative ret;
512 ret.rel_value = GNUNET_ntohll (a.rel_value__);
519 * Convert absolute time to network byte order.
521 * @param a time to convert
522 * @return time in network byte order
524 struct GNUNET_TIME_AbsoluteNBO
525 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
527 struct GNUNET_TIME_AbsoluteNBO ret;
529 ret.abs_value__ = GNUNET_htonll (a.abs_value);
535 * Convert absolute time from network byte order.
537 * @param a time to convert
538 * @return time in host byte order
540 struct GNUNET_TIME_Absolute
541 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
543 struct GNUNET_TIME_Absolute ret;
545 ret.abs_value = GNUNET_ntohll (a.abs_value__);