diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-07-23 20:07:44 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-07-23 20:07:44 +0200 |
commit | d44f867e71e13be3970e8803e0ae238e8d3ed7ee (patch) | |
tree | 61181efa1d80a797a414bc3311fe19018effdb0e | |
parent | 3477569ac4e5ee54003aa0b7a8309ad10b4bb2bb (diff) | |
download | gnunet-d44f867e71e13be3970e8803e0ae238e8d3ed7ee.tar.gz gnunet-d44f867e71e13be3970e8803e0ae238e8d3ed7ee.zip |
introduce new TIME helper functions
-rw-r--r-- | src/include/gnunet_time_lib.h | 42 | ||||
-rw-r--r-- | src/util/time.c | 41 |
2 files changed, 81 insertions, 2 deletions
diff --git a/src/include/gnunet_time_lib.h b/src/include/gnunet_time_lib.h index b9e87fcbe..bddf5c6bf 100644 --- a/src/include/gnunet_time_lib.h +++ b/src/include/gnunet_time_lib.h | |||
@@ -530,6 +530,44 @@ GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a); | |||
530 | 530 | ||
531 | 531 | ||
532 | /** | 532 | /** |
533 | * Convert milliseconds after the UNIX epoch to absolute time. | ||
534 | * | ||
535 | * @param ms_after_epoch millisecond timestamp to convert | ||
536 | * @return converted time value | ||
537 | */ | ||
538 | struct GNUNET_TIME_Absolute | ||
539 | GNUNET_TIME_absolute_from_ms (uint64_t ms_after_epoch); | ||
540 | |||
541 | |||
542 | /** | ||
543 | * Test if @a abs is never. | ||
544 | * | ||
545 | * @return true if it is. | ||
546 | */ | ||
547 | bool | ||
548 | GNUNET_TIME_absolute_is_never (struct GNUNET_TIME_Absolute abs); | ||
549 | |||
550 | |||
551 | /** | ||
552 | * Test if @a rel is forever. | ||
553 | * | ||
554 | * @return true if it is. | ||
555 | */ | ||
556 | bool | ||
557 | GNUNET_TIME_relative_is_forever (struct GNUNET_TIME_Relative rel); | ||
558 | |||
559 | |||
560 | /** | ||
561 | * Convert seconds after the UNIX epoch to absolute time. | ||
562 | * | ||
563 | * @param s_after_epoch seconds after epoch to convert | ||
564 | * @return converted time value | ||
565 | */ | ||
566 | struct GNUNET_TIME_Absolute | ||
567 | GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch); | ||
568 | |||
569 | |||
570 | /** | ||
533 | * Convert absolute time from network byte order. | 571 | * Convert absolute time from network byte order. |
534 | * | 572 | * |
535 | * @param a time to convert | 573 | * @param a time to convert |
@@ -609,8 +647,8 @@ struct GNUNET_CONFIGURATION_Handle; | |||
609 | * @return monotonically increasing time | 647 | * @return monotonically increasing time |
610 | */ | 648 | */ |
611 | struct GNUNET_TIME_Absolute | 649 | struct GNUNET_TIME_Absolute |
612 | GNUNET_TIME_absolute_get_monotonic (const struct | 650 | GNUNET_TIME_absolute_get_monotonic ( |
613 | GNUNET_CONFIGURATION_Handle *cfg); | 651 | const struct GNUNET_CONFIGURATION_Handle *cfg); |
614 | 652 | ||
615 | 653 | ||
616 | #if 0 /* keep Emacsens' auto-indent happy */ | 654 | #if 0 /* keep Emacsens' auto-indent happy */ |
diff --git a/src/util/time.c b/src/util/time.c index 456ee660b..15a4160e2 100644 --- a/src/util/time.c +++ b/src/util/time.c | |||
@@ -470,6 +470,47 @@ GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a) | |||
470 | } | 470 | } |
471 | 471 | ||
472 | 472 | ||
473 | bool | ||
474 | GNUNET_TIME_absolute_is_never (struct GNUNET_TIME_Absolute abs) | ||
475 | { | ||
476 | return abs.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | ||
477 | } | ||
478 | |||
479 | |||
480 | bool | ||
481 | GNUNET_TIME_relative_is_forever (struct GNUNET_TIME_Relative rel) | ||
482 | { | ||
483 | return rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us; | ||
484 | } | ||
485 | |||
486 | |||
487 | struct GNUNET_TIME_Absolute | ||
488 | GNUNET_TIME_absolute_from_ms (uint64_t ms_after_epoch) | ||
489 | { | ||
490 | struct GNUNET_TIME_Absolute ret; | ||
491 | |||
492 | ret.abs_value_us = GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us | ||
493 | * ms_after_epoch; | ||
494 | if (ret.abs_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us != | ||
495 | ms_after_epoch) | ||
496 | ret = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
497 | return ret; | ||
498 | } | ||
499 | |||
500 | |||
501 | struct GNUNET_TIME_Absolute | ||
502 | GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch) | ||
503 | { | ||
504 | struct GNUNET_TIME_Absolute ret; | ||
505 | |||
506 | ret.abs_value_us = GNUNET_TIME_UNIT_SECONDS.rel_value_us * s_after_epoch; | ||
507 | if (ret.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us != | ||
508 | s_after_epoch) | ||
509 | ret = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
510 | return ret; | ||
511 | } | ||
512 | |||
513 | |||
473 | struct GNUNET_TIME_Absolute | 514 | struct GNUNET_TIME_Absolute |
474 | GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a) | 515 | GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a) |
475 | { | 516 | { |