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 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 */