gnunet-android

GNUnet for Android
Log | Files | Refs | README

gnunet_time_lib.h (25353B)


      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  * Quantities by which we support round up absolute time values.
    237  */
    238 enum GNUNET_TIME_RounderInterval
    239 {
    240   /**
    241    * No rounding up.
    242    */
    243   GNUNET_TIME_RI_NONE = 0,
    244 
    245   /**
    246    * Round up to a multiple of seconds.
    247    */
    248   GNUNET_TIME_RI_SECOND,
    249 
    250   /**
    251    * Round up to the next minute.
    252    */
    253   GNUNET_TIME_RI_MINUTE,
    254 
    255   /**
    256    * Round up to the next hour.
    257    */
    258   GNUNET_TIME_RI_HOUR,
    259 
    260   /**
    261    * Round up to the next day.
    262    */
    263   GNUNET_TIME_RI_DAY,
    264 
    265   /**
    266    * Round up to the next calendar week.
    267    */
    268   GNUNET_TIME_RI_WEEK,
    269 
    270   /**
    271    * Round up to the next month.
    272    */
    273   GNUNET_TIME_RI_MONTH,
    274 
    275   /**
    276    * Round up to the next quarter.
    277    */
    278   GNUNET_TIME_RI_QUARTER,
    279 
    280   /**
    281    * Round up to the next year.
    282    */
    283   GNUNET_TIME_RI_YEAR
    284 };
    285 
    286 
    287 /**
    288  * Convert a relative time to the corresponding rounding
    289  * interval.
    290  *
    291  * @param rel relative time to convert
    292  * @return rounding interval, #GNUNET_TIME_RI_NONE if
    293  *   either @a rel is zero or if the input does not match exactly
    294  *   any of the supported rounding intervals
    295  */
    296 enum GNUNET_TIME_RounderInterval
    297 GNUNET_TIME_relative_to_round_interval (struct GNUNET_TIME_Relative rel);
    298 
    299 
    300 /**
    301  * Convert rounding interval given as a string to the enum value.
    302  *
    303  * @param ri_str rounding interval as string
    304  * @param[out] ri set to enum value
    305  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
    306  */
    307 enum GNUNET_GenericReturnValue
    308 GNUNET_TIME_string_to_round_interval (const char *ri_str,
    309                                       enum GNUNET_TIME_RounderInterval *ri);
    310 
    311 
    312 /**
    313  * Convert rounding interval to string.
    314  *
    315  * @param ri the rounding interval
    316  * @return NULL on failure (invalid enum value)
    317  */
    318 const char *
    319 GNUNET_TIME_round_interval2s (enum GNUNET_TIME_RounderInterval ri);
    320 
    321 
    322 /**
    323  * Round up the given @a at to the interval @a ri.
    324  * NEVER/FOREVER always remains NEVER/FOREVER.
    325  *
    326  * @param at some absolute time to round
    327  * @param ri how much to round up
    328  * @return rounded up value of @a at
    329  */
    330 struct GNUNET_TIME_Absolute
    331 GNUNET_TIME_round_up (struct GNUNET_TIME_Absolute at,
    332                       enum GNUNET_TIME_RounderInterval ri);
    333 
    334 
    335 /**
    336  * Round @at down to the start of the next interval @a ri.
    337  * NEVER/FOREVER always remains NEVER/FOREVER.
    338  *
    339  * @param at some absolute time to round
    340  * @param ri how much to round down
    341  * @return rounded up value of @a at
    342  */
    343 struct GNUNET_TIME_Absolute
    344 GNUNET_TIME_round_down (struct GNUNET_TIME_Absolute at,
    345                         enum GNUNET_TIME_RounderInterval ri);
    346 
    347 
    348 /**
    349  * Convert @a ts to human-readable timestamp.
    350  * Note that the returned value will be overwritten if this function
    351  * is called again.
    352  *
    353  * @param ts the timestamp to convert
    354  * @return statically allocated string, will change on the next call
    355  */
    356 const char *
    357 GNUNET_TIME_timestamp2s (struct GNUNET_TIME_Timestamp ts);
    358 
    359 
    360 /**
    361  * @ingroup time
    362  * Like `asctime`, except for GNUnet time.  Converts a GNUnet internal
    363  * absolute time (which is in UTC) to a string in local time.
    364  * Note that the returned value will be overwritten if this function
    365  * is called again.
    366  *
    367  * @param ts the absolute time to convert
    368  * @return timestamp in human-readable form in local time
    369  */
    370 const char *
    371 GNUNET_TIME_absolute2s (struct GNUNET_TIME_Absolute ts);
    372 
    373 
    374 /**
    375  * @ingroup time
    376  * Give relative time in human-readable fancy format.
    377  * This is one of the very few calls in the entire API that is
    378  * NOT reentrant!
    379  *
    380  * @param delta time in milli seconds
    381  * @param do_round are we allowed to round a bit?
    382  * @return string in human-readable form
    383  */
    384 const char *
    385 GNUNET_TIME_relative2s (struct GNUNET_TIME_Relative delta,
    386                         bool do_round);
    387 
    388 
    389 /**
    390  * Randomized exponential back-off, starting at 1 ms
    391  * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
    392  * to a maximum of the given threshold.
    393  *
    394  * @param rt current backoff time, initially zero
    395  * @param threshold maximum value for backoff
    396  * @return the next backoff time
    397  */
    398 struct GNUNET_TIME_Relative
    399 GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt,
    400                                 struct GNUNET_TIME_Relative threshold);
    401 
    402 
    403 /**
    404  * Return a random time value between 0.5*r and 1.5*r.
    405  *
    406  * @param r input time for scaling
    407  * @return randomized time
    408  */
    409 struct GNUNET_TIME_Relative
    410 GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r);
    411 
    412 
    413 /**
    414  * Return relative time of 0ms.
    415  */
    416 struct GNUNET_TIME_Relative
    417 GNUNET_TIME_relative_get_zero_ (void);
    418 
    419 
    420 /**
    421  * Return absolute time of 0ms.
    422  */
    423 struct GNUNET_TIME_Absolute
    424 GNUNET_TIME_absolute_get_zero_ (void);
    425 
    426 
    427 /**
    428  * Return relative time of 1 microsecond.
    429  */
    430 struct GNUNET_TIME_Relative
    431 GNUNET_TIME_relative_get_unit_ (void);
    432 
    433 
    434 /**
    435  * Return relative time of 1ms.
    436  */
    437 struct GNUNET_TIME_Relative
    438 GNUNET_TIME_relative_get_millisecond_ (void);
    439 
    440 
    441 /**
    442  * Return relative time of 1s.
    443  */
    444 struct GNUNET_TIME_Relative
    445 GNUNET_TIME_relative_get_second_ (void);
    446 
    447 
    448 /**
    449  * Return relative time of 1 minute.
    450  */
    451 struct GNUNET_TIME_Relative
    452 GNUNET_TIME_relative_get_minute_ (void);
    453 
    454 
    455 /**
    456  * Return relative time of 1 hour.
    457  */
    458 struct GNUNET_TIME_Relative
    459 GNUNET_TIME_relative_get_hour_ (void);
    460 
    461 
    462 /**
    463  * Return "forever".
    464  */
    465 struct GNUNET_TIME_Relative
    466 GNUNET_TIME_relative_get_forever_ (void);
    467 
    468 
    469 /**
    470  * Return "forever".
    471  */
    472 struct GNUNET_TIME_Absolute
    473 GNUNET_TIME_absolute_get_forever_ (void);
    474 
    475 
    476 /**
    477  * Get the current time.
    478  *
    479  * @return the current time
    480  */
    481 struct GNUNET_TIME_Absolute
    482 GNUNET_TIME_absolute_get (void);
    483 
    484 
    485 /**
    486  * Convert relative time to an absolute time in the
    487  * future.
    488  *
    489  * @param rel relative time to convert
    490  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
    491  */
    492 struct GNUNET_TIME_Absolute
    493 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
    494 
    495 
    496 /**
    497  * Convert relative time to a timestamp in the
    498  * future.
    499  *
    500  * @param rel relative time to convert
    501  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
    502  */
    503 struct GNUNET_TIME_Timestamp
    504 GNUNET_TIME_relative_to_timestamp (struct GNUNET_TIME_Relative rel);
    505 
    506 
    507 /**
    508  * Round an absolute time to a timestamp.
    509  *
    510  * @param at time to round
    511  * @return the result
    512  */
    513 struct GNUNET_TIME_Timestamp
    514 GNUNET_TIME_absolute_to_timestamp (struct GNUNET_TIME_Absolute at);
    515 
    516 
    517 /**
    518  * Get timestamp representing the current time.
    519  *
    520  * @return current time, rounded down to seconds
    521  */
    522 struct GNUNET_TIME_Timestamp
    523 GNUNET_TIME_timestamp_get (void);
    524 
    525 
    526 /**
    527  * Compare two absolute times.
    528  *
    529  * @param t1 first time
    530  * @param op compare operator
    531  * @param t2 second time
    532  * @return true if @a t1 @a op @a t2
    533  */
    534 #define GNUNET_TIME_absolute_cmp(t1,op,t2) \
    535         ((void) (1 op 2), (t1).abs_value_us op (t2).abs_value_us)
    536 
    537 
    538 /**
    539  * Compare two timestamps
    540  *
    541  * @param t1 first timestamp
    542  * @param op compare operator
    543  * @param t2 second timestamp
    544  * @return true if @a t1 @a op @a t2
    545  */
    546 #define GNUNET_TIME_timestamp_cmp(t1,op,t2) \
    547         GNUNET_TIME_absolute_cmp ((t1).abs_time,op,(t2).abs_time)
    548 
    549 
    550 /**
    551  * Compare two relative times.
    552  *
    553  * @param t1 first time
    554  * @param op compare operator
    555  * @param t2 second time
    556  * @return true if @a t1 @a op @a t2
    557  */
    558 #define GNUNET_TIME_relative_cmp(t1,op,t2) \
    559         ((void) (1 op 2), (t1).rel_value_us op (t2).rel_value_us)
    560 
    561 
    562 /**
    563  * Return the minimum of two relative time values.
    564  *
    565  * @param t1 first timestamp
    566  * @param t2 other timestamp
    567  * @return timestamp that is smaller
    568  */
    569 struct GNUNET_TIME_Relative
    570 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
    571                           struct GNUNET_TIME_Relative t2);
    572 
    573 
    574 /**
    575  * Return the maximum of two relative time values.
    576  *
    577  * @param t1 first timestamp
    578  * @param t2 other timestamp
    579  * @return timestamp that is larger
    580  */
    581 struct GNUNET_TIME_Relative
    582 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
    583                           struct GNUNET_TIME_Relative t2);
    584 
    585 
    586 /**
    587  * Return the minimum of two absolute time values.
    588  *
    589  * @param t1 first timestamp
    590  * @param t2 other timestamp
    591  * @return timestamp that is smaller
    592  */
    593 struct GNUNET_TIME_Absolute
    594 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
    595                           struct GNUNET_TIME_Absolute t2);
    596 
    597 
    598 /**
    599  * Return the maximum of two absolute time values.
    600  *
    601  * @param t1 first timestamp
    602  * @param t2 other timestamp
    603  * @return timestamp that is smaller
    604  */
    605 struct GNUNET_TIME_Absolute
    606 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
    607                           struct GNUNET_TIME_Absolute t2);
    608 
    609 
    610 /**
    611  * Round down absolute time @a at to multiple of @a rt.
    612  *
    613  * @param at absolute time to round
    614  * @param rt multiple to round to (non-zero)
    615  * @return rounded time
    616  */
    617 struct GNUNET_TIME_Absolute
    618 GNUNET_TIME_absolute_round_down (struct GNUNET_TIME_Absolute at,
    619                                  struct GNUNET_TIME_Relative rt);
    620 
    621 
    622 /**
    623  * Return the maximum of two timestamps.
    624  *
    625  * @param t1 first timestamp
    626  * @param t2 other timestamp
    627  * @return timestamp that is smaller
    628  */
    629 struct GNUNET_TIME_Timestamp
    630 GNUNET_TIME_timestamp_max (struct GNUNET_TIME_Timestamp t1,
    631                            struct GNUNET_TIME_Timestamp t2);
    632 
    633 
    634 /**
    635  * Return the minimum of two timestamps.
    636  *
    637  * @param t1 first timestamp
    638  * @param t2 other timestamp
    639  * @return timestamp that is smaller
    640  */
    641 struct GNUNET_TIME_Timestamp
    642 GNUNET_TIME_timestamp_min (struct GNUNET_TIME_Timestamp t1,
    643                            struct GNUNET_TIME_Timestamp t2);
    644 
    645 
    646 /**
    647  * Given a timestamp in the future, how much time
    648  * remains until then?
    649  *
    650  * @param future some absolute time, typically in the future
    651  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
    652  */
    653 struct GNUNET_TIME_Relative
    654 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
    655 
    656 
    657 /**
    658  * Test if @a a1 and @a a2 are equal within a margin of
    659  * error of @a t.
    660  *
    661  * @param a1 time to compare
    662  * @param a2 time to compare
    663  * @param t tolerance to apply
    664  * @return true if "|a1-a2|<=t" holds.
    665  */
    666 bool
    667 GNUNET_TIME_absolute_approx_eq (struct GNUNET_TIME_Absolute a1,
    668                                 struct GNUNET_TIME_Absolute a2,
    669                                 struct GNUNET_TIME_Relative t);
    670 
    671 
    672 /**
    673  * Calculate the estimate time of arrival/completion
    674  * for an operation.
    675  *
    676  * @param start when did the operation start?
    677  * @param finished how much has been done?
    678  * @param total how much must be done overall (same unit as for "finished")
    679  * @return remaining duration for the operation,
    680  *        assuming it continues at the same speed
    681  */
    682 struct GNUNET_TIME_Relative
    683 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
    684                            uint64_t finished,
    685                            uint64_t total);
    686 
    687 
    688 /**
    689  * Compute the time difference between the given start and end times.
    690  * Use this function instead of actual subtraction to ensure that
    691  * "FOREVER" and overflows are handled correctly.
    692  *
    693  * @param start some absolute time
    694  * @param end some absolute time (typically larger or equal to start)
    695  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
    696  */
    697 struct GNUNET_TIME_Relative
    698 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
    699                                      struct GNUNET_TIME_Absolute end);
    700 
    701 
    702 /**
    703  * Get the duration of an operation as the
    704  * difference of the current time and the given start time "hence".
    705  *
    706  * @param whence some absolute time, typically in the past
    707  * @return 0 if hence > now, otherwise now-hence.
    708  */
    709 struct GNUNET_TIME_Relative
    710 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
    711 
    712 
    713 /**
    714  * Add a given relative duration to the
    715  * given start time.
    716  *
    717  * @param start some absolute time
    718  * @param duration some relative time to add
    719  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
    720  */
    721 struct GNUNET_TIME_Absolute
    722 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
    723                           struct GNUNET_TIME_Relative duration);
    724 
    725 
    726 /**
    727  * Subtract a given relative duration from the
    728  * given start time.
    729  *
    730  * @param start some absolute time
    731  * @param duration some relative time to subtract
    732  * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
    733  */
    734 struct GNUNET_TIME_Absolute
    735 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
    736                                struct GNUNET_TIME_Relative duration);
    737 
    738 
    739 /**
    740  * Multiply relative time by a given factor.
    741  *
    742  * @param rel some duration
    743  * @param factor double to multiply with
    744  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
    745  */
    746 struct GNUNET_TIME_Relative
    747 GNUNET_TIME_relative_multiply_double (struct GNUNET_TIME_Relative rel,
    748                                       double factor);
    749 
    750 /**
    751  * Multiply relative time by a given factor.
    752  *
    753  * @param rel some duration
    754  * @param factor integer to multiply with
    755  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
    756  */
    757 struct GNUNET_TIME_Relative
    758 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
    759                                unsigned long long factor);
    760 
    761 
    762 /**
    763  * Saturating multiply relative time by a given factor.
    764  *
    765  * @param rel some duration
    766  * @param factor integer to multiply with
    767  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
    768  */
    769 struct GNUNET_TIME_Relative
    770 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
    771                                           unsigned long long factor);
    772 
    773 
    774 /**
    775  * Divide relative time by a given factor.
    776  *
    777  * @param rel some duration
    778  * @param factor integer to divide by
    779  * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
    780  */
    781 struct GNUNET_TIME_Relative
    782 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
    783                              unsigned long long factor);
    784 
    785 
    786 /**
    787  * Add relative times together.
    788  *
    789  * @param a1 some relative time
    790  * @param a2 some other relative time
    791  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
    792  */
    793 struct GNUNET_TIME_Relative
    794 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
    795                           struct GNUNET_TIME_Relative a2);
    796 
    797 
    798 /**
    799  * Subtract relative timestamp from the other.
    800  *
    801  * @param a1 first timestamp
    802  * @param a2 second timestamp
    803  * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
    804  */
    805 struct GNUNET_TIME_Relative
    806 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
    807                                struct GNUNET_TIME_Relative a2);
    808 
    809 
    810 /**
    811  * Convert relative time to network byte order.
    812  *
    813  * @param a time to convert
    814  * @return converted time value
    815  */
    816 struct GNUNET_TIME_RelativeNBO
    817 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
    818 
    819 
    820 /**
    821  * Convert relative time from network byte order.
    822  *
    823  * @param a time to convert
    824  * @return converted time value
    825  */
    826 struct GNUNET_TIME_Relative
    827 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
    828 
    829 
    830 /**
    831  * Convert absolute time to network byte order.
    832  *
    833  * @param a time to convert
    834  * @return converted time value
    835  */
    836 struct GNUNET_TIME_AbsoluteNBO
    837 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
    838 
    839 
    840 /**
    841  * Convert timestamp to network byte order.
    842  *
    843  * @param t time to convert
    844  * @return converted time value
    845  */
    846 struct GNUNET_TIME_TimestampNBO
    847 GNUNET_TIME_timestamp_hton (struct GNUNET_TIME_Timestamp t);
    848 
    849 
    850 /**
    851  * Convert milliseconds after the UNIX epoch to absolute time.
    852  *
    853  * @param ms_after_epoch millisecond timestamp to convert
    854  * @return converted time value
    855  */
    856 struct GNUNET_TIME_Absolute
    857 GNUNET_TIME_absolute_from_ms (uint64_t ms_after_epoch);
    858 
    859 
    860 /**
    861  * Test if @a abs is never.
    862  *
    863  * @return true if it is.
    864  */
    865 bool
    866 GNUNET_TIME_absolute_is_never (struct GNUNET_TIME_Absolute abs);
    867 
    868 
    869 /**
    870  * Test if @a abs is truly in the past (excluding now).
    871  *
    872  * @return true if it is.
    873  */
    874 bool
    875 GNUNET_TIME_absolute_is_past (struct GNUNET_TIME_Absolute abs);
    876 
    877 
    878 /**
    879  * Test if @a abs is truly zero.
    880  *
    881  * @return true if it is.
    882  */
    883 bool
    884 GNUNET_TIME_absolute_is_zero (struct GNUNET_TIME_Absolute abs);
    885 
    886 
    887 /**
    888  * Test if @a abs is truly in the future (excluding now).
    889  *
    890  * @return true if it is.
    891  */
    892 bool
    893 GNUNET_TIME_absolute_is_future (struct GNUNET_TIME_Absolute abs);
    894 
    895 
    896 /**
    897  * Test if @a rel is forever.
    898  *
    899  * @return true if it is.
    900  */
    901 bool
    902 GNUNET_TIME_relative_is_forever (struct GNUNET_TIME_Relative rel);
    903 
    904 
    905 /**
    906  * Test if @a rel is zero.
    907  *
    908  * @return true if it is.
    909  */
    910 bool
    911 GNUNET_TIME_relative_is_zero (struct GNUNET_TIME_Relative rel);
    912 
    913 
    914 /**
    915  * Convert seconds after the UNIX epoch to absolute time.
    916  *
    917  * @param s_after_epoch seconds after epoch to convert
    918  * @return converted time value
    919  */
    920 struct GNUNET_TIME_Absolute
    921 GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch);
    922 
    923 
    924 /**
    925  * Convert seconds after the UNIX epoch to timestamp.
    926  *
    927  * @param s_after_epoch seconds after epoch to convert
    928  * @return converted time value
    929  */
    930 struct GNUNET_TIME_Timestamp
    931 GNUNET_TIME_timestamp_from_s (uint64_t s_after_epoch);
    932 
    933 
    934 /**
    935  * Convert timestamp to number of seconds after the UNIX epoch.
    936  *
    937  * @param ts timestamp to convert
    938  * @return converted time value
    939  */
    940 uint64_t
    941 GNUNET_TIME_timestamp_to_s (struct GNUNET_TIME_Timestamp ts);
    942 
    943 
    944 /**
    945  * Convert absolute time from network byte order.
    946  *
    947  * @param a time to convert
    948  * @return converted time value
    949  */
    950 struct GNUNET_TIME_Absolute
    951 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
    952 
    953 
    954 /**
    955  * Convert timestamp from network byte order.
    956  *
    957  * @param tn time to convert
    958  * @return converted time value
    959  */
    960 struct GNUNET_TIME_Timestamp
    961 GNUNET_TIME_timestamp_ntoh (struct GNUNET_TIME_TimestampNBO tn);
    962 
    963 
    964 /**
    965  * Set the timestamp offset for this instance.
    966  *
    967  * @param offset the offset to skew the locale time by
    968  */
    969 void
    970 GNUNET_TIME_set_offset (long long offset);
    971 
    972 
    973 /**
    974  * Get the timestamp offset for this instance.
    975  *
    976  * @return the offset we currently skew the locale time by
    977  */
    978 long long
    979 GNUNET_TIME_get_offset (void);
    980 
    981 
    982 /**
    983  * Return the current year (e.g. '2011').
    984  */
    985 unsigned int
    986 GNUNET_TIME_get_current_year (void);
    987 
    988 
    989 /**
    990  * Convert a year to an expiration time of January 1st of that year.
    991  *
    992  * @param year a year (after 1970, please ;-)).
    993  * @return absolute time for January 1st of that year.
    994  */
    995 struct GNUNET_TIME_Absolute
    996 GNUNET_TIME_year_to_time (unsigned int year);
    997 
    998 
    999 /**
   1000  * Convert an expiration time to the respective year (rounds)
   1001  *
   1002  * @param at absolute time
   1003  * @return year a year (after 1970), 0 on error
   1004  */
   1005 unsigned int
   1006 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
   1007 
   1008 
   1009 /**
   1010  * A configuration object.
   1011  */
   1012 struct GNUNET_CONFIGURATION_Handle;
   1013 
   1014 
   1015 /**
   1016  * Obtain the current time and make sure it is monotonically
   1017  * increasing.  Guards against systems without an RTC or
   1018  * clocks running backwards and other nasty surprises. Does
   1019  * not guarantee that the returned time is near the current
   1020  * time returned by #GNUNET_TIME_absolute_get().  Two
   1021  * subsequent calls (within a short time period) may return the
   1022  * same value. Persists the last returned time on disk to
   1023  * ensure that time never goes backwards. As a result, the
   1024  * resulting value can be used to check if a message is the
   1025  * "most recent" value and replays of older messages (from
   1026  * the same origin) would be discarded.
   1027  *
   1028  * @param cfg configuration, used to determine where to
   1029  *   store the time; user can also insist RTC is working
   1030  *   nicely and disable the feature
   1031  * @return monotonically increasing time
   1032  */
   1033 struct GNUNET_TIME_Absolute
   1034 GNUNET_TIME_absolute_get_monotonic (
   1035   const struct GNUNET_CONFIGURATION_Handle *cfg);
   1036 
   1037 
   1038 #if 0                           /* keep Emacsens' auto-indent happy */
   1039 {
   1040 #endif
   1041 #ifdef __cplusplus
   1042 }
   1043 #endif
   1044 
   1045 /* ifndef GNUNET_TIME_LIB_H */
   1046 #endif
   1047 
   1048 /** @} */ /* end of group time */
   1049 
   1050 /** @} */ /* end of group addition */
   1051 
   1052 /* end of gnunet_time_lib.h */