aboutsummaryrefslogtreecommitdiff
path: root/src/util/time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/time.c')
-rw-r--r--src/util/time.c79
1 files changed, 45 insertions, 34 deletions
diff --git a/src/util/time.c b/src/util/time.c
index 3dc2ef03a..f4d4c4070 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -34,7 +34,7 @@ static long long timestamp_offset;
34 * @param offset the offset to skew the locale time by 34 * @param offset the offset to skew the locale time by
35 */ 35 */
36void 36void
37GNUNET_TIME_set_offset(long long offset) 37GNUNET_TIME_set_offset (long long offset)
38{ 38{
39 timestamp_offset = offset; 39 timestamp_offset = offset;
40} 40}
@@ -53,8 +53,8 @@ GNUNET_TIME_absolute_get ()
53 53
54 GETTIMEOFDAY (&tv, NULL); 54 GETTIMEOFDAY (&tv, NULL);
55 ret.abs_value = 55 ret.abs_value =
56 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) + 56 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) +
57 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset; 57 ((uint64_t) tv.tv_usec / 1000LL)) + timestamp_offset;
58 return ret; 58 return ret;
59} 59}
60 60
@@ -66,6 +66,7 @@ struct GNUNET_TIME_Relative
66GNUNET_TIME_relative_get_zero () 66GNUNET_TIME_relative_get_zero ()
67{ 67{
68 static struct GNUNET_TIME_Relative zero; 68 static struct GNUNET_TIME_Relative zero;
69
69 return zero; 70 return zero;
70} 71}
71 72
@@ -77,6 +78,7 @@ struct GNUNET_TIME_Absolute
77GNUNET_TIME_absolute_get_zero () 78GNUNET_TIME_absolute_get_zero ()
78{ 79{
79 static struct GNUNET_TIME_Absolute zero; 80 static struct GNUNET_TIME_Absolute zero;
81
80 return zero; 82 return zero;
81} 83}
82 84
@@ -120,14 +122,16 @@ struct GNUNET_TIME_Absolute
120GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel) 122GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
121{ 123{
122 struct GNUNET_TIME_Absolute ret; 124 struct GNUNET_TIME_Absolute ret;
125
123 if (rel.rel_value == UINT64_MAX) 126 if (rel.rel_value == UINT64_MAX)
124 return GNUNET_TIME_absolute_get_forever (); 127 return GNUNET_TIME_absolute_get_forever ();
125 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 128 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
129
126 if (rel.rel_value + now.abs_value < rel.rel_value) 130 if (rel.rel_value + now.abs_value < rel.rel_value)
127 { 131 {
128 GNUNET_break (0); /* overflow... */ 132 GNUNET_break (0); /* overflow... */
129 return GNUNET_TIME_absolute_get_forever (); 133 return GNUNET_TIME_absolute_get_forever ();
130 } 134 }
131 ret.abs_value = rel.rel_value + now.abs_value; 135 ret.abs_value = rel.rel_value + now.abs_value;
132 return ret; 136 return ret;
133} 137}
@@ -208,9 +212,11 @@ struct GNUNET_TIME_Relative
208GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future) 212GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
209{ 213{
210 struct GNUNET_TIME_Relative ret; 214 struct GNUNET_TIME_Relative ret;
215
211 if (future.abs_value == UINT64_MAX) 216 if (future.abs_value == UINT64_MAX)
212 return GNUNET_TIME_relative_get_forever (); 217 return GNUNET_TIME_relative_get_forever ();
213 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 218 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
219
214 if (now.abs_value > future.abs_value) 220 if (now.abs_value > future.abs_value)
215 return GNUNET_TIME_relative_get_zero (); 221 return GNUNET_TIME_relative_get_zero ();
216 ret.rel_value = future.abs_value - now.abs_value; 222 ret.rel_value = future.abs_value - now.abs_value;
@@ -229,6 +235,7 @@ GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
229 struct GNUNET_TIME_Absolute end) 235 struct GNUNET_TIME_Absolute end)
230{ 236{
231 struct GNUNET_TIME_Relative ret; 237 struct GNUNET_TIME_Relative ret;
238
232 if (end.abs_value == UINT64_MAX) 239 if (end.abs_value == UINT64_MAX)
233 return GNUNET_TIME_relative_get_forever (); 240 return GNUNET_TIME_relative_get_forever ();
234 if (end.abs_value < start.abs_value) 241 if (end.abs_value < start.abs_value)
@@ -270,14 +277,13 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
270{ 277{
271 struct GNUNET_TIME_Absolute ret; 278 struct GNUNET_TIME_Absolute ret;
272 279
273 if ((start.abs_value == UINT64_MAX) || 280 if ((start.abs_value == UINT64_MAX) || (duration.rel_value == UINT64_MAX))
274 (duration.rel_value == UINT64_MAX))
275 return GNUNET_TIME_absolute_get_forever (); 281 return GNUNET_TIME_absolute_get_forever ();
276 if (start.abs_value + duration.rel_value < start.abs_value) 282 if (start.abs_value + duration.rel_value < start.abs_value)
277 { 283 {
278 GNUNET_break (0); 284 GNUNET_break (0);
279 return GNUNET_TIME_absolute_get_forever (); 285 return GNUNET_TIME_absolute_get_forever ();
280 } 286 }
281 ret.abs_value = start.abs_value + duration.rel_value; 287 ret.abs_value = start.abs_value + duration.rel_value;
282 return ret; 288 return ret;
283} 289}
@@ -291,15 +297,13 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
291 * @param duration some relative time to subtract 297 * @param duration some relative time to subtract
292 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise 298 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
293 */ 299 */
294struct GNUNET_TIME_Absolute 300struct GNUNET_TIME_Absolute
295GNUNET_TIME_absolute_subtract (struct 301GNUNET_TIME_absolute_subtract (struct
296 GNUNET_TIME_Absolute 302 GNUNET_TIME_Absolute
297 start, 303 start, struct GNUNET_TIME_Relative duration)
298 struct
299 GNUNET_TIME_Relative
300 duration)
301{ 304{
302 struct GNUNET_TIME_Absolute ret; 305 struct GNUNET_TIME_Absolute ret;
306
303 if (start.abs_value <= duration.rel_value) 307 if (start.abs_value <= duration.rel_value)
304 return GNUNET_TIME_UNIT_ZERO_ABS; 308 return GNUNET_TIME_UNIT_ZERO_ABS;
305 if (start.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value) 309 if (start.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
@@ -319,14 +323,15 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
319 unsigned int factor) 323 unsigned int factor)
320{ 324{
321 struct GNUNET_TIME_Relative ret; 325 struct GNUNET_TIME_Relative ret;
326
322 if (factor == 0) 327 if (factor == 0)
323 return GNUNET_TIME_relative_get_zero (); 328 return GNUNET_TIME_relative_get_zero ();
324 ret.rel_value = rel.rel_value * (unsigned long long) factor; 329 ret.rel_value = rel.rel_value * (unsigned long long) factor;
325 if (ret.rel_value / factor != rel.rel_value) 330 if (ret.rel_value / factor != rel.rel_value)
326 { 331 {
327 GNUNET_break (0); 332 GNUNET_break (0);
328 return GNUNET_TIME_relative_get_forever (); 333 return GNUNET_TIME_relative_get_forever ();
329 } 334 }
330 return ret; 335 return ret;
331} 336}
332 337
@@ -340,11 +345,12 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
340 */ 345 */
341struct GNUNET_TIME_Relative 346struct GNUNET_TIME_Relative
342GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel, 347GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
343 unsigned int factor) 348 unsigned int factor)
344{ 349{
345 struct GNUNET_TIME_Relative ret; 350 struct GNUNET_TIME_Relative ret;
346 if ( (factor == 0) || 351
347 (rel.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ) 352 if ((factor == 0) ||
353 (rel.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value))
348 return GNUNET_TIME_UNIT_FOREVER_REL; 354 return GNUNET_TIME_UNIT_FOREVER_REL;
349 ret.rel_value = rel.rel_value / (unsigned long long) factor; 355 ret.rel_value = rel.rel_value / (unsigned long long) factor;
350 return ret; 356 return ret;
@@ -397,10 +403,10 @@ GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
397 if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX)) 403 if ((a1.rel_value == UINT64_MAX) || (a2.rel_value == UINT64_MAX))
398 return GNUNET_TIME_relative_get_forever (); 404 return GNUNET_TIME_relative_get_forever ();
399 if (a1.rel_value + a2.rel_value < a1.rel_value) 405 if (a1.rel_value + a2.rel_value < a1.rel_value)
400 { 406 {
401 GNUNET_break (0); 407 GNUNET_break (0);
402 return GNUNET_TIME_relative_get_forever (); 408 return GNUNET_TIME_relative_get_forever ();
403 } 409 }
404 ret.rel_value = a1.rel_value + a2.rel_value; 410 ret.rel_value = a1.rel_value + a2.rel_value;
405 return ret; 411 return ret;
406} 412}
@@ -415,13 +421,13 @@ GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
415 */ 421 */
416struct GNUNET_TIME_Relative 422struct GNUNET_TIME_Relative
417GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1, 423GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
418 struct GNUNET_TIME_Relative a2) 424 struct GNUNET_TIME_Relative a2)
419{ 425{
420 struct GNUNET_TIME_Relative ret; 426 struct GNUNET_TIME_Relative ret;
421 427
422 if (a2.rel_value >= a1.rel_value) 428 if (a2.rel_value >= a1.rel_value)
423 return GNUNET_TIME_relative_get_zero (); 429 return GNUNET_TIME_relative_get_zero ();
424 if (a1.rel_value == UINT64_MAX) 430 if (a1.rel_value == UINT64_MAX)
425 return GNUNET_TIME_relative_get_forever (); 431 return GNUNET_TIME_relative_get_forever ();
426 ret.rel_value = a1.rel_value - a2.rel_value; 432 ret.rel_value = a1.rel_value - a2.rel_value;
427 return ret; 433 return ret;
@@ -438,6 +444,7 @@ struct GNUNET_TIME_RelativeNBO
438GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a) 444GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
439{ 445{
440 struct GNUNET_TIME_RelativeNBO ret; 446 struct GNUNET_TIME_RelativeNBO ret;
447
441 ret.rel_value__ = GNUNET_htonll (a.rel_value); 448 ret.rel_value__ = GNUNET_htonll (a.rel_value);
442 return ret; 449 return ret;
443} 450}
@@ -452,6 +459,7 @@ struct GNUNET_TIME_Relative
452GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a) 459GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
453{ 460{
454 struct GNUNET_TIME_Relative ret; 461 struct GNUNET_TIME_Relative ret;
462
455 ret.rel_value = GNUNET_ntohll (a.rel_value__); 463 ret.rel_value = GNUNET_ntohll (a.rel_value__);
456 return ret; 464 return ret;
457 465
@@ -467,6 +475,7 @@ struct GNUNET_TIME_AbsoluteNBO
467GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a) 475GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
468{ 476{
469 struct GNUNET_TIME_AbsoluteNBO ret; 477 struct GNUNET_TIME_AbsoluteNBO ret;
478
470 ret.abs_value__ = GNUNET_htonll (a.abs_value); 479 ret.abs_value__ = GNUNET_htonll (a.abs_value);
471 return ret; 480 return ret;
472} 481}
@@ -481,6 +490,7 @@ struct GNUNET_TIME_Absolute
481GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a) 490GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
482{ 491{
483 struct GNUNET_TIME_Absolute ret; 492 struct GNUNET_TIME_Absolute ret;
493
484 ret.abs_value = GNUNET_ntohll (a.abs_value__); 494 ret.abs_value = GNUNET_ntohll (a.abs_value__);
485 return ret; 495 return ret;
486 496
@@ -499,9 +509,10 @@ const char *
499GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time) 509GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time)
500{ 510{
501 static char time_string[21]; 511 static char time_string[21];
502 memset(time_string, 0, sizeof(time_string));
503 512
504 sprintf(time_string, "%llu", (unsigned long long) time.rel_value); 513 memset (time_string, 0, sizeof (time_string));
514
515 sprintf (time_string, "%llu", (unsigned long long) time.rel_value);
505 return (const char *) time_string; 516 return (const char *) time_string;
506} 517}
507 518