gnunet_time_lib.h (22867B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2001-2022 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your 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 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 21 /** 22 * @addtogroup libgnunetutil 23 * Multi-function utilities library for GNUnet programs 24 * @{ 25 * 26 * @author Christian Grothoff 27 * 28 * @file 29 * Functions related to time 30 * 31 * @defgroup time Time library 32 * Time and time calculations. 33 * @{ 34 */ 35 36 #ifndef GNUNET_TIME_LIB_H 37 #define GNUNET_TIME_LIB_H 38 39 #ifdef __cplusplus 40 extern "C" 41 { 42 #if 0 /* keep Emacsens' auto-indent happy */ 43 } 44 #endif 45 #endif 46 47 48 #include "gnunet_common.h" 49 50 /** 51 * Time for absolute times used by GNUnet, in microseconds. 52 */ 53 struct GNUNET_TIME_Absolute 54 { 55 /** 56 * The actual value. 57 * UINT64_MAX to represents "never". 58 */ 59 uint64_t abs_value_us; 60 }; 61 62 /** 63 * Time for timestamps used by GNUnet, in microseconds rounded to seconds. 64 */ 65 struct GNUNET_TIME_Timestamp 66 { 67 /** 68 * The actual value. Must be a round number of seconds in microseconds, 69 * or UINT64_MAX to represent "never". 70 */ 71 struct GNUNET_TIME_Absolute abs_time; 72 }; 73 74 /** 75 * Time for relative time used by GNUnet, in microseconds. 76 * Always positive, so we can only refer to future time. 77 */ 78 struct GNUNET_TIME_Relative 79 { 80 /** 81 * The actual value. 82 * UINT64_MAX represents "forever". 83 */ 84 uint64_t rel_value_us; 85 }; 86 87 GNUNET_NETWORK_STRUCT_BEGIN 88 89 /** 90 * Time for relative time used by GNUnet, in microseconds and in network byte order. 91 */ 92 struct GNUNET_TIME_RelativeNBO 93 { 94 /** 95 * The actual value (in network byte order). 96 * UINT64_MAX represents "forever". 97 */ 98 uint64_t rel_value_us__ GNUNET_PACKED; 99 }; 100 101 102 /** 103 * Time for absolute time used by GNUnet, in microseconds and in network byte order. 104 */ 105 struct GNUNET_TIME_AbsoluteNBO 106 { 107 /** 108 * The actual value (in network byte order). 109 * UINT64_MAX represents "never". 110 */ 111 uint64_t abs_value_us__ GNUNET_PACKED; 112 }; 113 114 /** 115 * Time for timestamps used by GNUnet, in seconds and in network byte order. 116 */ 117 struct GNUNET_TIME_TimestampNBO 118 { 119 /** 120 * The actual value. Must be round number in seconds *or* 121 * UINT64_MAX to represent "never". 122 */ 123 struct GNUNET_TIME_AbsoluteNBO abs_time_nbo; 124 }; 125 126 GNUNET_NETWORK_STRUCT_END 127 128 /** 129 * Relative time zero. 130 */ 131 #define GNUNET_TIME_UNIT_ZERO ((struct GNUNET_TIME_Relative){0}) 132 133 /** 134 * Absolute time zero. 135 */ 136 #define GNUNET_TIME_UNIT_ZERO_ABS ((struct GNUNET_TIME_Absolute){0}) 137 138 /** 139 * Timestamp of zero. 140 */ 141 #define GNUNET_TIME_UNIT_ZERO_TS ((struct GNUNET_TIME_Timestamp){{0}}) 142 143 /** 144 * One microsecond, our basic time unit. 145 */ 146 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_ () 147 148 /** 149 * One millisecond. 150 */ 151 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_ () 152 153 /** 154 * One second. 155 */ 156 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_ () 157 158 /** 159 * One minute. 160 */ 161 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_ () 162 163 /** 164 * One hour. 165 */ 166 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_ () 167 168 /** 169 * One day. 170 */ 171 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply ( \ 172 GNUNET_TIME_UNIT_HOURS, 24) 173 174 /** 175 * One week. 176 */ 177 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply ( \ 178 GNUNET_TIME_UNIT_DAYS, 7) 179 180 /** 181 * One month (30 days). 182 */ 183 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply ( \ 184 GNUNET_TIME_UNIT_DAYS, 30) 185 186 /** 187 * One year (365 days). 188 */ 189 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply ( \ 190 GNUNET_TIME_UNIT_DAYS, 365) 191 192 /** 193 * Constant used to specify "forever". This constant 194 * will be treated specially in all time operations. 195 */ 196 #define GNUNET_TIME_UNIT_FOREVER_REL \ 197 ((struct GNUNET_TIME_Relative){UINT64_MAX}) 198 199 /** 200 * Constant used to specify "forever". This constant 201 * will be treated specially in all time operations. 202 */ 203 #define GNUNET_TIME_UNIT_FOREVER_ABS \ 204 ((struct GNUNET_TIME_Absolute){UINT64_MAX}) 205 #define GNUNET_TIME_UNIT_NEVER_ABS \ 206 ((struct GNUNET_TIME_Absolute){UINT64_MAX}) 207 208 /** 209 * Constant used to specify "forever". This constant 210 * will be treated specially in all time operations. 211 */ 212 #define GNUNET_TIME_UNIT_FOREVER_TS \ 213 ((struct GNUNET_TIME_Timestamp){{UINT64_MAX}}) 214 215 216 /** 217 * Threshold after which exponential backoff should not increase (15 m). 218 */ 219 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD \ 220 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) 221 222 223 /** 224 * Perform our standard exponential back-off calculation, starting at 1 ms 225 * and then going by a factor of 2 up unto a maximum of 15 m. 226 * 227 * @param r current backoff time, initially zero 228 */ 229 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min ( \ 230 GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \ 231 GNUNET_TIME_relative_multiply ( \ 232 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2)) 233 234 235 /** 236 * Convert @a ts to human-readable timestamp. 237 * Note that the returned value will be overwritten if this function 238 * is called again. 239 * 240 * @param ts the timestamp to convert 241 * @return statically allocated string, will change on the next call 242 */ 243 const char * 244 GNUNET_TIME_timestamp2s (struct GNUNET_TIME_Timestamp ts); 245 246 247 /** 248 * @ingroup time 249 * Like `asctime`, except for GNUnet time. Converts a GNUnet internal 250 * absolute time (which is in UTC) to a string in local time. 251 * Note that the returned value will be overwritten if this function 252 * is called again. 253 * 254 * @param ts the absolute time to convert 255 * @return timestamp in human-readable form in local time 256 */ 257 const char * 258 GNUNET_TIME_absolute2s (struct GNUNET_TIME_Absolute ts); 259 260 261 /** 262 * @ingroup time 263 * Give relative time in human-readable fancy format. 264 * This is one of the very few calls in the entire API that is 265 * NOT reentrant! 266 * 267 * @param delta time in milli seconds 268 * @param do_round are we allowed to round a bit? 269 * @return string in human-readable form 270 */ 271 const char * 272 GNUNET_TIME_relative2s (struct GNUNET_TIME_Relative delta, 273 bool do_round); 274 275 276 /** 277 * Randomized exponential back-off, starting at 1 ms 278 * and going up by a factor of 2+r, where 0 <= r <= 0.5, up 279 * to a maximum of the given threshold. 280 * 281 * @param rt current backoff time, initially zero 282 * @param threshold maximum value for backoff 283 * @return the next backoff time 284 */ 285 struct GNUNET_TIME_Relative 286 GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt, 287 struct GNUNET_TIME_Relative threshold); 288 289 290 /** 291 * Return a random time value between 0.5*r and 1.5*r. 292 * 293 * @param r input time for scaling 294 * @return randomized time 295 */ 296 struct GNUNET_TIME_Relative 297 GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r); 298 299 300 /** 301 * Return relative time of 0ms. 302 */ 303 struct GNUNET_TIME_Relative 304 GNUNET_TIME_relative_get_zero_ (void); 305 306 307 /** 308 * Return absolute time of 0ms. 309 */ 310 struct GNUNET_TIME_Absolute 311 GNUNET_TIME_absolute_get_zero_ (void); 312 313 314 /** 315 * Return relative time of 1 microsecond. 316 */ 317 struct GNUNET_TIME_Relative 318 GNUNET_TIME_relative_get_unit_ (void); 319 320 321 /** 322 * Return relative time of 1ms. 323 */ 324 struct GNUNET_TIME_Relative 325 GNUNET_TIME_relative_get_millisecond_ (void); 326 327 328 /** 329 * Return relative time of 1s. 330 */ 331 struct GNUNET_TIME_Relative 332 GNUNET_TIME_relative_get_second_ (void); 333 334 335 /** 336 * Return relative time of 1 minute. 337 */ 338 struct GNUNET_TIME_Relative 339 GNUNET_TIME_relative_get_minute_ (void); 340 341 342 /** 343 * Return relative time of 1 hour. 344 */ 345 struct GNUNET_TIME_Relative 346 GNUNET_TIME_relative_get_hour_ (void); 347 348 349 /** 350 * Return "forever". 351 */ 352 struct GNUNET_TIME_Relative 353 GNUNET_TIME_relative_get_forever_ (void); 354 355 356 /** 357 * Return "forever". 358 */ 359 struct GNUNET_TIME_Absolute 360 GNUNET_TIME_absolute_get_forever_ (void); 361 362 363 /** 364 * Get the current time. 365 * 366 * @return the current time 367 */ 368 struct GNUNET_TIME_Absolute 369 GNUNET_TIME_absolute_get (void); 370 371 372 /** 373 * Convert relative time to an absolute time in the 374 * future. 375 * 376 * @param rel relative time to convert 377 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow) 378 */ 379 struct GNUNET_TIME_Absolute 380 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel); 381 382 383 /** 384 * Convert relative time to a timestamp in the 385 * future. 386 * 387 * @param rel relative time to convert 388 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow) 389 */ 390 struct GNUNET_TIME_Timestamp 391 GNUNET_TIME_relative_to_timestamp (struct GNUNET_TIME_Relative rel); 392 393 394 /** 395 * Round an absolute time to a timestamp. 396 * 397 * @param at time to round 398 * @return the result 399 */ 400 struct GNUNET_TIME_Timestamp 401 GNUNET_TIME_absolute_to_timestamp (struct GNUNET_TIME_Absolute at); 402 403 404 /** 405 * Get timestamp representing the current time. 406 * 407 * @return current time, rounded down to seconds 408 */ 409 struct GNUNET_TIME_Timestamp 410 GNUNET_TIME_timestamp_get (void); 411 412 413 /** 414 * Compare two absolute times. 415 * 416 * @param t1 first time 417 * @param op compare operator 418 * @param t2 second time 419 * @return true if @a t1 @a op @a t2 420 */ 421 #define GNUNET_TIME_absolute_cmp(t1,op,t2) \ 422 ((void) (1 op 2), (t1).abs_value_us op (t2).abs_value_us) 423 424 425 /** 426 * Compare two timestamps 427 * 428 * @param t1 first timestamp 429 * @param op compare operator 430 * @param t2 second timestamp 431 * @return true if @a t1 @a op @a t2 432 */ 433 #define GNUNET_TIME_timestamp_cmp(t1,op,t2) \ 434 GNUNET_TIME_absolute_cmp ((t1).abs_time,op,(t2).abs_time) 435 436 437 /** 438 * Compare two relative times. 439 * 440 * @param t1 first time 441 * @param op compare operator 442 * @param t2 second time 443 * @return true if @a t1 @a op @a t2 444 */ 445 #define GNUNET_TIME_relative_cmp(t1,op,t2) \ 446 ((void) (1 op 2), (t1).rel_value_us op (t2).rel_value_us) 447 448 449 /** 450 * Return the minimum of two relative time values. 451 * 452 * @param t1 first timestamp 453 * @param t2 other timestamp 454 * @return timestamp that is smaller 455 */ 456 struct GNUNET_TIME_Relative 457 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1, 458 struct GNUNET_TIME_Relative t2); 459 460 461 /** 462 * Return the maximum of two relative time values. 463 * 464 * @param t1 first timestamp 465 * @param t2 other timestamp 466 * @return timestamp that is larger 467 */ 468 struct GNUNET_TIME_Relative 469 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1, 470 struct GNUNET_TIME_Relative t2); 471 472 473 /** 474 * Return the minimum of two absolute time values. 475 * 476 * @param t1 first timestamp 477 * @param t2 other timestamp 478 * @return timestamp that is smaller 479 */ 480 struct GNUNET_TIME_Absolute 481 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1, 482 struct GNUNET_TIME_Absolute t2); 483 484 485 /** 486 * Return the maximum of two absolute time values. 487 * 488 * @param t1 first timestamp 489 * @param t2 other timestamp 490 * @return timestamp that is smaller 491 */ 492 struct GNUNET_TIME_Absolute 493 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1, 494 struct GNUNET_TIME_Absolute t2); 495 496 497 /** 498 * Round down absolute time @a at to multiple of @a rt. 499 * 500 * @param at absolute time to round 501 * @param rt multiple to round to (non-zero) 502 * @return rounded time 503 */ 504 struct GNUNET_TIME_Absolute 505 GNUNET_TIME_absolute_round_down (struct GNUNET_TIME_Absolute at, 506 struct GNUNET_TIME_Relative rt); 507 508 509 /** 510 * Return the maximum of two timestamps. 511 * 512 * @param t1 first timestamp 513 * @param t2 other timestamp 514 * @return timestamp that is smaller 515 */ 516 struct GNUNET_TIME_Timestamp 517 GNUNET_TIME_timestamp_max (struct GNUNET_TIME_Timestamp t1, 518 struct GNUNET_TIME_Timestamp t2); 519 520 521 /** 522 * Return the minimum of two timestamps. 523 * 524 * @param t1 first timestamp 525 * @param t2 other timestamp 526 * @return timestamp that is smaller 527 */ 528 struct GNUNET_TIME_Timestamp 529 GNUNET_TIME_timestamp_min (struct GNUNET_TIME_Timestamp t1, 530 struct GNUNET_TIME_Timestamp t2); 531 532 533 /** 534 * Given a timestamp in the future, how much time 535 * remains until then? 536 * 537 * @param future some absolute time, typically in the future 538 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER. 539 */ 540 struct GNUNET_TIME_Relative 541 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future); 542 543 544 /** 545 * Test if @a a1 and @a a2 are equal within a margin of 546 * error of @a t. 547 * 548 * @param a1 time to compare 549 * @param a2 time to compare 550 * @param t tolerance to apply 551 * @return true if "|a1-a2|<=t" holds. 552 */ 553 bool 554 GNUNET_TIME_absolute_approx_eq (struct GNUNET_TIME_Absolute a1, 555 struct GNUNET_TIME_Absolute a2, 556 struct GNUNET_TIME_Relative t); 557 558 559 /** 560 * Calculate the estimate time of arrival/completion 561 * for an operation. 562 * 563 * @param start when did the operation start? 564 * @param finished how much has been done? 565 * @param total how much must be done overall (same unit as for "finished") 566 * @return remaining duration for the operation, 567 * assuming it continues at the same speed 568 */ 569 struct GNUNET_TIME_Relative 570 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, 571 uint64_t finished, 572 uint64_t total); 573 574 575 /** 576 * Compute the time difference between the given start and end times. 577 * Use this function instead of actual subtraction to ensure that 578 * "FOREVER" and overflows are handled correctly. 579 * 580 * @param start some absolute time 581 * @param end some absolute time (typically larger or equal to start) 582 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start 583 */ 584 struct GNUNET_TIME_Relative 585 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start, 586 struct GNUNET_TIME_Absolute end); 587 588 589 /** 590 * Get the duration of an operation as the 591 * difference of the current time and the given start time "hence". 592 * 593 * @param whence some absolute time, typically in the past 594 * @return 0 if hence > now, otherwise now-hence. 595 */ 596 struct GNUNET_TIME_Relative 597 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence); 598 599 600 /** 601 * Add a given relative duration to the 602 * given start time. 603 * 604 * @param start some absolute time 605 * @param duration some relative time to add 606 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise 607 */ 608 struct GNUNET_TIME_Absolute 609 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start, 610 struct GNUNET_TIME_Relative duration); 611 612 613 /** 614 * Subtract a given relative duration from the 615 * given start time. 616 * 617 * @param start some absolute time 618 * @param duration some relative time to subtract 619 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise 620 */ 621 struct GNUNET_TIME_Absolute 622 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start, 623 struct GNUNET_TIME_Relative duration); 624 625 626 /** 627 * Multiply relative time by a given factor. 628 * 629 * @param rel some duration 630 * @param factor double to multiply with 631 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 632 */ 633 struct GNUNET_TIME_Relative 634 GNUNET_TIME_relative_multiply_double (struct GNUNET_TIME_Relative rel, 635 double factor); 636 637 /** 638 * Multiply relative time by a given factor. 639 * 640 * @param rel some duration 641 * @param factor integer to multiply with 642 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 643 */ 644 struct GNUNET_TIME_Relative 645 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, 646 unsigned long long factor); 647 648 649 /** 650 * Saturating multiply relative time by a given factor. 651 * 652 * @param rel some duration 653 * @param factor integer to multiply with 654 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 655 */ 656 struct GNUNET_TIME_Relative 657 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel, 658 unsigned long long factor); 659 660 661 /** 662 * Divide relative time by a given factor. 663 * 664 * @param rel some duration 665 * @param factor integer to divide by 666 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor 667 */ 668 struct GNUNET_TIME_Relative 669 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel, 670 unsigned long long factor); 671 672 673 /** 674 * Add relative times together. 675 * 676 * @param a1 some relative time 677 * @param a2 some other relative time 678 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise 679 */ 680 struct GNUNET_TIME_Relative 681 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1, 682 struct GNUNET_TIME_Relative a2); 683 684 685 /** 686 * Subtract relative timestamp from the other. 687 * 688 * @param a1 first timestamp 689 * @param a2 second timestamp 690 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise 691 */ 692 struct GNUNET_TIME_Relative 693 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1, 694 struct GNUNET_TIME_Relative a2); 695 696 697 /** 698 * Convert relative time to network byte order. 699 * 700 * @param a time to convert 701 * @return converted time value 702 */ 703 struct GNUNET_TIME_RelativeNBO 704 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a); 705 706 707 /** 708 * Convert relative time from network byte order. 709 * 710 * @param a time to convert 711 * @return converted time value 712 */ 713 struct GNUNET_TIME_Relative 714 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a); 715 716 717 /** 718 * Convert absolute time to network byte order. 719 * 720 * @param a time to convert 721 * @return converted time value 722 */ 723 struct GNUNET_TIME_AbsoluteNBO 724 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a); 725 726 727 /** 728 * Convert timestamp to network byte order. 729 * 730 * @param t time to convert 731 * @return converted time value 732 */ 733 struct GNUNET_TIME_TimestampNBO 734 GNUNET_TIME_timestamp_hton (struct GNUNET_TIME_Timestamp t); 735 736 737 /** 738 * Convert milliseconds after the UNIX epoch to absolute time. 739 * 740 * @param ms_after_epoch millisecond timestamp to convert 741 * @return converted time value 742 */ 743 struct GNUNET_TIME_Absolute 744 GNUNET_TIME_absolute_from_ms (uint64_t ms_after_epoch); 745 746 747 /** 748 * Test if @a abs is never. 749 * 750 * @return true if it is. 751 */ 752 bool 753 GNUNET_TIME_absolute_is_never (struct GNUNET_TIME_Absolute abs); 754 755 756 /** 757 * Test if @a abs is truly in the past (excluding now). 758 * 759 * @return true if it is. 760 */ 761 bool 762 GNUNET_TIME_absolute_is_past (struct GNUNET_TIME_Absolute abs); 763 764 765 /** 766 * Test if @a abs is truly zero. 767 * 768 * @return true if it is. 769 */ 770 bool 771 GNUNET_TIME_absolute_is_zero (struct GNUNET_TIME_Absolute abs); 772 773 774 /** 775 * Test if @a abs is truly in the future (excluding now). 776 * 777 * @return true if it is. 778 */ 779 bool 780 GNUNET_TIME_absolute_is_future (struct GNUNET_TIME_Absolute abs); 781 782 783 /** 784 * Test if @a rel is forever. 785 * 786 * @return true if it is. 787 */ 788 bool 789 GNUNET_TIME_relative_is_forever (struct GNUNET_TIME_Relative rel); 790 791 792 /** 793 * Test if @a rel is zero. 794 * 795 * @return true if it is. 796 */ 797 bool 798 GNUNET_TIME_relative_is_zero (struct GNUNET_TIME_Relative rel); 799 800 801 /** 802 * Convert seconds after the UNIX epoch to absolute time. 803 * 804 * @param s_after_epoch seconds after epoch to convert 805 * @return converted time value 806 */ 807 struct GNUNET_TIME_Absolute 808 GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch); 809 810 811 /** 812 * Convert seconds after the UNIX epoch to timestamp. 813 * 814 * @param s_after_epoch seconds after epoch to convert 815 * @return converted time value 816 */ 817 struct GNUNET_TIME_Timestamp 818 GNUNET_TIME_timestamp_from_s (uint64_t s_after_epoch); 819 820 821 /** 822 * Convert timestamp to number of seconds after the UNIX epoch. 823 * 824 * @param ts timestamp to convert 825 * @return converted time value 826 */ 827 uint64_t 828 GNUNET_TIME_timestamp_to_s (struct GNUNET_TIME_Timestamp ts); 829 830 831 /** 832 * Convert absolute time from network byte order. 833 * 834 * @param a time to convert 835 * @return converted time value 836 */ 837 struct GNUNET_TIME_Absolute 838 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a); 839 840 841 /** 842 * Convert timestamp from network byte order. 843 * 844 * @param tn time to convert 845 * @return converted time value 846 */ 847 struct GNUNET_TIME_Timestamp 848 GNUNET_TIME_timestamp_ntoh (struct GNUNET_TIME_TimestampNBO tn); 849 850 851 /** 852 * Set the timestamp offset for this instance. 853 * 854 * @param offset the offset to skew the locale time by 855 */ 856 void 857 GNUNET_TIME_set_offset (long long offset); 858 859 860 /** 861 * Get the timestamp offset for this instance. 862 * 863 * @return the offset we currently skew the locale time by 864 */ 865 long long 866 GNUNET_TIME_get_offset (void); 867 868 869 /** 870 * Return the current year (e.g. '2011'). 871 */ 872 unsigned int 873 GNUNET_TIME_get_current_year (void); 874 875 876 /** 877 * Convert a year to an expiration time of January 1st of that year. 878 * 879 * @param year a year (after 1970, please ;-)). 880 * @return absolute time for January 1st of that year. 881 */ 882 struct GNUNET_TIME_Absolute 883 GNUNET_TIME_year_to_time (unsigned int year); 884 885 886 /** 887 * Convert an expiration time to the respective year (rounds) 888 * 889 * @param at absolute time 890 * @return year a year (after 1970), 0 on error 891 */ 892 unsigned int 893 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at); 894 895 896 /** 897 * A configuration object. 898 */ 899 struct GNUNET_CONFIGURATION_Handle; 900 901 902 /** 903 * Obtain the current time and make sure it is monotonically 904 * increasing. Guards against systems without an RTC or 905 * clocks running backwards and other nasty surprises. Does 906 * not guarantee that the returned time is near the current 907 * time returned by #GNUNET_TIME_absolute_get(). Two 908 * subsequent calls (within a short time period) may return the 909 * same value. Persists the last returned time on disk to 910 * ensure that time never goes backwards. As a result, the 911 * resulting value can be used to check if a message is the 912 * "most recent" value and replays of older messages (from 913 * the same origin) would be discarded. 914 * 915 * @param cfg configuration, used to determine where to 916 * store the time; user can also insist RTC is working 917 * nicely and disable the feature 918 * @return monotonically increasing time 919 */ 920 struct GNUNET_TIME_Absolute 921 GNUNET_TIME_absolute_get_monotonic ( 922 const struct GNUNET_CONFIGURATION_Handle *cfg); 923 924 925 #if 0 /* keep Emacsens' auto-indent happy */ 926 { 927 #endif 928 #ifdef __cplusplus 929 } 930 #endif 931 932 /* ifndef GNUNET_TIME_LIB_H */ 933 #endif 934 935 /** @} */ /* end of group time */ 936 937 /** @} */ /* end of group addition */ 938 939 /* end of gnunet_time_lib.h */