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 = tv.tv_sec * 1000 + tv.tv_usec / 1000;
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;
111 * Given a timestamp in the future, how much time
112 * remains until then?
114 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
116 struct GNUNET_TIME_Relative
117 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
119 struct GNUNET_TIME_Relative ret;
120 if (future.value == (uint64_t) - 1LL)
121 return GNUNET_TIME_relative_get_forever ();
122 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
123 if (now.value > future.value)
124 return GNUNET_TIME_relative_get_zero ();
125 ret.value = future.value - now.value;
130 * Compute the time difference between the given start and end times.
131 * Use this function instead of actual subtraction to ensure that
132 * "FOREVER" and overflows are handeled correctly.
134 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
136 struct GNUNET_TIME_Relative
137 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
138 struct GNUNET_TIME_Absolute end)
140 struct GNUNET_TIME_Relative ret;
141 if (end.value == (uint64_t) - 1LL)
142 return GNUNET_TIME_relative_get_forever ();
143 if (end.value < start.value)
144 return GNUNET_TIME_relative_get_zero ();
145 ret.value = end.value - start.value;
150 * Get the duration of an operation as the
151 * difference of the current time and the given start time "hence".
153 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
155 struct GNUNET_TIME_Relative
156 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute hence)
158 struct GNUNET_TIME_Absolute now;
159 struct GNUNET_TIME_Relative ret;
161 now = GNUNET_TIME_absolute_get ();
162 GNUNET_assert (hence.value != (uint64_t) - 1LL);
163 if (hence.value > now.value)
164 return GNUNET_TIME_relative_get_zero ();
165 ret.value = now.value - hence.value;
171 * Add a given relative duration to the
174 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
176 struct GNUNET_TIME_Absolute
177 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
178 struct GNUNET_TIME_Relative duration)
180 struct GNUNET_TIME_Absolute ret;
182 if ((start.value == (uint64_t) - 1LL) ||
183 (duration.value == (uint64_t) - 1LL))
184 return GNUNET_TIME_absolute_get_forever ();
185 if (start.value + duration.value < start.value)
188 return GNUNET_TIME_absolute_get_forever ();
190 ret.value = start.value + duration.value;
195 * Multiply relative time by a given factor.
197 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
199 struct GNUNET_TIME_Relative
200 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
203 struct GNUNET_TIME_Relative ret;
205 return GNUNET_TIME_relative_get_zero ();
206 ret.value = rel.value * factor;
207 if (ret.value / factor != rel.value)
210 return GNUNET_TIME_relative_get_forever ();
216 * Add relative times together.
218 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
220 struct GNUNET_TIME_Relative
221 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
222 struct GNUNET_TIME_Relative a2)
224 struct GNUNET_TIME_Relative ret;
226 if ((a1.value == (uint64_t) - 1LL) || (a2.value == (uint64_t) - 1LL))
227 return GNUNET_TIME_relative_get_forever ();
228 if (a1.value + a2.value < a1.value)
231 return GNUNET_TIME_relative_get_forever ();
233 ret.value = a1.value + a2.value;
239 * Convert relative time to network byte order.
241 struct GNUNET_TIME_RelativeNBO
242 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
244 struct GNUNET_TIME_RelativeNBO ret;
245 ret.value = GNUNET_htonll (a.value);
250 * Convert relative time from network byte order.
252 struct GNUNET_TIME_Relative
253 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
255 struct GNUNET_TIME_Relative ret;
256 ret.value = GNUNET_ntohll (a.value);
262 * Convert absolute time to network byte order.
264 struct GNUNET_TIME_AbsoluteNBO
265 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
267 struct GNUNET_TIME_AbsoluteNBO ret;
268 ret.value = GNUNET_htonll (a.value);
273 * Convert absolute time from network byte order.
275 struct GNUNET_TIME_Absolute
276 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
278 struct GNUNET_TIME_Absolute ret;
279 ret.value = GNUNET_ntohll (a.value);