2 This file is part of GNUnet.
3 Copyright (C) 2001-2013, 2018 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_util_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__)
30 * Variable used to simulate clock skew. Used for testing, never in production.
32 static long long timestamp_offset;
35 * Set the timestamp offset for this instance.
37 * @param offset the offset to skew the locale time by
40 GNUNET_TIME_set_offset (long long offset)
42 timestamp_offset = offset;
47 * Get the timestamp offset for this instance.
49 * @return the offset we currently skew the locale time by
52 GNUNET_TIME_get_offset ()
54 return timestamp_offset;
59 * Round a time value so that it is suitable for transmission
62 * @param at time to round
63 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
64 * it was just now rounded
67 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at)
69 if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
71 if (0 == at->abs_value_us % 1000000)
73 at->abs_value_us -= at->abs_value_us % 1000000;
79 * Round a time value so that it is suitable for transmission
82 * @param rt time to round
83 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
84 * it was just now rounded
87 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt)
89 if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
91 if (0 == rt->rel_value_us % 1000000)
93 rt->rel_value_us -= rt->rel_value_us % 1000000;
99 * Get the current time (works just as "time", just that we use the
100 * unit of time that the cron-jobs use (and is 64 bit)).
102 * @return the current time
104 struct GNUNET_TIME_Absolute
105 GNUNET_TIME_absolute_get ()
107 struct GNUNET_TIME_Absolute ret;
110 GETTIMEOFDAY (&tv, NULL);
112 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) +
113 ((uint64_t) tv.tv_usec)) + timestamp_offset;
119 * Return relative time of 0ms.
121 struct GNUNET_TIME_Relative
122 GNUNET_TIME_relative_get_zero_ ()
124 static struct GNUNET_TIME_Relative zero;
131 * Return absolute time of 0ms.
133 struct GNUNET_TIME_Absolute
134 GNUNET_TIME_absolute_get_zero_ ()
136 static struct GNUNET_TIME_Absolute zero;
143 * Return relative time of 1us.
145 struct GNUNET_TIME_Relative
146 GNUNET_TIME_relative_get_unit_ ()
148 static struct GNUNET_TIME_Relative one = { 1 };
155 * Return relative time of 1ms.
157 struct GNUNET_TIME_Relative
158 GNUNET_TIME_relative_get_millisecond_ ()
160 static struct GNUNET_TIME_Relative one = { 1000 };
167 * Return relative time of 1s.
169 struct GNUNET_TIME_Relative
170 GNUNET_TIME_relative_get_second_ ()
172 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL };
179 * Return relative time of 1 minute.
181 struct GNUNET_TIME_Relative
182 GNUNET_TIME_relative_get_minute_ ()
184 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL };
191 * Return relative time of 1 hour.
193 struct GNUNET_TIME_Relative
194 GNUNET_TIME_relative_get_hour_ ()
196 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL };
205 struct GNUNET_TIME_Relative
206 GNUNET_TIME_relative_get_forever_ ()
208 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
217 struct GNUNET_TIME_Absolute
218 GNUNET_TIME_absolute_get_forever_ ()
220 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
226 * Convert relative time to an absolute time in the
229 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
231 struct GNUNET_TIME_Absolute
232 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
234 struct GNUNET_TIME_Absolute ret;
236 if (rel.rel_value_us == UINT64_MAX)
237 return GNUNET_TIME_UNIT_FOREVER_ABS;
238 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
240 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
242 GNUNET_break (0); /* overflow... */
243 return GNUNET_TIME_UNIT_FOREVER_ABS;
245 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
251 * Return the minimum of two relative time values.
253 * @param t1 first timestamp
254 * @param t2 other timestamp
255 * @return timestamp that is smaller
257 struct GNUNET_TIME_Relative
258 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
259 struct GNUNET_TIME_Relative t2)
261 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2;
266 * Return the maximum of two relative time values.
268 * @param t1 first timestamp
269 * @param t2 other timestamp
270 * @return timestamp that is larger
272 struct GNUNET_TIME_Relative
273 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
274 struct GNUNET_TIME_Relative t2)
276 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2;
282 * Return the minimum of two relative time values.
284 * @param t1 first timestamp
285 * @param t2 other timestamp
286 * @return timestamp that is smaller
288 struct GNUNET_TIME_Absolute
289 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
290 struct GNUNET_TIME_Absolute t2)
292 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2;
297 * Return the maximum of two relative time values.
299 * @param t1 first timestamp
300 * @param t2 other timestamp
301 * @return timestamp that is bigger
303 struct GNUNET_TIME_Absolute
304 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
305 struct GNUNET_TIME_Absolute t2)
307 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2;
312 * Given a timestamp in the future, how much time
313 * remains until then?
315 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
317 struct GNUNET_TIME_Relative
318 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
320 struct GNUNET_TIME_Relative ret;
322 if (future.abs_value_us == UINT64_MAX)
323 return GNUNET_TIME_UNIT_FOREVER_REL;
324 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
326 if (now.abs_value_us > future.abs_value_us)
327 return GNUNET_TIME_UNIT_ZERO;
328 ret.rel_value_us = future.abs_value_us - now.abs_value_us;
333 * Compute the time difference between the given start and end times.
334 * Use this function instead of actual subtraction to ensure that
335 * "FOREVER" and overflows are handled correctly.
337 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
339 struct GNUNET_TIME_Relative
340 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
341 struct GNUNET_TIME_Absolute end)
343 struct GNUNET_TIME_Relative ret;
345 if (end.abs_value_us == UINT64_MAX)
346 return GNUNET_TIME_UNIT_FOREVER_REL;
347 if (end.abs_value_us < start.abs_value_us)
348 return GNUNET_TIME_UNIT_ZERO;
349 ret.rel_value_us = end.abs_value_us - start.abs_value_us;
354 * Get the duration of an operation as the
355 * difference of the current time and the given start time "whence".
357 * @return 0 if whence > now, otherwise now-whence.
359 struct GNUNET_TIME_Relative
360 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
362 struct GNUNET_TIME_Absolute now;
363 struct GNUNET_TIME_Relative ret;
365 now = GNUNET_TIME_absolute_get ();
366 if (whence.abs_value_us > now.abs_value_us)
367 return GNUNET_TIME_UNIT_ZERO;
368 ret.rel_value_us = now.abs_value_us - whence.abs_value_us;
374 * Add a given relative duration to the
377 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
379 struct GNUNET_TIME_Absolute
380 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
381 struct GNUNET_TIME_Relative duration)
383 struct GNUNET_TIME_Absolute ret;
385 if ((start.abs_value_us == UINT64_MAX) || (duration.rel_value_us == UINT64_MAX))
386 return GNUNET_TIME_UNIT_FOREVER_ABS;
387 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
390 return GNUNET_TIME_UNIT_FOREVER_ABS;
392 ret.abs_value_us = start.abs_value_us + duration.rel_value_us;
398 * Subtract a given relative duration from the
401 * @param start some absolute time
402 * @param duration some relative time to subtract
403 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
405 struct GNUNET_TIME_Absolute
406 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
407 struct GNUNET_TIME_Relative duration)
409 struct GNUNET_TIME_Absolute ret;
411 if (start.abs_value_us <= duration.rel_value_us)
412 return GNUNET_TIME_UNIT_ZERO_ABS;
413 if (start.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
414 return GNUNET_TIME_UNIT_FOREVER_ABS;
415 ret.abs_value_us = start.abs_value_us - duration.rel_value_us;
421 * Multiply relative time by a given factor.
423 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
425 struct GNUNET_TIME_Relative
426 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
427 unsigned long long factor)
429 struct GNUNET_TIME_Relative ret;
432 return GNUNET_TIME_UNIT_ZERO;
433 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
434 return GNUNET_TIME_UNIT_FOREVER_REL;
435 ret.rel_value_us = rel.rel_value_us * factor;
436 if (ret.rel_value_us / factor != rel.rel_value_us)
439 return GNUNET_TIME_UNIT_FOREVER_REL;
446 * Multiply relative time by a given floating-point factor. The factor must be
449 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
451 struct GNUNET_TIME_Relative
452 relative_multiply_double (struct GNUNET_TIME_Relative rel,
455 struct GNUNET_TIME_Relative out;
458 GNUNET_assert (0 <= factor);
461 return GNUNET_TIME_UNIT_ZERO;
462 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
463 return GNUNET_TIME_UNIT_FOREVER_REL;
465 m = ((double) rel.rel_value_us) * factor;
467 if (m >= (double) (GNUNET_TIME_UNIT_FOREVER_REL).rel_value_us)
470 return GNUNET_TIME_UNIT_FOREVER_REL;
473 out.rel_value_us = (uint64_t) m;
479 * Saturating multiply relative time by a given factor.
481 * @param rel some duration
482 * @param factor integer to multiply with
483 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
485 struct GNUNET_TIME_Relative
486 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
487 unsigned long long factor)
489 struct GNUNET_TIME_Relative ret;
492 return GNUNET_TIME_UNIT_ZERO;
493 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
494 return GNUNET_TIME_UNIT_FOREVER_REL;
495 ret.rel_value_us = rel.rel_value_us * factor;
496 if (ret.rel_value_us / factor != rel.rel_value_us)
498 return GNUNET_TIME_UNIT_FOREVER_REL;
505 * Divide relative time by a given factor.
507 * @param rel some duration
508 * @param factor integer to divide by
509 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
511 struct GNUNET_TIME_Relative
512 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
513 unsigned long long factor)
515 struct GNUNET_TIME_Relative ret;
518 (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
519 return GNUNET_TIME_UNIT_FOREVER_REL;
520 ret.rel_value_us = rel.rel_value_us / factor;
526 * Calculate the estimate time of arrival/completion
529 * @param start when did the operation start?
530 * @param finished how much has been done?
531 * @param total how much must be done overall (same unit as for "finished")
532 * @return remaining duration for the operation,
533 * assuming it continues at the same speed
535 struct GNUNET_TIME_Relative
536 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
539 struct GNUNET_TIME_Relative dur;
541 struct GNUNET_TIME_Relative ret;
543 GNUNET_break (finished <= total);
544 if (finished >= total)
545 return GNUNET_TIME_UNIT_ZERO;
547 return GNUNET_TIME_UNIT_FOREVER_REL;
548 dur = GNUNET_TIME_absolute_get_duration (start);
549 exp = ((double) dur.rel_value_us) * ((double) total) / ((double) finished);
550 ret.rel_value_us = ((uint64_t) exp) - dur.rel_value_us;
556 * Add relative times together.
558 * @param a1 first timestamp
559 * @param a2 second timestamp
560 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
562 struct GNUNET_TIME_Relative
563 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
564 struct GNUNET_TIME_Relative a2)
566 struct GNUNET_TIME_Relative ret;
568 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
569 return GNUNET_TIME_UNIT_FOREVER_REL;
570 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
573 return GNUNET_TIME_UNIT_FOREVER_REL;
575 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
581 * Subtract relative timestamp from the other.
583 * @param a1 first timestamp
584 * @param a2 second timestamp
585 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
587 struct GNUNET_TIME_Relative
588 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
589 struct GNUNET_TIME_Relative a2)
591 struct GNUNET_TIME_Relative ret;
593 if (a2.rel_value_us >= a1.rel_value_us)
594 return GNUNET_TIME_UNIT_ZERO;
595 if (a1.rel_value_us == UINT64_MAX)
596 return GNUNET_TIME_UNIT_FOREVER_REL;
597 ret.rel_value_us = a1.rel_value_us - a2.rel_value_us;
603 * Convert relative time to network byte order.
605 * @param a time to convert
606 * @return time in network byte order
608 struct GNUNET_TIME_RelativeNBO
609 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
611 struct GNUNET_TIME_RelativeNBO ret;
613 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
619 * Convert relative time from network byte order.
621 * @param a time to convert
622 * @return time in host byte order
624 struct GNUNET_TIME_Relative
625 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
627 struct GNUNET_TIME_Relative ret;
629 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
635 * Convert absolute time to network byte order.
637 * @param a time to convert
638 * @return time in network byte order
640 struct GNUNET_TIME_AbsoluteNBO
641 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
643 struct GNUNET_TIME_AbsoluteNBO ret;
645 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
651 * Convert absolute time from network byte order.
653 * @param a time to convert
654 * @return time in host byte order
656 struct GNUNET_TIME_Absolute
657 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
659 struct GNUNET_TIME_Absolute ret;
661 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
668 * Return the current year (i.e. '2011').
671 GNUNET_TIME_get_current_year ()
680 return t->tm_year + 1900;
685 * Convert an expiration time to the respective year (rounds)
687 * @param at absolute time
688 * @return year a year (after 1970), 0 on error
691 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
696 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
700 return t->tm_year + 1900;
706 * Convert a year to an expiration time of January 1st of that year.
708 * @param year a year (after 1970, please ;-)).
709 * @return absolute time for January 1st of that year.
711 struct GNUNET_TIME_Absolute
712 GNUNET_TIME_year_to_time (unsigned int year)
714 struct GNUNET_TIME_Absolute ret;
718 memset (&t, 0, sizeof (t));
722 return GNUNET_TIME_absolute_get (); /* now */
724 t.tm_year = year - 1900;
730 GNUNET_break (tp != (time_t) - 1);
731 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */
737 * Randomized exponential back-off, starting at 1 ms
738 * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
739 * to a maximum of the given threshold.
741 * @param r current backoff time, initially zero
742 * @param threshold maximum value for backoff
743 * @return the next backoff time
745 struct GNUNET_TIME_Relative
746 GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold)
748 double r = (rand() % 500) / 1000.0;
749 struct GNUNET_TIME_Relative t;
751 t = relative_multiply_double (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
754 return GNUNET_TIME_relative_min (threshold,
760 * Obtain the current time and make sure it is monotonically
761 * increasing. Guards against systems without an RTC or
762 * clocks running backwards and other nasty surprises. Does
763 * not guarantee that the returned time is near the current
764 * time returned by #GNUNET_TIME_absolute_get(). Two
765 * subsequent calls (within a short time period) may return the
766 * same value. Persists the last returned time on disk to
767 * ensure that time never goes backwards. As a result, the
768 * resulting value can be used to check if a message is the
769 * "most recent" value and replays of older messages (from
770 * the same origin) would be discarded.
772 * @param cfg configuration, used to determine where to
773 * store the time; user can also insist RTC is working
774 * nicely and disable the feature
775 * @return monotonically increasing time
777 struct GNUNET_TIME_Absolute
778 GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cfg)
780 static const struct GNUNET_CONFIGURATION_Handle *last_cfg;
781 static struct GNUNET_TIME_Absolute last_time;
782 static struct GNUNET_DISK_MapHandle *map_handle;
783 static struct GNUNET_TIME_AbsoluteNBO *map;
784 struct GNUNET_TIME_Absolute now;
786 now = GNUNET_TIME_absolute_get ();
791 if (NULL != map_handle)
793 GNUNET_DISK_file_unmap (map_handle);
799 if ( (NULL != cfg) &&
801 GNUNET_CONFIGURATION_get_value_filename (cfg,
803 "MONOTONIC_TIME_FILENAME",
806 struct GNUNET_DISK_FileHandle *fh;
808 fh = GNUNET_DISK_file_open (filename,
809 GNUNET_DISK_OPEN_READWRITE | GNUNET_DISK_OPEN_CREATE,
810 GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_WRITE |
811 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_GROUP_READ);
814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
815 _("Failed to map `%s', cannot assure monotonic time!\n"),
823 GNUNET_break (GNUNET_OK ==
824 GNUNET_DISK_file_handle_size (fh,
826 if (size < sizeof (*map))
828 struct GNUNET_TIME_AbsoluteNBO o;
830 o = GNUNET_TIME_absolute_hton (now);
832 GNUNET_DISK_file_write (fh,
839 if (size == sizeof (*map))
841 map = GNUNET_DISK_file_map (fh,
843 GNUNET_DISK_MAP_TYPE_READWRITE,
846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
847 _("Failed to map `%s', cannot assure monotonic time!\n"),
852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
853 _("Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"),
857 GNUNET_DISK_file_close (fh);
858 GNUNET_free (filename);
862 last_time = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (*map),
864 if (now.abs_value_us <= last_time.abs_value_us)
865 now.abs_value_us = last_time.abs_value_us+1;
868 *map = GNUNET_TIME_absolute_hton (now);
876 void __attribute__ ((destructor))
877 GNUNET_util_time_fini ()
879 (void) GNUNET_TIME_absolute_get_monotonic (NULL);