time.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Time-related functions and macros
  20. */
  21. #ifndef _ASTERISK_TIME_H
  22. #define _ASTERISK_TIME_H
  23. #ifdef HAVE_SYS_TIME_H
  24. #include <sys/time.h>
  25. #endif
  26. #include "asterisk/inline_api.h"
  27. /* We have to let the compiler learn what types to use for the elements of a
  28. struct timeval since on linux, it's time_t and suseconds_t, but on *BSD,
  29. they are just a long.
  30. note:dummy_tv_var_for_types never actually gets exported, only used as
  31. local place holder. */
  32. extern struct timeval dummy_tv_var_for_types;
  33. typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t;
  34. typedef typeof(dummy_tv_var_for_types.tv_usec) ast_suseconds_t;
  35. /*!
  36. * \brief Computes the difference (in seconds) between two \c struct \c timeval instances.
  37. * \param end the end of the time period
  38. * \param start the beginning of the time period
  39. * \return the difference in seconds
  40. */
  41. AST_INLINE_API(
  42. int64_t ast_tvdiff_sec(struct timeval end, struct timeval start),
  43. {
  44. int64_t result = end.tv_sec - start.tv_sec;
  45. if (result > 0 && end.tv_usec < start.tv_usec)
  46. result--;
  47. else if (result < 0 && end.tv_usec > start.tv_usec)
  48. result++;
  49. return result;
  50. }
  51. )
  52. /*!
  53. * \brief Computes the difference (in microseconds) between two \c struct \c timeval instances.
  54. * \param end the end of the time period
  55. * \param start the beginning of the time period
  56. * \return the difference in microseconds
  57. */
  58. AST_INLINE_API(
  59. int64_t ast_tvdiff_us(struct timeval end, struct timeval start),
  60. {
  61. return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
  62. end.tv_usec - start.tv_usec;
  63. }
  64. )
  65. /*!
  66. * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
  67. * \param end end of the time period
  68. * \param start beginning of the time period
  69. * \return the difference in milliseconds
  70. */
  71. AST_INLINE_API(
  72. int64_t ast_tvdiff_ms(struct timeval end, struct timeval start),
  73. {
  74. /* the offset by 1,000,000 below is intentional...
  75. it avoids differences in the way that division
  76. is handled for positive and negative numbers, by ensuring
  77. that the divisor is always positive
  78. */
  79. int64_t sec_dif = (int64_t)(end.tv_sec - start.tv_sec) * 1000;
  80. int64_t usec_dif = (1000000 + end.tv_usec - start.tv_usec) / 1000 - 1000;
  81. return sec_dif + usec_dif;
  82. }
  83. )
  84. /*!
  85. * \brief Returns true if the argument is 0,0
  86. */
  87. AST_INLINE_API(
  88. int ast_tvzero(const struct timeval t),
  89. {
  90. return (t.tv_sec == 0 && t.tv_usec == 0);
  91. }
  92. )
  93. /*!
  94. * \brief Compres two \c struct \c timeval instances returning
  95. * -1, 0, 1 if the first arg is smaller, equal or greater to the second.
  96. */
  97. AST_INLINE_API(
  98. int ast_tvcmp(struct timeval _a, struct timeval _b),
  99. {
  100. if (_a.tv_sec < _b.tv_sec)
  101. return -1;
  102. if (_a.tv_sec > _b.tv_sec)
  103. return 1;
  104. /* now seconds are equal */
  105. if (_a.tv_usec < _b.tv_usec)
  106. return -1;
  107. if (_a.tv_usec > _b.tv_usec)
  108. return 1;
  109. return 0;
  110. }
  111. )
  112. /*!
  113. * \brief Returns true if the two \c struct \c timeval arguments are equal.
  114. */
  115. AST_INLINE_API(
  116. int ast_tveq(struct timeval _a, struct timeval _b),
  117. {
  118. return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
  119. }
  120. )
  121. /*!
  122. * \brief Returns current timeval. Meant to replace calls to gettimeofday().
  123. */
  124. AST_INLINE_API(
  125. struct timeval ast_tvnow(void),
  126. {
  127. struct timeval t;
  128. gettimeofday(&t, NULL);
  129. return t;
  130. }
  131. )
  132. /*!
  133. * \brief Returns the sum of two timevals a + b
  134. */
  135. struct timeval ast_tvadd(struct timeval a, struct timeval b);
  136. /*!
  137. * \brief Returns the difference of two timevals a - b
  138. */
  139. struct timeval ast_tvsub(struct timeval a, struct timeval b);
  140. /*!
  141. * \since 12
  142. * \brief Formats a duration into HH:MM:SS
  143. *
  144. * \param duration The time (in seconds) to format
  145. * \param buf A buffer to hold the formatted string'
  146. * \param length The size of the buffer
  147. */
  148. void ast_format_duration_hh_mm_ss(int duration, char *buf, size_t length);
  149. /*!
  150. * \brief Calculate remaining milliseconds given a starting timestamp
  151. * and upper bound
  152. *
  153. * If the upper bound is negative, then this indicates that there is no
  154. * upper bound on the amount of time to wait. This will result in a
  155. * negative return.
  156. *
  157. * \param start When timing started being calculated
  158. * \param max_ms The maximum number of milliseconds to wait from start. May be negative.
  159. * \return The number of milliseconds left to wait for. May be negative.
  160. */
  161. int ast_remaining_ms(struct timeval start, int max_ms);
  162. /*!
  163. * \brief Returns a timeval from sec, usec
  164. */
  165. AST_INLINE_API(
  166. struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
  167. {
  168. struct timeval t;
  169. t.tv_sec = sec;
  170. t.tv_usec = usec;
  171. return t;
  172. }
  173. )
  174. /*!
  175. * \brief Returns a timeval corresponding to the duration of n samples at rate r.
  176. * Useful to convert samples to timevals, or even milliseconds to timevals
  177. * in the form ast_samp2tv(milliseconds, 1000)
  178. */
  179. AST_INLINE_API(
  180. struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
  181. {
  182. return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / (float) _rate));
  183. }
  184. )
  185. #endif /* _ASTERISK_TIME_H */