bugfix
[oweals/gnunet.git] / src / util / time.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/time.c
23  * @author Christian Grothoff
24  * @brief functions for handling time and time arithmetic
25  */
26 #include "platform.h"
27 #include "gnunet_time_lib.h"
28
29
30 /**
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)).
33  *
34  * @return the current time
35  */
36 struct GNUNET_TIME_Absolute
37 GNUNET_TIME_absolute_get ()
38 {
39   struct GNUNET_TIME_Absolute ret;
40   struct timeval tv;
41
42   GETTIMEOFDAY (&tv, NULL);
43   ret.value = tv.tv_sec * 1000 + tv.tv_usec / 1000;
44   return ret;
45 }
46
47
48 /**
49  * Return relative time of 0ms.
50  */
51 struct GNUNET_TIME_Relative
52 GNUNET_TIME_relative_get_zero ()
53 {
54   static struct GNUNET_TIME_Relative zero;
55   return zero;
56 }
57
58 /**
59  * Return relative time of 1ms.
60  */
61 struct GNUNET_TIME_Relative
62 GNUNET_TIME_relative_get_unit ()
63 {
64   static struct GNUNET_TIME_Relative one = { 1 };
65   return one;
66 }
67
68 /**
69  * Return "forever".
70  */
71 struct GNUNET_TIME_Relative
72 GNUNET_TIME_relative_get_forever ()
73 {
74   static struct GNUNET_TIME_Relative forever = { (uint64_t) - 1LL };
75   return forever;
76 }
77
78 /**
79  * Return "forever".
80  */
81 struct GNUNET_TIME_Absolute
82 GNUNET_TIME_absolute_get_forever ()
83 {
84   static struct GNUNET_TIME_Absolute forever = { (uint64_t) - 1LL };
85   return forever;
86 }
87
88 /**
89  * Convert relative time to an absolute time in the
90  * future.
91  *
92  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
93  */
94 struct GNUNET_TIME_Absolute
95 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
96 {
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)
102     {
103       GNUNET_break (0);         /* overflow... */
104       return GNUNET_TIME_absolute_get_forever ();
105     }
106   ret.value = rel.value + now.value;
107   return ret;
108 }
109
110
111 /**
112  * Return the minimum of two relative time values.
113  *
114  * @return timestamp that is smaller
115  */
116 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
117                                                       GNUNET_TIME_Relative
118                                                       t1,
119                                                       struct
120                                                       GNUNET_TIME_Relative t2)
121 {
122   return (t1.value < t2.value) ? t1 : t2;
123 }
124
125 /**
126  * Given a timestamp in the future, how much time
127  * remains until then?
128  *
129  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
130  */
131 struct GNUNET_TIME_Relative
132 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
133 {
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;
141   return ret;
142 }
143
144 /**
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.
148  *
149  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
150  */
151 struct GNUNET_TIME_Relative
152 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
153                                      struct GNUNET_TIME_Absolute end)
154 {
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;
161   return ret;
162 }
163
164 /**
165  * Get the duration of an operation as the
166  * difference of the current time and the given start time "hence".
167  *
168  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
169  */
170 struct GNUNET_TIME_Relative
171 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute hence)
172 {
173   struct GNUNET_TIME_Absolute now;
174   struct GNUNET_TIME_Relative ret;
175
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;
181   return ret;
182 }
183
184
185 /**
186  * Add a given relative duration to the
187  * given start time.
188  *
189  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
190  */
191 struct GNUNET_TIME_Absolute
192 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
193                           struct GNUNET_TIME_Relative duration)
194 {
195   struct GNUNET_TIME_Absolute ret;
196
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)
201     {
202       GNUNET_break (0);
203       return GNUNET_TIME_absolute_get_forever ();
204     }
205   ret.value = start.value + duration.value;
206   return ret;
207 }
208
209 /**
210  * Multiply relative time by a given factor.
211  *
212  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
213  */
214 struct GNUNET_TIME_Relative
215 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
216                                unsigned int factor)
217 {
218   struct GNUNET_TIME_Relative ret;
219   if (factor == 0)
220     return GNUNET_TIME_relative_get_zero ();
221   ret.value = rel.value * factor;
222   if (ret.value / factor != rel.value)
223     {
224       GNUNET_break (0);
225       return GNUNET_TIME_relative_get_forever ();
226     }
227   return ret;
228 }
229
230 /**
231  * Add relative times together.
232  *
233  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
234  */
235 struct GNUNET_TIME_Relative
236 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
237                           struct GNUNET_TIME_Relative a2)
238 {
239   struct GNUNET_TIME_Relative ret;
240
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)
244     {
245       GNUNET_break (0);
246       return GNUNET_TIME_relative_get_forever ();
247     }
248   ret.value = a1.value + a2.value;
249   return ret;
250 }
251
252
253 /**
254  * Convert relative time to network byte order.
255  */
256 struct GNUNET_TIME_RelativeNBO
257 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
258 {
259   struct GNUNET_TIME_RelativeNBO ret;
260   ret.value = GNUNET_htonll (a.value);
261   return ret;
262 }
263
264 /**
265  * Convert relative time from network byte order.
266  */
267 struct GNUNET_TIME_Relative
268 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
269 {
270   struct GNUNET_TIME_Relative ret;
271   ret.value = GNUNET_ntohll (a.value);
272   return ret;
273
274 }
275
276 /**
277  * Convert absolute time to network byte order.
278  */
279 struct GNUNET_TIME_AbsoluteNBO
280 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
281 {
282   struct GNUNET_TIME_AbsoluteNBO ret;
283   ret.value = GNUNET_htonll (a.value);
284   return ret;
285 }
286
287 /**
288  * Convert absolute time from network byte order.
289  */
290 struct GNUNET_TIME_Absolute
291 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
292 {
293   struct GNUNET_TIME_Absolute ret;
294   ret.value = GNUNET_ntohll (a.value);
295   return ret;
296
297 }
298
299
300
301 /* end of time.c */