2 This file is part of GNUnet.
3 (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., 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_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 * Variable used to simulate clock skew. Used for testing, never in production.
34 static long long timestamp_offset;
37 * Set the timestamp offset for this instance.
39 * @param offset the offset to skew the locale time by
42 GNUNET_TIME_set_offset (long long offset)
44 timestamp_offset = offset;
49 * Get the timestamp offset for this instance.
51 * @return the offset we currently skew the locale time by
54 GNUNET_TIME_get_offset ()
56 return timestamp_offset;
61 * Get the current time (works just as "time", just that we use the
62 * unit of time that the cron-jobs use (and is 64 bit)).
64 * @return the current time
66 struct GNUNET_TIME_Absolute
67 GNUNET_TIME_absolute_get ()
69 struct GNUNET_TIME_Absolute ret;
72 GETTIMEOFDAY (&tv, NULL);
74 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) +
75 ((uint64_t) tv.tv_usec)) + timestamp_offset;
81 * Return relative time of 0ms.
83 struct GNUNET_TIME_Relative
84 GNUNET_TIME_relative_get_zero_ ()
86 static struct GNUNET_TIME_Relative zero;
93 * Return absolute time of 0ms.
95 struct GNUNET_TIME_Absolute
96 GNUNET_TIME_absolute_get_zero_ ()
98 static struct GNUNET_TIME_Absolute zero;
105 * Return relative time of 1us.
107 struct GNUNET_TIME_Relative
108 GNUNET_TIME_relative_get_unit_ ()
110 static struct GNUNET_TIME_Relative one = { 1 };
116 * Return relative time of 1ms.
118 struct GNUNET_TIME_Relative
119 GNUNET_TIME_relative_get_millisecond_ ()
121 static struct GNUNET_TIME_Relative one = { 1000 };
127 * Return relative time of 1s.
129 struct GNUNET_TIME_Relative
130 GNUNET_TIME_relative_get_second_ ()
132 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL };
138 * Return relative time of 1 minute.
140 struct GNUNET_TIME_Relative
141 GNUNET_TIME_relative_get_minute_ ()
143 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL };
149 * Return relative time of 1 hour.
151 struct GNUNET_TIME_Relative
152 GNUNET_TIME_relative_get_hour_ ()
154 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL };
162 struct GNUNET_TIME_Relative
163 GNUNET_TIME_relative_get_forever_ ()
165 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
172 struct GNUNET_TIME_Absolute
173 GNUNET_TIME_absolute_get_forever_ ()
175 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
180 * Convert relative time to an absolute time in the
183 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
185 struct GNUNET_TIME_Absolute
186 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
188 struct GNUNET_TIME_Absolute ret;
190 if (rel.rel_value_us == UINT64_MAX)
191 return GNUNET_TIME_UNIT_FOREVER_ABS;
192 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
194 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
196 GNUNET_break (0); /* overflow... */
197 return GNUNET_TIME_UNIT_FOREVER_ABS;
199 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
205 * Return the minimum of two relative time values.
207 * @param t1 first timestamp
208 * @param t2 other timestamp
209 * @return timestamp that is smaller
211 struct GNUNET_TIME_Relative
212 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
213 struct GNUNET_TIME_Relative t2)
215 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2;
220 * Return the maximum of two relative time values.
222 * @param t1 first timestamp
223 * @param t2 other timestamp
224 * @return timestamp that is larger
226 struct GNUNET_TIME_Relative
227 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
228 struct GNUNET_TIME_Relative t2)
230 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2;
236 * Return the minimum of two relative time values.
238 * @param t1 first timestamp
239 * @param t2 other timestamp
240 * @return timestamp that is smaller
242 struct GNUNET_TIME_Absolute
243 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
244 struct GNUNET_TIME_Absolute t2)
246 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2;
251 * Return the maximum of two relative time values.
253 * @param t1 first timestamp
254 * @param t2 other timestamp
255 * @return timestamp that is bigger
257 struct GNUNET_TIME_Absolute
258 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
259 struct GNUNET_TIME_Absolute t2)
261 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2;
266 * Given a timestamp in the future, how much time
267 * remains until then?
269 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
271 struct GNUNET_TIME_Relative
272 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
274 struct GNUNET_TIME_Relative ret;
276 if (future.abs_value_us == UINT64_MAX)
277 return GNUNET_TIME_UNIT_FOREVER_REL;
278 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
280 if (now.abs_value_us > future.abs_value_us)
281 return GNUNET_TIME_UNIT_ZERO;
282 ret.rel_value_us = future.abs_value_us - now.abs_value_us;
287 * Compute the time difference between the given start and end times.
288 * Use this function instead of actual subtraction to ensure that
289 * "FOREVER" and overflows are handled correctly.
291 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
293 struct GNUNET_TIME_Relative
294 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
295 struct GNUNET_TIME_Absolute end)
297 struct GNUNET_TIME_Relative ret;
299 if (end.abs_value_us == UINT64_MAX)
300 return GNUNET_TIME_UNIT_FOREVER_REL;
301 if (end.abs_value_us < start.abs_value_us)
302 return GNUNET_TIME_UNIT_ZERO;
303 ret.rel_value_us = end.abs_value_us - start.abs_value_us;
308 * Get the duration of an operation as the
309 * difference of the current time and the given start time "whence".
311 * @return aborts if whence==FOREVER, 0 if whence > now, otherwise now-whence.
313 struct GNUNET_TIME_Relative
314 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
316 struct GNUNET_TIME_Absolute now;
317 struct GNUNET_TIME_Relative ret;
319 now = GNUNET_TIME_absolute_get ();
320 GNUNET_assert (whence.abs_value_us != UINT64_MAX);
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 ret.rel_value_us = rel.rel_value_us * (unsigned long long) factor;
389 if (ret.rel_value_us / factor != rel.rel_value_us)
392 return GNUNET_TIME_UNIT_FOREVER_REL;
399 * Divide relative time by a given factor.
401 * @param rel some duration
402 * @param factor integer to divide by
403 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
405 struct GNUNET_TIME_Relative
406 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
409 struct GNUNET_TIME_Relative ret;
412 (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
413 return GNUNET_TIME_UNIT_FOREVER_REL;
414 ret.rel_value_us = rel.rel_value_us / (unsigned long long) factor;
420 * Calculate the estimate time of arrival/completion
423 * @param start when did the operation start?
424 * @param finished how much has been done?
425 * @param total how much must be done overall (same unit as for "finished")
426 * @return remaining duration for the operation,
427 * assuming it continues at the same speed
429 struct GNUNET_TIME_Relative
430 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
433 struct GNUNET_TIME_Relative dur;
435 struct GNUNET_TIME_Relative ret;
437 GNUNET_break (finished <= total);
438 if (finished >= total)
439 return GNUNET_TIME_UNIT_ZERO;
441 return GNUNET_TIME_UNIT_FOREVER_REL;
442 dur = GNUNET_TIME_absolute_get_duration (start);
443 exp = ((double) dur.rel_value_us) * ((double) total) / ((double) finished);
444 ret.rel_value_us = ((uint64_t) exp) - dur.rel_value_us;
450 * Add relative times together.
452 * @param a1 first timestamp
453 * @param a2 second timestamp
454 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
456 struct GNUNET_TIME_Relative
457 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
458 struct GNUNET_TIME_Relative a2)
460 struct GNUNET_TIME_Relative ret;
462 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
463 return GNUNET_TIME_UNIT_FOREVER_REL;
464 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
467 return GNUNET_TIME_UNIT_FOREVER_REL;
469 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
475 * Subtract relative timestamp from the other.
477 * @param a1 first timestamp
478 * @param a2 second timestamp
479 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
481 struct GNUNET_TIME_Relative
482 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
483 struct GNUNET_TIME_Relative a2)
485 struct GNUNET_TIME_Relative ret;
487 if (a2.rel_value_us >= a1.rel_value_us)
488 return GNUNET_TIME_UNIT_ZERO;
489 if (a1.rel_value_us == UINT64_MAX)
490 return GNUNET_TIME_UNIT_FOREVER_REL;
491 ret.rel_value_us = a1.rel_value_us - a2.rel_value_us;
497 * Convert relative time to network byte order.
499 * @param a time to convert
500 * @return time in network byte order
502 struct GNUNET_TIME_RelativeNBO
503 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
505 struct GNUNET_TIME_RelativeNBO ret;
507 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
513 * Convert relative time from network byte order.
515 * @param a time to convert
516 * @return time in host byte order
518 struct GNUNET_TIME_Relative
519 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
521 struct GNUNET_TIME_Relative ret;
523 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
530 * Convert absolute time to network byte order.
532 * @param a time to convert
533 * @return time in network byte order
535 struct GNUNET_TIME_AbsoluteNBO
536 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
538 struct GNUNET_TIME_AbsoluteNBO ret;
540 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
546 * Convert absolute time from network byte order.
548 * @param a time to convert
549 * @return time in host byte order
551 struct GNUNET_TIME_Absolute
552 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
554 struct GNUNET_TIME_Absolute ret;
556 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
563 * Return the current year (i.e. '2011').
566 GNUNET_TIME_get_current_year ()
575 return t->tm_year + 1900;
580 * Convert an expiration time to the respective year (rounds)
582 * @param at absolute time
583 * @return year a year (after 1970), 0 on error
586 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
591 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
595 return t->tm_year + 1900;
601 * Convert a year to an expiration time of January 1st of that year.
603 * @param year a year (after 1970, please ;-)).
604 * @return absolute time for January 1st of that year.
606 struct GNUNET_TIME_Absolute
607 GNUNET_TIME_year_to_time (unsigned int year)
609 struct GNUNET_TIME_Absolute ret;
613 memset (&t, 0, sizeof (t));
617 return GNUNET_TIME_absolute_get (); /* now */
619 t.tm_year = year - 1900;
625 GNUNET_break (tp != (time_t) - 1);
626 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */