2 This file is part of GNUnet.
3 (C) 2001, 2002, 2006, 2009 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 2, 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_time_lib.h"
31 * Get the current time (works just as "time", just that we use the
32 * unit of time that the cron-jobs use (and is 64 bit)).
34 * @return the current time
36 struct GNUNET_TIME_Absolute
37 GNUNET_TIME_absolute_get ()
39 struct GNUNET_TIME_Absolute ret;
42 GETTIMEOFDAY (&tv, NULL);
43 ret.value = (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) + ((uint64_t) tv.tv_usec / 1000LL));
49 * Return relative time of 0ms.
51 struct GNUNET_TIME_Relative
52 GNUNET_TIME_relative_get_zero ()
54 static struct GNUNET_TIME_Relative zero;
59 * Return relative time of 1ms.
61 struct GNUNET_TIME_Relative
62 GNUNET_TIME_relative_get_unit ()
64 static struct GNUNET_TIME_Relative one = { 1 };
71 struct GNUNET_TIME_Relative
72 GNUNET_TIME_relative_get_forever ()
74 static struct GNUNET_TIME_Relative forever = { (uint64_t) - 1LL };
81 struct GNUNET_TIME_Absolute
82 GNUNET_TIME_absolute_get_forever ()
84 static struct GNUNET_TIME_Absolute forever = { (uint64_t) - 1LL };
89 * Convert relative time to an absolute time in the
92 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
94 struct GNUNET_TIME_Absolute
95 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
97 struct GNUNET_TIME_Absolute ret;
98 if (rel.value == (uint64_t) -1LL)
99 return GNUNET_TIME_absolute_get_forever ();
100 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
101 if (rel.value + now.value < rel.value)
103 GNUNET_break (0); /* overflow... */
104 return GNUNET_TIME_absolute_get_forever ();
106 ret.value = rel.value + now.value;
112 * Return the minimum of two relative time values.
114 * @return timestamp that is smaller
116 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
120 GNUNET_TIME_Relative t2)
122 return (t1.value < t2.value) ? t1 : t2;
126 * Given a timestamp in the future, how much time
127 * remains until then?
129 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
131 struct GNUNET_TIME_Relative
132 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
134 struct GNUNET_TIME_Relative ret;
135 if (future.value == (uint64_t) - 1LL)
136 return GNUNET_TIME_relative_get_forever ();
137 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
138 if (now.value > future.value)
139 return GNUNET_TIME_relative_get_zero ();
140 ret.value = future.value - now.value;
145 * Compute the time difference between the given start and end times.
146 * Use this function instead of actual subtraction to ensure that
147 * "FOREVER" and overflows are handeled correctly.
149 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
151 struct GNUNET_TIME_Relative
152 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
153 struct GNUNET_TIME_Absolute end)
155 struct GNUNET_TIME_Relative ret;
156 if (end.value == (uint64_t) - 1LL)
157 return GNUNET_TIME_relative_get_forever ();
158 if (end.value < start.value)
159 return GNUNET_TIME_relative_get_zero ();
160 ret.value = end.value - start.value;
165 * Get the duration of an operation as the
166 * difference of the current time and the given start time "hence".
168 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
170 struct GNUNET_TIME_Relative
171 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute hence)
173 struct GNUNET_TIME_Absolute now;
174 struct GNUNET_TIME_Relative ret;
176 now = GNUNET_TIME_absolute_get ();
177 GNUNET_assert (hence.value != (uint64_t) - 1LL);
178 if (hence.value > now.value)
179 return GNUNET_TIME_relative_get_zero ();
180 ret.value = now.value - hence.value;
186 * Add a given relative duration to the
189 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
191 struct GNUNET_TIME_Absolute
192 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
193 struct GNUNET_TIME_Relative duration)
195 struct GNUNET_TIME_Absolute ret;
197 if ((start.value == (uint64_t) - 1LL) ||
198 (duration.value == (uint64_t) - 1LL))
199 return GNUNET_TIME_absolute_get_forever ();
200 if (start.value + duration.value < start.value)
203 return GNUNET_TIME_absolute_get_forever ();
205 ret.value = start.value + duration.value;
210 * Multiply relative time by a given factor.
212 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
214 struct GNUNET_TIME_Relative
215 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
218 struct GNUNET_TIME_Relative ret;
220 return GNUNET_TIME_relative_get_zero ();
221 ret.value = rel.value * (unsigned long long) factor;
222 if (ret.value / factor != rel.value)
225 return GNUNET_TIME_relative_get_forever ();
231 * Add relative times together.
233 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
235 struct GNUNET_TIME_Relative
236 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
237 struct GNUNET_TIME_Relative a2)
239 struct GNUNET_TIME_Relative ret;
241 if ((a1.value == (uint64_t) - 1LL) || (a2.value == (uint64_t) - 1LL))
242 return GNUNET_TIME_relative_get_forever ();
243 if (a1.value + a2.value < a1.value)
246 return GNUNET_TIME_relative_get_forever ();
248 ret.value = a1.value + a2.value;
254 * Convert relative time to network byte order.
256 struct GNUNET_TIME_RelativeNBO
257 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
259 struct GNUNET_TIME_RelativeNBO ret;
260 ret.value = GNUNET_htonll (a.value);
265 * Convert relative time from network byte order.
267 struct GNUNET_TIME_Relative
268 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
270 struct GNUNET_TIME_Relative ret;
271 ret.value = GNUNET_ntohll (a.value);
277 * Convert absolute time to network byte order.
279 struct GNUNET_TIME_AbsoluteNBO
280 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
282 struct GNUNET_TIME_AbsoluteNBO ret;
283 ret.value = GNUNET_htonll (a.value);
288 * Convert absolute time from network byte order.
290 struct GNUNET_TIME_Absolute
291 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
293 struct GNUNET_TIME_Absolute ret;
294 ret.value = GNUNET_ntohll (a.value);