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 static long long timestamp_offset;
32 * Set the timestamp offset for this instance.
34 * @param offset the offset to skew the locale time by
37 GNUNET_TIME_set_offset (long long offset)
39 timestamp_offset = offset;
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)).
46 * @return the current time
48 struct GNUNET_TIME_Absolute
49 GNUNET_TIME_absolute_get ()
51 struct GNUNET_TIME_Absolute ret;
54 GETTIMEOFDAY (&tv, NULL);
56 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) +
57 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset;
63 * Return relative time of 0ms.
65 struct GNUNET_TIME_Relative
66 GNUNET_TIME_relative_get_zero ()
68 static struct GNUNET_TIME_Relative zero;
75 * Return absolute time of 0ms.
77 struct GNUNET_TIME_Absolute
78 GNUNET_TIME_absolute_get_zero ()
80 static struct GNUNET_TIME_Absolute zero;
86 * Return relative time of 1ms.
88 struct GNUNET_TIME_Relative
89 GNUNET_TIME_relative_get_unit ()
91 static struct GNUNET_TIME_Relative one = { 1 };
98 struct GNUNET_TIME_Relative
99 GNUNET_TIME_relative_get_forever ()
101 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
108 struct GNUNET_TIME_Absolute
109 GNUNET_TIME_absolute_get_forever ()
111 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
116 * Convert relative time to an absolute time in the
119 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
121 struct GNUNET_TIME_Absolute
122 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
124 struct GNUNET_TIME_Absolute ret;
126 if (rel.rel_value == UINT64_MAX)
127 return GNUNET_TIME_absolute_get_forever ();
128 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
130 if (rel.rel_value + now.abs_value < rel.rel_value)
132 GNUNET_break (0); /* overflow... */
133 return GNUNET_TIME_absolute_get_forever ();
135 ret.abs_value = rel.rel_value + now.abs_value;
141 * Return the minimum of two relative time values.
143 * @param t1 first timestamp
144 * @param t2 other timestamp
145 * @return timestamp that is smaller
147 struct GNUNET_TIME_Relative
148 GNUNET_TIME_relative_min (struct
150 t1, struct GNUNET_TIME_Relative t2)
152 return (t1.rel_value < t2.rel_value) ? t1 : t2;
157 * Return the maximum of two relative time values.
159 * @param t1 first timestamp
160 * @param t2 other timestamp
161 * @return timestamp that is larger
163 struct GNUNET_TIME_Relative
164 GNUNET_TIME_relative_max (struct
166 t1, struct GNUNET_TIME_Relative t2)
168 return (t1.rel_value > t2.rel_value) ? t1 : t2;
174 * Return the minimum of two relative time values.
176 * @param t1 first timestamp
177 * @param t2 other timestamp
178 * @return timestamp that is smaller
180 struct GNUNET_TIME_Absolute
181 GNUNET_TIME_absolute_min (struct
183 t1, struct GNUNET_TIME_Absolute t2)
185 return (t1.abs_value < t2.abs_value) ? t1 : t2;
190 * Return the maximum of two relative time values.
192 * @param t1 first timestamp
193 * @param t2 other timestamp
194 * @return timestamp that is smaller
196 struct GNUNET_TIME_Absolute
197 GNUNET_TIME_absolute_max (struct
199 t1, struct GNUNET_TIME_Absolute t2)
201 return (t1.abs_value > t2.abs_value) ? t1 : t2;
206 * Given a timestamp in the future, how much time
207 * remains until then?
209 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
211 struct GNUNET_TIME_Relative
212 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
214 struct GNUNET_TIME_Relative ret;
216 if (future.abs_value == UINT64_MAX)
217 return GNUNET_TIME_relative_get_forever ();
218 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
220 if (now.abs_value > future.abs_value)
221 return GNUNET_TIME_relative_get_zero ();
222 ret.rel_value = future.abs_value - now.abs_value;
227 * Compute the time difference between the given start and end times.
228 * Use this function instead of actual subtraction to ensure that
229 * "FOREVER" and overflows are handled correctly.
231 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
233 struct GNUNET_TIME_Relative
234 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
235 struct GNUNET_TIME_Absolute end)
237 struct GNUNET_TIME_Relative ret;
239 if (end.abs_value == UINT64_MAX)
240 return GNUNET_TIME_relative_get_forever ();
241 if (end.abs_value < start.abs_value)
242 return GNUNET_TIME_relative_get_zero ();
243 ret.rel_value = end.abs_value - start.abs_value;
248 * Get the duration of an operation as the
249 * difference of the current time and the given start time "whence".
251 * @return aborts if whence==FOREVER, 0 if whence > now, otherwise now-whence.
253 struct GNUNET_TIME_Relative
254 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
256 struct GNUNET_TIME_Absolute now;
257 struct GNUNET_TIME_Relative ret;
259 now = GNUNET_TIME_absolute_get ();
260 GNUNET_assert (whence.abs_value != UINT64_MAX);
261 if (whence.abs_value > now.abs_value)
262 return GNUNET_TIME_relative_get_zero ();
263 ret.rel_value = now.abs_value - whence.abs_value;
269 * Add a given relative duration to the
272 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
274 struct GNUNET_TIME_Absolute
275 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
276 struct GNUNET_TIME_Relative duration)
278 struct GNUNET_TIME_Absolute ret;
280 if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX))
281 return GNUNET_TIME_absolute_get_forever ();
282 if (start.abs_value + duration.rel_value < start.abs_value)
285 return GNUNET_TIME_absolute_get_forever ();
287 ret.abs_value = start.abs_value + duration.rel_value;
293 * Subtract a given relative duration from the
296 * @param start some absolute time
297 * @param duration some relative time to subtract
298 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
300 struct GNUNET_TIME_Absolute
301 GNUNET_TIME_absolute_subtract (struct
303 start, struct GNUNET_TIME_Relative duration)
305 struct GNUNET_TIME_Absolute ret;
307 if (start.abs_value <= duration.rel_value)
308 return GNUNET_TIME_UNIT_ZERO_ABS;
309 if (start.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
310 return GNUNET_TIME_UNIT_FOREVER_ABS;
311 ret.abs_value = start.abs_value - duration.rel_value;
317 * Multiply relative time by a given factor.
319 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
321 struct GNUNET_TIME_Relative
322 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
325 struct GNUNET_TIME_Relative ret;
328 return GNUNET_TIME_relative_get_zero ();
329 ret.rel_value = rel.rel_value * (unsigned long long) factor;
330 if (ret.rel_value / factor != rel.rel_value)
333 return GNUNET_TIME_relative_get_forever ();
340 * Divide relative time by a given factor.
342 * @param rel some duration
343 * @param factor integer to divide by
344 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
346 struct GNUNET_TIME_Relative
347 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
350 struct GNUNET_TIME_Relative ret;
353 (rel.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value))
354 return GNUNET_TIME_UNIT_FOREVER_REL;
355 ret.rel_value = rel.rel_value / (unsigned long long) factor;
361 * Calculate the estimate time of arrival/completion
364 * @param start when did the operation start?
365 * @param finished how much has been done?
366 * @param total how much must be done overall (same unit as for "finished")
367 * @return remaining duration for the operation,
368 * assuming it continues at the same speed
370 struct GNUNET_TIME_Relative
371 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
372 uint64_t finished, uint64_t total)
374 struct GNUNET_TIME_Relative dur;
376 struct GNUNET_TIME_Relative ret;
378 GNUNET_break (finished <= total);
379 if (finished >= total)
380 return GNUNET_TIME_UNIT_ZERO;
382 return GNUNET_TIME_UNIT_FOREVER_REL;
383 dur = GNUNET_TIME_absolute_get_duration (start);
384 exp = ((double) dur.rel_value) * ((double) total) / ((double) finished);
385 ret.rel_value = ((uint64_t) exp) - dur.rel_value;
391 * Add relative times together.
393 * @param a1 first timestamp
394 * @param a2 second timestamp
395 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
397 struct GNUNET_TIME_Relative
398 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
399 struct GNUNET_TIME_Relative a2)
401 struct GNUNET_TIME_Relative ret;
403 if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX))
404 return GNUNET_TIME_relative_get_forever ();
405 if (a1.rel_value + a2.rel_value < a1.rel_value)
408 return GNUNET_TIME_relative_get_forever ();
410 ret.rel_value = a1.rel_value + a2.rel_value;
416 * Subtract relative timestamp from the other.
418 * @param a1 first timestamp
419 * @param a2 second timestamp
420 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
422 struct GNUNET_TIME_Relative
423 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
424 struct GNUNET_TIME_Relative a2)
426 struct GNUNET_TIME_Relative ret;
428 if (a2.rel_value >= a1.rel_value)
429 return GNUNET_TIME_relative_get_zero ();
430 if (a1.rel_value == UINT64_MAX)
431 return GNUNET_TIME_relative_get_forever ();
432 ret.rel_value = a1.rel_value - a2.rel_value;
438 * Convert relative time to network byte order.
440 * @param a time to convert
441 * @return time in network byte order
443 struct GNUNET_TIME_RelativeNBO
444 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
446 struct GNUNET_TIME_RelativeNBO ret;
448 ret.rel_value__ = GNUNET_htonll (a.rel_value);
453 * Convert relative time from network byte order.
455 * @param a time to convert
456 * @return time in host byte order
458 struct GNUNET_TIME_Relative
459 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
461 struct GNUNET_TIME_Relative ret;
463 ret.rel_value = GNUNET_ntohll (a.rel_value__);
469 * Convert absolute time to network byte order.
471 * @param a time to convert
472 * @return time in network byte order
474 struct GNUNET_TIME_AbsoluteNBO
475 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
477 struct GNUNET_TIME_AbsoluteNBO ret;
479 ret.abs_value__ = GNUNET_htonll (a.abs_value);
484 * Convert absolute time from network byte order.
486 * @param a time to convert
487 * @return time in host byte order
489 struct GNUNET_TIME_Absolute
490 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
492 struct GNUNET_TIME_Absolute ret;
494 ret.abs_value = GNUNET_ntohll (a.abs_value__);
500 * Convert a relative time to a string.
501 * This is one of the very few calls in the entire API that is
504 * @param time the time to print
506 * @return string form of the time (as milliseconds)
509 GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time)
511 static char time_string[21];
513 memset (time_string, 0, sizeof (time_string));
515 sprintf (time_string, "%llu", (unsigned long long) time.rel_value);
516 return (const char *) time_string;