2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @author Christian Grothoff
22 * @brief functions for handling time and time arithmetic
25 #include "gnunet_crypto_lib.h"
26 #include "gnunet_time_lib.h"
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__)
31 * Variable used to simulate clock skew. Used for testing, never in production.
33 static long long timestamp_offset;
36 * Set the timestamp offset for this instance.
38 * @param offset the offset to skew the locale time by
41 GNUNET_TIME_set_offset (long long offset)
43 timestamp_offset = offset;
48 * Get the timestamp offset for this instance.
50 * @return the offset we currently skew the locale time by
53 GNUNET_TIME_get_offset ()
55 return timestamp_offset;
60 * Round a time value so that it is suitable for transmission
63 * @param at time to round
64 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
65 * it was just now rounded
68 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at)
70 if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
72 if (0 == at->abs_value_us % 1000000)
74 at->abs_value_us -= at->abs_value_us % 1000000;
80 * Round a time value so that it is suitable for transmission
83 * @param rt time to round
84 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
85 * it was just now rounded
88 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt)
90 if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
92 if (0 == rt->rel_value_us % 1000000)
94 rt->rel_value_us -= rt->rel_value_us % 1000000;
100 * Get the current time (works just as "time", just that we use the
101 * unit of time that the cron-jobs use (and is 64 bit)).
103 * @return the current time
105 struct GNUNET_TIME_Absolute
106 GNUNET_TIME_absolute_get ()
108 struct GNUNET_TIME_Absolute ret;
111 GETTIMEOFDAY (&tv, NULL);
113 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) +
114 ((uint64_t) tv.tv_usec)) + timestamp_offset;
120 * Return relative time of 0ms.
122 struct GNUNET_TIME_Relative
123 GNUNET_TIME_relative_get_zero_ ()
125 static struct GNUNET_TIME_Relative zero;
132 * Return absolute time of 0ms.
134 struct GNUNET_TIME_Absolute
135 GNUNET_TIME_absolute_get_zero_ ()
137 static struct GNUNET_TIME_Absolute zero;
144 * Return relative time of 1us.
146 struct GNUNET_TIME_Relative
147 GNUNET_TIME_relative_get_unit_ ()
149 static struct GNUNET_TIME_Relative one = { 1 };
156 * Return relative time of 1ms.
158 struct GNUNET_TIME_Relative
159 GNUNET_TIME_relative_get_millisecond_ ()
161 static struct GNUNET_TIME_Relative one = { 1000 };
168 * Return relative time of 1s.
170 struct GNUNET_TIME_Relative
171 GNUNET_TIME_relative_get_second_ ()
173 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL };
180 * Return relative time of 1 minute.
182 struct GNUNET_TIME_Relative
183 GNUNET_TIME_relative_get_minute_ ()
185 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL };
192 * Return relative time of 1 hour.
194 struct GNUNET_TIME_Relative
195 GNUNET_TIME_relative_get_hour_ ()
197 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL };
206 struct GNUNET_TIME_Relative
207 GNUNET_TIME_relative_get_forever_ ()
209 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
218 struct GNUNET_TIME_Absolute
219 GNUNET_TIME_absolute_get_forever_ ()
221 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
227 * Convert relative time to an absolute time in the
230 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
232 struct GNUNET_TIME_Absolute
233 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
235 struct GNUNET_TIME_Absolute ret;
237 if (rel.rel_value_us == UINT64_MAX)
238 return GNUNET_TIME_UNIT_FOREVER_ABS;
239 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
241 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
243 GNUNET_break (0); /* overflow... */
244 return GNUNET_TIME_UNIT_FOREVER_ABS;
246 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
252 * Return the minimum of two relative time values.
254 * @param t1 first timestamp
255 * @param t2 other timestamp
256 * @return timestamp that is smaller
258 struct GNUNET_TIME_Relative
259 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
260 struct GNUNET_TIME_Relative t2)
262 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2;
267 * Return the maximum of two relative time values.
269 * @param t1 first timestamp
270 * @param t2 other timestamp
271 * @return timestamp that is larger
273 struct GNUNET_TIME_Relative
274 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
275 struct GNUNET_TIME_Relative t2)
277 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2;
283 * Return the minimum of two relative time values.
285 * @param t1 first timestamp
286 * @param t2 other timestamp
287 * @return timestamp that is smaller
289 struct GNUNET_TIME_Absolute
290 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
291 struct GNUNET_TIME_Absolute t2)
293 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2;
298 * Return the maximum of two relative time values.
300 * @param t1 first timestamp
301 * @param t2 other timestamp
302 * @return timestamp that is bigger
304 struct GNUNET_TIME_Absolute
305 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
306 struct GNUNET_TIME_Absolute t2)
308 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2;
313 * Given a timestamp in the future, how much time
314 * remains until then?
316 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
318 struct GNUNET_TIME_Relative
319 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
321 struct GNUNET_TIME_Relative ret;
323 if (future.abs_value_us == UINT64_MAX)
324 return GNUNET_TIME_UNIT_FOREVER_REL;
325 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
327 if (now.abs_value_us > future.abs_value_us)
328 return GNUNET_TIME_UNIT_ZERO;
329 ret.rel_value_us = future.abs_value_us - now.abs_value_us;
334 * Compute the time difference between the given start and end times.
335 * Use this function instead of actual subtraction to ensure that
336 * "FOREVER" and overflows are handled correctly.
338 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
340 struct GNUNET_TIME_Relative
341 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
342 struct GNUNET_TIME_Absolute end)
344 struct GNUNET_TIME_Relative ret;
346 if (end.abs_value_us == UINT64_MAX)
347 return GNUNET_TIME_UNIT_FOREVER_REL;
348 if (end.abs_value_us < start.abs_value_us)
349 return GNUNET_TIME_UNIT_ZERO;
350 ret.rel_value_us = end.abs_value_us - start.abs_value_us;
355 * Get the duration of an operation as the
356 * difference of the current time and the given start time "whence".
358 * @return 0 if whence > now, otherwise now-whence.
360 struct GNUNET_TIME_Relative
361 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
363 struct GNUNET_TIME_Absolute now;
364 struct GNUNET_TIME_Relative ret;
366 now = GNUNET_TIME_absolute_get ();
367 if (whence.abs_value_us > now.abs_value_us)
368 return GNUNET_TIME_UNIT_ZERO;
369 ret.rel_value_us = now.abs_value_us - whence.abs_value_us;
375 * Add a given relative duration to the
378 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
380 struct GNUNET_TIME_Absolute
381 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
382 struct GNUNET_TIME_Relative duration)
384 struct GNUNET_TIME_Absolute ret;
386 if ((start.abs_value_us == UINT64_MAX) || (duration.rel_value_us == UINT64_MAX))
387 return GNUNET_TIME_UNIT_FOREVER_ABS;
388 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
391 return GNUNET_TIME_UNIT_FOREVER_ABS;
393 ret.abs_value_us = start.abs_value_us + duration.rel_value_us;
399 * Subtract a given relative duration from the
402 * @param start some absolute time
403 * @param duration some relative time to subtract
404 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
406 struct GNUNET_TIME_Absolute
407 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
408 struct GNUNET_TIME_Relative duration)
410 struct GNUNET_TIME_Absolute ret;
412 if (start.abs_value_us <= duration.rel_value_us)
413 return GNUNET_TIME_UNIT_ZERO_ABS;
414 if (start.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
415 return GNUNET_TIME_UNIT_FOREVER_ABS;
416 ret.abs_value_us = start.abs_value_us - duration.rel_value_us;
422 * Multiply relative time by a given factor.
424 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
426 struct GNUNET_TIME_Relative
427 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
428 unsigned long long factor)
430 struct GNUNET_TIME_Relative ret;
433 return GNUNET_TIME_UNIT_ZERO;
434 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
435 return GNUNET_TIME_UNIT_FOREVER_REL;
436 ret.rel_value_us = rel.rel_value_us * factor;
437 if (ret.rel_value_us / factor != rel.rel_value_us)
440 return GNUNET_TIME_UNIT_FOREVER_REL;
447 * Multiply relative time by a given floating-point factor. The factor must be
450 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
452 struct GNUNET_TIME_Relative
453 relative_multiply_double (struct GNUNET_TIME_Relative rel,
456 struct GNUNET_TIME_Relative out;
459 GNUNET_assert (0 <= factor);
462 return GNUNET_TIME_UNIT_ZERO;
463 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
464 return GNUNET_TIME_UNIT_FOREVER_REL;
466 m = ((double) rel.rel_value_us) * factor;
468 if (m >= (double) (GNUNET_TIME_UNIT_FOREVER_REL).rel_value_us)
471 return GNUNET_TIME_UNIT_FOREVER_REL;
474 out.rel_value_us = (uint64_t) m;
480 * Saturating multiply relative time by a given factor.
482 * @param rel some duration
483 * @param factor integer to multiply with
484 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
486 struct GNUNET_TIME_Relative
487 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
488 unsigned long long factor)
490 struct GNUNET_TIME_Relative ret;
493 return GNUNET_TIME_UNIT_ZERO;
494 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
495 return GNUNET_TIME_UNIT_FOREVER_REL;
496 ret.rel_value_us = rel.rel_value_us * factor;
497 if (ret.rel_value_us / factor != rel.rel_value_us)
499 return GNUNET_TIME_UNIT_FOREVER_REL;
506 * Divide relative time by a given factor.
508 * @param rel some duration
509 * @param factor integer to divide by
510 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
512 struct GNUNET_TIME_Relative
513 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
514 unsigned long long factor)
516 struct GNUNET_TIME_Relative ret;
519 (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
520 return GNUNET_TIME_UNIT_FOREVER_REL;
521 ret.rel_value_us = rel.rel_value_us / factor;
527 * Calculate the estimate time of arrival/completion
530 * @param start when did the operation start?
531 * @param finished how much has been done?
532 * @param total how much must be done overall (same unit as for "finished")
533 * @return remaining duration for the operation,
534 * assuming it continues at the same speed
536 struct GNUNET_TIME_Relative
537 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
540 struct GNUNET_TIME_Relative dur;
542 struct GNUNET_TIME_Relative ret;
544 GNUNET_break (finished <= total);
545 if (finished >= total)
546 return GNUNET_TIME_UNIT_ZERO;
548 return GNUNET_TIME_UNIT_FOREVER_REL;
549 dur = GNUNET_TIME_absolute_get_duration (start);
550 exp = ((double) dur.rel_value_us) * ((double) total) / ((double) finished);
551 ret.rel_value_us = ((uint64_t) exp) - dur.rel_value_us;
557 * Add relative times together.
559 * @param a1 first timestamp
560 * @param a2 second timestamp
561 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
563 struct GNUNET_TIME_Relative
564 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
565 struct GNUNET_TIME_Relative a2)
567 struct GNUNET_TIME_Relative ret;
569 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
570 return GNUNET_TIME_UNIT_FOREVER_REL;
571 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
574 return GNUNET_TIME_UNIT_FOREVER_REL;
576 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
582 * Subtract relative timestamp from the other.
584 * @param a1 first timestamp
585 * @param a2 second timestamp
586 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
588 struct GNUNET_TIME_Relative
589 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
590 struct GNUNET_TIME_Relative a2)
592 struct GNUNET_TIME_Relative ret;
594 if (a2.rel_value_us >= a1.rel_value_us)
595 return GNUNET_TIME_UNIT_ZERO;
596 if (a1.rel_value_us == UINT64_MAX)
597 return GNUNET_TIME_UNIT_FOREVER_REL;
598 ret.rel_value_us = a1.rel_value_us - a2.rel_value_us;
604 * Convert relative time to network byte order.
606 * @param a time to convert
607 * @return time in network byte order
609 struct GNUNET_TIME_RelativeNBO
610 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
612 struct GNUNET_TIME_RelativeNBO ret;
614 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
620 * Convert relative time from network byte order.
622 * @param a time to convert
623 * @return time in host byte order
625 struct GNUNET_TIME_Relative
626 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
628 struct GNUNET_TIME_Relative ret;
630 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
636 * Convert absolute time to network byte order.
638 * @param a time to convert
639 * @return time in network byte order
641 struct GNUNET_TIME_AbsoluteNBO
642 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
644 struct GNUNET_TIME_AbsoluteNBO ret;
646 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
652 * Convert absolute time from network byte order.
654 * @param a time to convert
655 * @return time in host byte order
657 struct GNUNET_TIME_Absolute
658 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
660 struct GNUNET_TIME_Absolute ret;
662 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
669 * Return the current year (i.e. '2011').
672 GNUNET_TIME_get_current_year ()
681 return t->tm_year + 1900;
686 * Convert an expiration time to the respective year (rounds)
688 * @param at absolute time
689 * @return year a year (after 1970), 0 on error
692 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
697 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
701 return t->tm_year + 1900;
707 * Convert a year to an expiration time of January 1st of that year.
709 * @param year a year (after 1970, please ;-)).
710 * @return absolute time for January 1st of that year.
712 struct GNUNET_TIME_Absolute
713 GNUNET_TIME_year_to_time (unsigned int year)
715 struct GNUNET_TIME_Absolute ret;
719 memset (&t, 0, sizeof (t));
723 return GNUNET_TIME_absolute_get (); /* now */
725 t.tm_year = year - 1900;
731 GNUNET_break (tp != (time_t) - 1);
732 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */
738 * Randomized exponential back-off, starting at 1 ms
739 * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
740 * to a maximum of the given threshold.
742 * @param r current backoff time, initially zero
743 * @param threshold maximum value for backoff
744 * @return the next backoff time
746 struct GNUNET_TIME_Relative
747 GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold)
749 double r = (rand() % 500) / 1000.0;
750 struct GNUNET_TIME_Relative t;
752 t = relative_multiply_double (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
755 return GNUNET_TIME_relative_min (threshold,