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 * Saturating multiply relative time by a given factor.
449 * @param rel some duration
450 * @param factor integer to multiply with
451 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
453 struct GNUNET_TIME_Relative
454 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
455 unsigned long long factor)
457 struct GNUNET_TIME_Relative ret;
460 return GNUNET_TIME_UNIT_ZERO;
461 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
462 return GNUNET_TIME_UNIT_FOREVER_REL;
463 ret.rel_value_us = rel.rel_value_us * factor;
464 if (ret.rel_value_us / factor != rel.rel_value_us)
466 return GNUNET_TIME_UNIT_FOREVER_REL;
473 * Divide relative time by a given factor.
475 * @param rel some duration
476 * @param factor integer to divide by
477 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
479 struct GNUNET_TIME_Relative
480 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
481 unsigned long long factor)
483 struct GNUNET_TIME_Relative ret;
486 (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
487 return GNUNET_TIME_UNIT_FOREVER_REL;
488 ret.rel_value_us = rel.rel_value_us / factor;
494 * Calculate the estimate time of arrival/completion
497 * @param start when did the operation start?
498 * @param finished how much has been done?
499 * @param total how much must be done overall (same unit as for "finished")
500 * @return remaining duration for the operation,
501 * assuming it continues at the same speed
503 struct GNUNET_TIME_Relative
504 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
507 struct GNUNET_TIME_Relative dur;
509 struct GNUNET_TIME_Relative ret;
511 GNUNET_break (finished <= total);
512 if (finished >= total)
513 return GNUNET_TIME_UNIT_ZERO;
515 return GNUNET_TIME_UNIT_FOREVER_REL;
516 dur = GNUNET_TIME_absolute_get_duration (start);
517 exp = ((double) dur.rel_value_us) * ((double) total) / ((double) finished);
518 ret.rel_value_us = ((uint64_t) exp) - dur.rel_value_us;
524 * Add relative times together.
526 * @param a1 first timestamp
527 * @param a2 second timestamp
528 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
530 struct GNUNET_TIME_Relative
531 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
532 struct GNUNET_TIME_Relative a2)
534 struct GNUNET_TIME_Relative ret;
536 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
537 return GNUNET_TIME_UNIT_FOREVER_REL;
538 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
541 return GNUNET_TIME_UNIT_FOREVER_REL;
543 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
549 * Subtract relative timestamp from the other.
551 * @param a1 first timestamp
552 * @param a2 second timestamp
553 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
555 struct GNUNET_TIME_Relative
556 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
557 struct GNUNET_TIME_Relative a2)
559 struct GNUNET_TIME_Relative ret;
561 if (a2.rel_value_us >= a1.rel_value_us)
562 return GNUNET_TIME_UNIT_ZERO;
563 if (a1.rel_value_us == UINT64_MAX)
564 return GNUNET_TIME_UNIT_FOREVER_REL;
565 ret.rel_value_us = a1.rel_value_us - a2.rel_value_us;
571 * Convert relative time to network byte order.
573 * @param a time to convert
574 * @return time in network byte order
576 struct GNUNET_TIME_RelativeNBO
577 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
579 struct GNUNET_TIME_RelativeNBO ret;
581 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
587 * Convert relative time from network byte order.
589 * @param a time to convert
590 * @return time in host byte order
592 struct GNUNET_TIME_Relative
593 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
595 struct GNUNET_TIME_Relative ret;
597 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
603 * Convert absolute time to network byte order.
605 * @param a time to convert
606 * @return time in network byte order
608 struct GNUNET_TIME_AbsoluteNBO
609 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
611 struct GNUNET_TIME_AbsoluteNBO ret;
613 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
619 * Convert absolute time from network byte order.
621 * @param a time to convert
622 * @return time in host byte order
624 struct GNUNET_TIME_Absolute
625 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
627 struct GNUNET_TIME_Absolute ret;
629 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
636 * Return the current year (i.e. '2011').
639 GNUNET_TIME_get_current_year ()
648 return t->tm_year + 1900;
653 * Convert an expiration time to the respective year (rounds)
655 * @param at absolute time
656 * @return year a year (after 1970), 0 on error
659 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
664 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
668 return t->tm_year + 1900;
674 * Convert a year to an expiration time of January 1st of that year.
676 * @param year a year (after 1970, please ;-)).
677 * @return absolute time for January 1st of that year.
679 struct GNUNET_TIME_Absolute
680 GNUNET_TIME_year_to_time (unsigned int year)
682 struct GNUNET_TIME_Absolute ret;
686 memset (&t, 0, sizeof (t));
690 return GNUNET_TIME_absolute_get (); /* now */
692 t.tm_year = year - 1900;
698 GNUNET_break (tp != (time_t) - 1);
699 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */