2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @author Christian Grothoff
24 * @brief functions for handling time and time arithmetic
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_time_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33 * Variable used to simulate clock skew. Used for testing, never in production.
35 static long long timestamp_offset;
38 * Set the timestamp offset for this instance.
40 * @param offset the offset to skew the locale time by
43 GNUNET_TIME_set_offset (long long offset)
45 timestamp_offset = offset;
50 * Get the timestamp offset for this instance.
52 * @return the offset we currently skew the locale time by
55 GNUNET_TIME_get_offset ()
57 return timestamp_offset;
62 * Get the current time (works just as "time", just that we use the
63 * unit of time that the cron-jobs use (and is 64 bit)).
65 * @return the current time
67 struct GNUNET_TIME_Absolute
68 GNUNET_TIME_absolute_get ()
70 struct GNUNET_TIME_Absolute ret;
73 GETTIMEOFDAY (&tv, NULL);
75 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) +
76 ((uint64_t) tv.tv_usec)) + timestamp_offset;
82 * Return relative time of 0ms.
84 struct GNUNET_TIME_Relative
85 GNUNET_TIME_relative_get_zero_ ()
87 static struct GNUNET_TIME_Relative zero;
94 * Return absolute time of 0ms.
96 struct GNUNET_TIME_Absolute
97 GNUNET_TIME_absolute_get_zero_ ()
99 static struct GNUNET_TIME_Absolute zero;
106 * Return relative time of 1us.
108 struct GNUNET_TIME_Relative
109 GNUNET_TIME_relative_get_unit_ ()
111 static struct GNUNET_TIME_Relative one = { 1 };
117 * Return relative time of 1ms.
119 struct GNUNET_TIME_Relative
120 GNUNET_TIME_relative_get_millisecond_ ()
122 static struct GNUNET_TIME_Relative one = { 1000 };
128 * Return relative time of 1s.
130 struct GNUNET_TIME_Relative
131 GNUNET_TIME_relative_get_second_ ()
133 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL };
139 * Return relative time of 1 minute.
141 struct GNUNET_TIME_Relative
142 GNUNET_TIME_relative_get_minute_ ()
144 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL };
150 * Return relative time of 1 hour.
152 struct GNUNET_TIME_Relative
153 GNUNET_TIME_relative_get_hour_ ()
155 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL };
163 struct GNUNET_TIME_Relative
164 GNUNET_TIME_relative_get_forever_ ()
166 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
173 struct GNUNET_TIME_Absolute
174 GNUNET_TIME_absolute_get_forever_ ()
176 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
181 * Convert relative time to an absolute time in the
184 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
186 struct GNUNET_TIME_Absolute
187 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
189 struct GNUNET_TIME_Absolute ret;
191 if (rel.rel_value_us == UINT64_MAX)
192 return GNUNET_TIME_UNIT_FOREVER_ABS;
193 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
195 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
197 GNUNET_break (0); /* overflow... */
198 return GNUNET_TIME_UNIT_FOREVER_ABS;
200 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
206 * Return the minimum of two relative time values.
208 * @param t1 first timestamp
209 * @param t2 other timestamp
210 * @return timestamp that is smaller
212 struct GNUNET_TIME_Relative
213 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
214 struct GNUNET_TIME_Relative t2)
216 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2;
221 * Return the maximum of two relative time values.
223 * @param t1 first timestamp
224 * @param t2 other timestamp
225 * @return timestamp that is larger
227 struct GNUNET_TIME_Relative
228 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
229 struct GNUNET_TIME_Relative t2)
231 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2;
237 * Return the minimum of two relative time values.
239 * @param t1 first timestamp
240 * @param t2 other timestamp
241 * @return timestamp that is smaller
243 struct GNUNET_TIME_Absolute
244 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
245 struct GNUNET_TIME_Absolute t2)
247 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2;
252 * Return the maximum of two relative time values.
254 * @param t1 first timestamp
255 * @param t2 other timestamp
256 * @return timestamp that is bigger
258 struct GNUNET_TIME_Absolute
259 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
260 struct GNUNET_TIME_Absolute t2)
262 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2;
267 * Given a timestamp in the future, how much time
268 * remains until then?
270 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
272 struct GNUNET_TIME_Relative
273 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
275 struct GNUNET_TIME_Relative ret;
277 if (future.abs_value_us == UINT64_MAX)
278 return GNUNET_TIME_UNIT_FOREVER_REL;
279 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
281 if (now.abs_value_us > future.abs_value_us)
282 return GNUNET_TIME_UNIT_ZERO;
283 ret.rel_value_us = future.abs_value_us - now.abs_value_us;
288 * Compute the time difference between the given start and end times.
289 * Use this function instead of actual subtraction to ensure that
290 * "FOREVER" and overflows are handled correctly.
292 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
294 struct GNUNET_TIME_Relative
295 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
296 struct GNUNET_TIME_Absolute end)
298 struct GNUNET_TIME_Relative ret;
300 if (end.abs_value_us == UINT64_MAX)
301 return GNUNET_TIME_UNIT_FOREVER_REL;
302 if (end.abs_value_us < start.abs_value_us)
303 return GNUNET_TIME_UNIT_ZERO;
304 ret.rel_value_us = end.abs_value_us - start.abs_value_us;
309 * Get the duration of an operation as the
310 * difference of the current time and the given start time "whence".
312 * @return 0 if whence > now, otherwise now-whence.
314 struct GNUNET_TIME_Relative
315 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
317 struct GNUNET_TIME_Absolute now;
318 struct GNUNET_TIME_Relative ret;
320 now = GNUNET_TIME_absolute_get ();
321 if (whence.abs_value_us > now.abs_value_us)
322 return GNUNET_TIME_UNIT_ZERO;
323 ret.rel_value_us = now.abs_value_us - whence.abs_value_us;
329 * Add a given relative duration to the
332 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
334 struct GNUNET_TIME_Absolute
335 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
336 struct GNUNET_TIME_Relative duration)
338 struct GNUNET_TIME_Absolute ret;
340 if ((start.abs_value_us == UINT64_MAX) || (duration.rel_value_us == UINT64_MAX))
341 return GNUNET_TIME_UNIT_FOREVER_ABS;
342 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
345 return GNUNET_TIME_UNIT_FOREVER_ABS;
347 ret.abs_value_us = start.abs_value_us + duration.rel_value_us;
353 * Subtract a given relative duration from the
356 * @param start some absolute time
357 * @param duration some relative time to subtract
358 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
360 struct GNUNET_TIME_Absolute
361 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
362 struct GNUNET_TIME_Relative duration)
364 struct GNUNET_TIME_Absolute ret;
366 if (start.abs_value_us <= duration.rel_value_us)
367 return GNUNET_TIME_UNIT_ZERO_ABS;
368 if (start.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
369 return GNUNET_TIME_UNIT_FOREVER_ABS;
370 ret.abs_value_us = start.abs_value_us - duration.rel_value_us;
376 * Multiply relative time by a given factor.
378 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
380 struct GNUNET_TIME_Relative
381 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
384 struct GNUNET_TIME_Relative ret;
387 return GNUNET_TIME_UNIT_ZERO;
388 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
389 return GNUNET_TIME_UNIT_FOREVER_REL;
390 ret.rel_value_us = rel.rel_value_us * (unsigned long long) factor;
391 if (ret.rel_value_us / factor != rel.rel_value_us)
394 return GNUNET_TIME_UNIT_FOREVER_REL;
401 * Divide relative time by a given factor.
403 * @param rel some duration
404 * @param factor integer to divide by
405 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
407 struct GNUNET_TIME_Relative
408 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
411 struct GNUNET_TIME_Relative ret;
414 (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
415 return GNUNET_TIME_UNIT_FOREVER_REL;
416 ret.rel_value_us = rel.rel_value_us / (unsigned long long) factor;
422 * Calculate the estimate time of arrival/completion
425 * @param start when did the operation start?
426 * @param finished how much has been done?
427 * @param total how much must be done overall (same unit as for "finished")
428 * @return remaining duration for the operation,
429 * assuming it continues at the same speed
431 struct GNUNET_TIME_Relative
432 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
435 struct GNUNET_TIME_Relative dur;
437 struct GNUNET_TIME_Relative ret;
439 GNUNET_break (finished <= total);
440 if (finished >= total)
441 return GNUNET_TIME_UNIT_ZERO;
443 return GNUNET_TIME_UNIT_FOREVER_REL;
444 dur = GNUNET_TIME_absolute_get_duration (start);
445 exp = ((double) dur.rel_value_us) * ((double) total) / ((double) finished);
446 ret.rel_value_us = ((uint64_t) exp) - dur.rel_value_us;
452 * Add relative times together.
454 * @param a1 first timestamp
455 * @param a2 second timestamp
456 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
458 struct GNUNET_TIME_Relative
459 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
460 struct GNUNET_TIME_Relative a2)
462 struct GNUNET_TIME_Relative ret;
464 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
465 return GNUNET_TIME_UNIT_FOREVER_REL;
466 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
469 return GNUNET_TIME_UNIT_FOREVER_REL;
471 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
477 * Subtract relative timestamp from the other.
479 * @param a1 first timestamp
480 * @param a2 second timestamp
481 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
483 struct GNUNET_TIME_Relative
484 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
485 struct GNUNET_TIME_Relative a2)
487 struct GNUNET_TIME_Relative ret;
489 if (a2.rel_value_us >= a1.rel_value_us)
490 return GNUNET_TIME_UNIT_ZERO;
491 if (a1.rel_value_us == UINT64_MAX)
492 return GNUNET_TIME_UNIT_FOREVER_REL;
493 ret.rel_value_us = a1.rel_value_us - a2.rel_value_us;
499 * Convert relative time to network byte order.
501 * @param a time to convert
502 * @return time in network byte order
504 struct GNUNET_TIME_RelativeNBO
505 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
507 struct GNUNET_TIME_RelativeNBO ret;
509 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
515 * Convert relative time from network byte order.
517 * @param a time to convert
518 * @return time in host byte order
520 struct GNUNET_TIME_Relative
521 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
523 struct GNUNET_TIME_Relative ret;
525 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
532 * Convert absolute time to network byte order.
534 * @param a time to convert
535 * @return time in network byte order
537 struct GNUNET_TIME_AbsoluteNBO
538 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
540 struct GNUNET_TIME_AbsoluteNBO ret;
542 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
548 * Convert absolute time from network byte order.
550 * @param a time to convert
551 * @return time in host byte order
553 struct GNUNET_TIME_Absolute
554 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
556 struct GNUNET_TIME_Absolute ret;
558 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
565 * Return the current year (i.e. '2011').
568 GNUNET_TIME_get_current_year ()
577 return t->tm_year + 1900;
582 * Convert an expiration time to the respective year (rounds)
584 * @param at absolute time
585 * @return year a year (after 1970), 0 on error
588 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
593 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
597 return t->tm_year + 1900;
603 * Convert a year to an expiration time of January 1st of that year.
605 * @param year a year (after 1970, please ;-)).
606 * @return absolute time for January 1st of that year.
608 struct GNUNET_TIME_Absolute
609 GNUNET_TIME_year_to_time (unsigned int year)
611 struct GNUNET_TIME_Absolute ret;
615 memset (&t, 0, sizeof (t));
619 return GNUNET_TIME_absolute_get (); /* now */
621 t.tm_year = year - 1900;
627 GNUNET_break (tp != (time_t) - 1);
628 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */