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;
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)).
51 * @return the current time
53 struct GNUNET_TIME_Absolute
54 GNUNET_TIME_absolute_get ()
56 struct GNUNET_TIME_Absolute ret;
59 GETTIMEOFDAY (&tv, NULL);
61 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) +
62 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset;
68 * Return relative time of 0ms.
70 struct GNUNET_TIME_Relative
71 GNUNET_TIME_relative_get_zero_ ()
73 static struct GNUNET_TIME_Relative zero;
80 * Return absolute time of 0ms.
82 struct GNUNET_TIME_Absolute
83 GNUNET_TIME_absolute_get_zero_ ()
85 static struct GNUNET_TIME_Absolute zero;
92 * Return relative time of 1ms.
94 struct GNUNET_TIME_Relative
95 GNUNET_TIME_relative_get_unit_ ()
97 static struct GNUNET_TIME_Relative one = { 1 };
103 * Return relative time of 1s.
105 struct GNUNET_TIME_Relative
106 GNUNET_TIME_relative_get_second_ ()
108 static struct GNUNET_TIME_Relative one = { 1000 };
114 * Return relative time of 1 minute.
116 struct GNUNET_TIME_Relative
117 GNUNET_TIME_relative_get_minute_ ()
119 static struct GNUNET_TIME_Relative one = { 60 * 1000 };
125 * Return relative time of 1 hour.
127 struct GNUNET_TIME_Relative
128 GNUNET_TIME_relative_get_hour_ ()
130 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 };
138 struct GNUNET_TIME_Relative
139 GNUNET_TIME_relative_get_forever_ ()
141 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
148 struct GNUNET_TIME_Absolute
149 GNUNET_TIME_absolute_get_forever_ ()
151 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
156 * Convert relative time to an absolute time in the
159 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
161 struct GNUNET_TIME_Absolute
162 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
164 struct GNUNET_TIME_Absolute ret;
166 if (rel.rel_value == UINT64_MAX)
167 return GNUNET_TIME_UNIT_FOREVER_ABS;
168 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
170 if (rel.rel_value + now.abs_value < rel.rel_value)
172 GNUNET_break (0); /* overflow... */
173 return GNUNET_TIME_UNIT_FOREVER_ABS;
175 ret.abs_value = rel.rel_value + now.abs_value;
181 * Return the minimum of two relative time values.
183 * @param t1 first timestamp
184 * @param t2 other timestamp
185 * @return timestamp that is smaller
187 struct GNUNET_TIME_Relative
188 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
189 struct GNUNET_TIME_Relative t2)
191 return (t1.rel_value < t2.rel_value) ? t1 : t2;
196 * Return the maximum of two relative time values.
198 * @param t1 first timestamp
199 * @param t2 other timestamp
200 * @return timestamp that is larger
202 struct GNUNET_TIME_Relative
203 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
204 struct GNUNET_TIME_Relative t2)
206 return (t1.rel_value > t2.rel_value) ? t1 : t2;
212 * Return the minimum of two relative time values.
214 * @param t1 first timestamp
215 * @param t2 other timestamp
216 * @return timestamp that is smaller
218 struct GNUNET_TIME_Absolute
219 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
220 struct GNUNET_TIME_Absolute t2)
222 return (t1.abs_value < t2.abs_value) ? t1 : t2;
227 * Return the maximum of two relative time values.
229 * @param t1 first timestamp
230 * @param t2 other timestamp
231 * @return timestamp that is bigger
233 struct GNUNET_TIME_Absolute
234 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
235 struct GNUNET_TIME_Absolute t2)
237 return (t1.abs_value > t2.abs_value) ? t1 : t2;
242 * Given a timestamp in the future, how much time
243 * remains until then?
245 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
247 struct GNUNET_TIME_Relative
248 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
250 struct GNUNET_TIME_Relative ret;
252 if (future.abs_value == UINT64_MAX)
253 return GNUNET_TIME_UNIT_FOREVER_REL;
254 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
256 if (now.abs_value > future.abs_value)
257 return GNUNET_TIME_UNIT_ZERO;
258 ret.rel_value = future.abs_value - now.abs_value;
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.
267 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
269 struct GNUNET_TIME_Relative
270 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
271 struct GNUNET_TIME_Absolute end)
273 struct GNUNET_TIME_Relative ret;
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;
284 * Get the duration of an operation as the
285 * difference of the current time and the given start time "whence".
287 * @return aborts if whence==FOREVER, 0 if whence > now, otherwise now-whence.
289 struct GNUNET_TIME_Relative
290 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
292 struct GNUNET_TIME_Absolute now;
293 struct GNUNET_TIME_Relative ret;
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;
305 * Add a given relative duration to the
308 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
310 struct GNUNET_TIME_Absolute
311 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
312 struct GNUNET_TIME_Relative duration)
314 struct GNUNET_TIME_Absolute ret;
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)
321 return GNUNET_TIME_UNIT_FOREVER_ABS;
323 ret.abs_value = start.abs_value + duration.rel_value;
329 * Subtract a given relative duration from the
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
336 struct GNUNET_TIME_Absolute
337 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
338 struct GNUNET_TIME_Relative duration)
340 struct GNUNET_TIME_Absolute ret;
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;
352 * Multiply relative time by a given factor.
354 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
356 struct GNUNET_TIME_Relative
357 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
360 struct GNUNET_TIME_Relative ret;
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)
368 return GNUNET_TIME_UNIT_FOREVER_REL;
375 * Divide relative time by a given factor.
377 * @param rel some duration
378 * @param factor integer to divide by
379 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
381 struct GNUNET_TIME_Relative
382 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
385 struct GNUNET_TIME_Relative ret;
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;
396 * Calculate the estimate time of arrival/completion
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
405 struct GNUNET_TIME_Relative
406 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
409 struct GNUNET_TIME_Relative dur;
411 struct GNUNET_TIME_Relative ret;
413 GNUNET_break (finished <= total);
414 if (finished >= total)
415 return GNUNET_TIME_UNIT_ZERO;
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;
426 * Add relative times together.
428 * @param a1 first timestamp
429 * @param a2 second timestamp
430 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
432 struct GNUNET_TIME_Relative
433 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
434 struct GNUNET_TIME_Relative a2)
436 struct GNUNET_TIME_Relative ret;
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)
443 return GNUNET_TIME_UNIT_FOREVER_REL;
445 ret.rel_value = a1.rel_value + a2.rel_value;
451 * Subtract relative timestamp from the other.
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
457 struct GNUNET_TIME_Relative
458 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
459 struct GNUNET_TIME_Relative a2)
461 struct GNUNET_TIME_Relative ret;
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;
473 * Convert relative time to network byte order.
475 * @param a time to convert
476 * @return time in network byte order
478 struct GNUNET_TIME_RelativeNBO
479 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
481 struct GNUNET_TIME_RelativeNBO ret;
483 ret.rel_value__ = GNUNET_htonll (a.rel_value);
488 * Convert relative time from network byte order.
490 * @param a time to convert
491 * @return time in host byte order
493 struct GNUNET_TIME_Relative
494 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
496 struct GNUNET_TIME_Relative ret;
498 ret.rel_value = GNUNET_ntohll (a.rel_value__);
504 * Convert absolute time to network byte order.
506 * @param a time to convert
507 * @return time in network byte order
509 struct GNUNET_TIME_AbsoluteNBO
510 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
512 struct GNUNET_TIME_AbsoluteNBO ret;
514 ret.abs_value__ = GNUNET_htonll (a.abs_value);
519 * Convert absolute time from network byte order.
521 * @param a time to convert
522 * @return time in host byte order
524 struct GNUNET_TIME_Absolute
525 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
527 struct GNUNET_TIME_Absolute ret;
529 ret.abs_value = GNUNET_ntohll (a.abs_value__);
535 * Convert a relative time to a string.
536 * This is one of the very few calls in the entire API that is
539 * @param time the time to print
541 * @return string form of the time (as milliseconds)
544 GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time)
546 static char time_string[21];
548 memset (time_string, 0, sizeof (time_string));
550 sprintf (time_string, "%llu", (unsigned long long) time.rel_value);
551 return (const char *) time_string;