gnunet_time_lib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. *
  19. * @file
  20. * Functions related to time
  21. *
  22. * @defgroup time Time library
  23. * Time and time calculations.
  24. * @{
  25. */
  26. #ifndef GNUNET_TIME_LIB_H
  27. #define GNUNET_TIME_LIB_H
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #if 0 /* keep Emacsens' auto-indent happy */
  32. }
  33. #endif
  34. #endif
  35. #include "gnunet_common.h"
  36. /**
  37. * Time for absolute times used by GNUnet, in microseconds.
  38. */
  39. struct GNUNET_TIME_Absolute
  40. {
  41. /**
  42. * The actual value.
  43. */
  44. uint64_t abs_value_us;
  45. };
  46. /**
  47. * Time for relative time used by GNUnet, in microseconds.
  48. * Always positive, so we can only refer to future time.
  49. */
  50. struct GNUNET_TIME_Relative
  51. {
  52. /**
  53. * The actual value.
  54. */
  55. uint64_t rel_value_us;
  56. };
  57. GNUNET_NETWORK_STRUCT_BEGIN
  58. /**
  59. * Time for relative time used by GNUnet, in microseconds and in network byte order.
  60. */
  61. struct GNUNET_TIME_RelativeNBO
  62. {
  63. /**
  64. * The actual value (in network byte order).
  65. */
  66. uint64_t rel_value_us__ GNUNET_PACKED;
  67. };
  68. /**
  69. * Time for absolute time used by GNUnet, in microseconds and in network byte order.
  70. */
  71. struct GNUNET_TIME_AbsoluteNBO
  72. {
  73. /**
  74. * The actual value (in network byte order).
  75. */
  76. uint64_t abs_value_us__ GNUNET_PACKED;
  77. };
  78. GNUNET_NETWORK_STRUCT_END
  79. /**
  80. * Relative time zero.
  81. */
  82. #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero_ ()
  83. /**
  84. * Absolute time zero.
  85. */
  86. #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_ ()
  87. /**
  88. * One microsecond, our basic time unit.
  89. */
  90. #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_ ()
  91. /**
  92. * One millisecond.
  93. */
  94. #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_ ()
  95. /**
  96. * One second.
  97. */
  98. #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_ ()
  99. /**
  100. * One minute.
  101. */
  102. #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_ ()
  103. /**
  104. * One hour.
  105. */
  106. #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_ ()
  107. /**
  108. * One day.
  109. */
  110. #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply ( \
  111. GNUNET_TIME_UNIT_HOURS, 24)
  112. /**
  113. * One week.
  114. */
  115. #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply ( \
  116. GNUNET_TIME_UNIT_DAYS, 7)
  117. /**
  118. * One month (30 days).
  119. */
  120. #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply ( \
  121. GNUNET_TIME_UNIT_DAYS, 30)
  122. /**
  123. * One year (365 days).
  124. */
  125. #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply ( \
  126. GNUNET_TIME_UNIT_DAYS, 365)
  127. /**
  128. * Constant used to specify "forever". This constant
  129. * will be treated specially in all time operations.
  130. */
  131. #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
  132. /**
  133. * Constant used to specify "forever". This constant
  134. * will be treated specially in all time operations.
  135. */
  136. #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
  137. /**
  138. * Threshold after which exponential backoff should not increase (15 m).
  139. */
  140. #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD \
  141. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  142. /**
  143. * Perform our standard exponential back-off calculation, starting at 1 ms
  144. * and then going by a factor of 2 up unto a maximum of 15 m.
  145. *
  146. * @param r current backoff time, initially zero
  147. */
  148. #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min ( \
  149. GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
  150. GNUNET_TIME_relative_multiply ( \
  151. GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
  152. /**
  153. * Randomized exponential back-off, starting at 1 ms
  154. * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
  155. * to a maximum of the given threshold.
  156. *
  157. * @param rt current backoff time, initially zero
  158. * @param threshold maximum value for backoff
  159. * @return the next backoff time
  160. */
  161. struct GNUNET_TIME_Relative
  162. GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt, struct
  163. GNUNET_TIME_Relative threshold);
  164. /**
  165. * Return a random time value between 0.5*r and 1.5*r.
  166. *
  167. * @param r input time for scaling
  168. * @return randomized time
  169. */
  170. struct GNUNET_TIME_Relative
  171. GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r);
  172. /**
  173. * Return relative time of 0ms.
  174. */
  175. struct GNUNET_TIME_Relative
  176. GNUNET_TIME_relative_get_zero_ (void);
  177. /**
  178. * Return absolute time of 0ms.
  179. */
  180. struct GNUNET_TIME_Absolute
  181. GNUNET_TIME_absolute_get_zero_ (void);
  182. /**
  183. * Return relative time of 1 microsecond.
  184. */
  185. struct GNUNET_TIME_Relative
  186. GNUNET_TIME_relative_get_unit_ (void);
  187. /**
  188. * Return relative time of 1ms.
  189. */
  190. struct GNUNET_TIME_Relative
  191. GNUNET_TIME_relative_get_millisecond_ (void);
  192. /**
  193. * Return relative time of 1s.
  194. */
  195. struct GNUNET_TIME_Relative
  196. GNUNET_TIME_relative_get_second_ (void);
  197. /**
  198. * Return relative time of 1 minute.
  199. */
  200. struct GNUNET_TIME_Relative
  201. GNUNET_TIME_relative_get_minute_ (void);
  202. /**
  203. * Return relative time of 1 hour.
  204. */
  205. struct GNUNET_TIME_Relative
  206. GNUNET_TIME_relative_get_hour_ (void);
  207. /**
  208. * Return "forever".
  209. */
  210. struct GNUNET_TIME_Relative
  211. GNUNET_TIME_relative_get_forever_ (void);
  212. /**
  213. * Return "forever".
  214. */
  215. struct GNUNET_TIME_Absolute
  216. GNUNET_TIME_absolute_get_forever_ (void);
  217. /**
  218. * Get the current time.
  219. *
  220. * @return the current time
  221. */
  222. struct GNUNET_TIME_Absolute
  223. GNUNET_TIME_absolute_get (void);
  224. /**
  225. * Convert relative time to an absolute time in the
  226. * future.
  227. *
  228. * @param rel relative time to convert
  229. * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
  230. */
  231. struct GNUNET_TIME_Absolute
  232. GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
  233. /**
  234. * Round a time value so that it is suitable for transmission
  235. * via JSON encodings.
  236. *
  237. * @param at time to round
  238. * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
  239. * it was just now rounded
  240. */
  241. int
  242. GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at);
  243. /**
  244. * Round a time value so that it is suitable for transmission
  245. * via JSON encodings.
  246. *
  247. * @param rt time to round
  248. * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
  249. * it was just now rounded
  250. */
  251. int
  252. GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt);
  253. /**
  254. * Return the minimum of two relative time values.
  255. *
  256. * @param t1 first timestamp
  257. * @param t2 other timestamp
  258. * @return timestamp that is smaller
  259. */
  260. struct GNUNET_TIME_Relative
  261. GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
  262. struct GNUNET_TIME_Relative t2);
  263. /**
  264. * Return the maximum of two relative time values.
  265. *
  266. * @param t1 first timestamp
  267. * @param t2 other timestamp
  268. * @return timestamp that is larger
  269. */
  270. struct GNUNET_TIME_Relative
  271. GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
  272. struct GNUNET_TIME_Relative t2);
  273. /**
  274. * Return the minimum of two absolute time values.
  275. *
  276. * @param t1 first timestamp
  277. * @param t2 other timestamp
  278. * @return timestamp that is smaller
  279. */
  280. struct GNUNET_TIME_Absolute
  281. GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
  282. struct GNUNET_TIME_Absolute t2);
  283. /**
  284. * Return the maximum of two absolute time values.
  285. *
  286. * @param t1 first timestamp
  287. * @param t2 other timestamp
  288. * @return timestamp that is smaller
  289. */
  290. struct GNUNET_TIME_Absolute
  291. GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
  292. struct GNUNET_TIME_Absolute t2);
  293. /**
  294. * Given a timestamp in the future, how much time
  295. * remains until then?
  296. *
  297. * @param future some absolute time, typically in the future
  298. * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
  299. */
  300. struct GNUNET_TIME_Relative
  301. GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
  302. /**
  303. * Calculate the estimate time of arrival/completion
  304. * for an operation.
  305. *
  306. * @param start when did the operation start?
  307. * @param finished how much has been done?
  308. * @param total how much must be done overall (same unit as for "finished")
  309. * @return remaining duration for the operation,
  310. * assuming it continues at the same speed
  311. */
  312. struct GNUNET_TIME_Relative
  313. GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
  314. uint64_t finished,
  315. uint64_t total);
  316. /**
  317. * Compute the time difference between the given start and end times.
  318. * Use this function instead of actual subtraction to ensure that
  319. * "FOREVER" and overflows are handled correctly.
  320. *
  321. * @param start some absolute time
  322. * @param end some absolute time (typically larger or equal to start)
  323. * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
  324. */
  325. struct GNUNET_TIME_Relative
  326. GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
  327. struct GNUNET_TIME_Absolute end);
  328. /**
  329. * Get the duration of an operation as the
  330. * difference of the current time and the given start time "hence".
  331. *
  332. * @param whence some absolute time, typically in the past
  333. * @return 0 if hence > now, otherwise now-hence.
  334. */
  335. struct GNUNET_TIME_Relative
  336. GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
  337. /**
  338. * Add a given relative duration to the
  339. * given start time.
  340. *
  341. * @param start some absolute time
  342. * @param duration some relative time to add
  343. * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
  344. */
  345. struct GNUNET_TIME_Absolute
  346. GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
  347. struct GNUNET_TIME_Relative duration);
  348. /**
  349. * Subtract a given relative duration from the
  350. * given start time.
  351. *
  352. * @param start some absolute time
  353. * @param duration some relative time to subtract
  354. * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
  355. */
  356. struct GNUNET_TIME_Absolute
  357. GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
  358. struct GNUNET_TIME_Relative duration);
  359. /**
  360. * Multiply relative time by a given factor.
  361. *
  362. * @param rel some duration
  363. * @param factor integer to multiply with
  364. * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
  365. */
  366. struct GNUNET_TIME_Relative
  367. GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
  368. unsigned long long factor);
  369. /**
  370. * Saturating multiply relative time by a given factor.
  371. *
  372. * @param rel some duration
  373. * @param factor integer to multiply with
  374. * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
  375. */
  376. struct GNUNET_TIME_Relative
  377. GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
  378. unsigned long long factor);
  379. /**
  380. * Divide relative time by a given factor.
  381. *
  382. * @param rel some duration
  383. * @param factor integer to divide by
  384. * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
  385. */
  386. struct GNUNET_TIME_Relative
  387. GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
  388. unsigned long long factor);
  389. /**
  390. * Add relative times together.
  391. *
  392. * @param a1 some relative time
  393. * @param a2 some other relative time
  394. * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
  395. */
  396. struct GNUNET_TIME_Relative
  397. GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
  398. struct GNUNET_TIME_Relative a2);
  399. /**
  400. * Subtract relative timestamp from the other.
  401. *
  402. * @param a1 first timestamp
  403. * @param a2 second timestamp
  404. * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
  405. */
  406. struct GNUNET_TIME_Relative
  407. GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
  408. struct GNUNET_TIME_Relative a2);
  409. /**
  410. * Convert relative time to network byte order.
  411. *
  412. * @param a time to convert
  413. * @return converted time value
  414. */
  415. struct GNUNET_TIME_RelativeNBO
  416. GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
  417. /**
  418. * Convert relative time from network byte order.
  419. *
  420. * @param a time to convert
  421. * @return converted time value
  422. */
  423. struct GNUNET_TIME_Relative
  424. GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
  425. /**
  426. * Convert absolute time to network byte order.
  427. *
  428. * @param a time to convert
  429. * @return converted time value
  430. */
  431. struct GNUNET_TIME_AbsoluteNBO
  432. GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
  433. /**
  434. * Convert absolute time from network byte order.
  435. *
  436. * @param a time to convert
  437. * @return converted time value
  438. */
  439. struct GNUNET_TIME_Absolute
  440. GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
  441. /**
  442. * Set the timestamp offset for this instance.
  443. *
  444. * @param offset the offset to skew the locale time by
  445. */
  446. void
  447. GNUNET_TIME_set_offset (long long offset);
  448. /**
  449. * Get the timestamp offset for this instance.
  450. *
  451. * @return the offset we currently skew the locale time by
  452. */
  453. long long
  454. GNUNET_TIME_get_offset (void);
  455. /**
  456. * Return the current year (i.e. '2011').
  457. */
  458. unsigned int
  459. GNUNET_TIME_get_current_year (void);
  460. /**
  461. * Convert a year to an expiration time of January 1st of that year.
  462. *
  463. * @param year a year (after 1970, please ;-)).
  464. * @return absolute time for January 1st of that year.
  465. */
  466. struct GNUNET_TIME_Absolute
  467. GNUNET_TIME_year_to_time (unsigned int year);
  468. /**
  469. * Convert an expiration time to the respective year (rounds)
  470. *
  471. * @param at absolute time
  472. * @return year a year (after 1970), 0 on error
  473. */
  474. unsigned int
  475. GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
  476. /**
  477. * A configuration object.
  478. */
  479. struct GNUNET_CONFIGURATION_Handle;
  480. /**
  481. * Obtain the current time and make sure it is monotonically
  482. * increasing. Guards against systems without an RTC or
  483. * clocks running backwards and other nasty surprises. Does
  484. * not guarantee that the returned time is near the current
  485. * time returned by #GNUNET_TIME_absolute_get(). Two
  486. * subsequent calls (within a short time period) may return the
  487. * same value. Persists the last returned time on disk to
  488. * ensure that time never goes backwards. As a result, the
  489. * resulting value can be used to check if a message is the
  490. * "most recent" value and replays of older messages (from
  491. * the same origin) would be discarded.
  492. *
  493. * @param cfg configuration, used to determine where to
  494. * store the time; user can also insist RTC is working
  495. * nicely and disable the feature
  496. * @return monotonically increasing time
  497. */
  498. struct GNUNET_TIME_Absolute
  499. GNUNET_TIME_absolute_get_monotonic (const struct
  500. GNUNET_CONFIGURATION_Handle *cfg);
  501. #if 0 /* keep Emacsens' auto-indent happy */
  502. {
  503. #endif
  504. #ifdef __cplusplus
  505. }
  506. #endif
  507. /* ifndef GNUNET_TIME_LIB_H */
  508. #endif
  509. /** @} */ /* end of group time */
  510. /* end of gnunet_time_lib.h */