time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /** @brief Some usefull function to work with time structs.
  2. */
  3. #include <sys/time.h>
  4. #include <time.h>
  5. #include <errno.h>
  6. #include <stdint.h>
  7. #include <assert.h>
  8. #include "faux/time.h"
  9. /** @brief Compares two time (struct timespec) values.
  10. *
  11. * @param [in] val1 First timespec struct value.
  12. * @param [in] val2 Second timespec struct value.
  13. * @return 0 if val1==val2, 1 if val1>val2, -1 if val1<val2.
  14. */
  15. int faux_timespec_cmp(const struct timespec *val1, const struct timespec *val2)
  16. {
  17. assert(val1);
  18. assert(val2);
  19. if (!val1 && !val2)
  20. return 0;
  21. if (val1 && !val2)
  22. return 1;
  23. if (!val1 && val2)
  24. return -1;
  25. if (val1->tv_sec > val2->tv_sec)
  26. return 1;
  27. if (val1->tv_sec < val2->tv_sec)
  28. return -1;
  29. // Seconds are equal
  30. if (val1->tv_nsec > val2->tv_nsec)
  31. return 1;
  32. if (val1->tv_nsec < val2->tv_nsec)
  33. return -1;
  34. // Nanoseconds are equal too
  35. return 0;
  36. }
  37. /** @brief Calculates difference between two time (struct timespec) values.
  38. *
  39. * It implements "res = val1 - val2" function.
  40. *
  41. * @param [out] res Result of operation.
  42. * @param [in] val1 First struct timespec value.
  43. * @param [in] val2 Second struct timespec value.
  44. * @return 0 - BOOL_TRUE, BOOL_FALSE on error.
  45. * @exception EINVAL Invalid arguments value.
  46. * @exception EOVERFLOW If val2>val1.
  47. */
  48. bool_t faux_timespec_diff(struct timespec *res,
  49. const struct timespec *val1, const struct timespec *val2)
  50. {
  51. assert(res);
  52. assert(val1);
  53. assert(val2);
  54. if (!res || !val1 || !val2) {
  55. errno = EINVAL;
  56. return BOOL_FALSE;
  57. }
  58. if (faux_timespec_cmp(val1, val2) < 0) {
  59. errno = EOVERFLOW;
  60. return BOOL_FALSE;
  61. }
  62. res->tv_sec = val1->tv_sec - val2->tv_sec;
  63. if (val1->tv_nsec < val2->tv_nsec) {
  64. res->tv_sec -= 1;
  65. res->tv_nsec = 1000000000l - val2->tv_nsec + val1->tv_nsec;
  66. } else {
  67. res->tv_nsec = val1->tv_nsec - val2->tv_nsec;
  68. }
  69. return BOOL_TRUE;
  70. }
  71. /** @brief Sum of two time (struct timespec) values.
  72. *
  73. * Function implements "res = val1 + val2" operation.
  74. *
  75. * @param [out] res Result of operation.
  76. * @param [in] val1 First time value.
  77. * @param [in] val2 Second time value.
  78. * @return BOOL_TRUE - success, BOOL_FALSE on error.
  79. * @exception EINVAL Invalid arguments value.
  80. */
  81. bool_t faux_timespec_sum(struct timespec *res,
  82. const struct timespec *val1, const struct timespec *val2)
  83. {
  84. assert(res);
  85. assert(val1);
  86. assert(val2);
  87. if (!res || !val1 || !val2) {
  88. errno = EINVAL;
  89. return BOOL_FALSE;
  90. }
  91. res->tv_sec = val1->tv_sec + val2->tv_sec + ((val1->tv_nsec + val2->tv_nsec) / 1000000000l);
  92. res->tv_nsec = (val1->tv_nsec + val2->tv_nsec) % 1000000000l;
  93. return BOOL_TRUE;
  94. }
  95. /** @brief Converts struct timespec value to nanoseconds.
  96. *
  97. * @param [in] ts Struct timespec to convert.
  98. * @return Number of nanoseconds or 0 if argument is invalid.
  99. */
  100. uint64_t faux_timespec_to_nsec(const struct timespec *ts)
  101. {
  102. assert(ts);
  103. if (!ts)
  104. return 0;
  105. return ((uint64_t)ts->tv_sec * 1000000000l) + (uint64_t)ts->tv_nsec;
  106. }
  107. /** @brief Converts nanoseconds to struct timespec value.
  108. *
  109. * @param [out] ts Struct timespec pointer to save result of conversion.
  110. * @param [in] nsec Time in nanoseconds to convert.
  111. */
  112. void faux_nsec_to_timespec(struct timespec *ts, uint64_t nsec)
  113. {
  114. assert(ts);
  115. if (!ts)
  116. return;
  117. ts->tv_sec = (time_t)(nsec / 1000000000l);
  118. ts->tv_nsec = (long)(nsec % 1000000000l);
  119. }
  120. /** @brief Returns current time (now).
  121. *
  122. * @param [out] now The struct timespec to save current time.
  123. * @return BOOL_TRUE - success, BOOL_FALSE on error.
  124. */
  125. bool_t faux_timespec_now(struct timespec *now)
  126. {
  127. assert(now);
  128. if (!now)
  129. return BOOL_FALSE;
  130. clock_gettime(CLOCK_REALTIME, now);
  131. return BOOL_TRUE;
  132. }
  133. /** @brief Returns current time using CLOCK_MONOTONIC (now).
  134. *
  135. * CLOCK_MONOTONIC is not ajustable by NTP and etc. But it represents
  136. * time from system up but not since Epoch.
  137. *
  138. * @param [out] now The struct timespec to save current time.
  139. * @return BOOL_TRUE success, BOOL_FALSE on error.
  140. */
  141. bool_t faux_timespec_now_monotonic(struct timespec *now)
  142. {
  143. assert(now);
  144. if (!now)
  145. return BOOL_FALSE;
  146. clock_gettime(CLOCK_MONOTONIC, now);
  147. return BOOL_TRUE;
  148. }
  149. /** @brief Indicates if specified struct timespec is before now.
  150. *
  151. * The equality to current time (now) is considered as already
  152. * coming time.
  153. *
  154. * @param [in] ts The struct timespec to compare.
  155. * @return BOOL_TRUE if timespec is before now else BOOL_FALSE.
  156. */
  157. bool_t faux_timespec_before_now(const struct timespec *ts)
  158. {
  159. struct timespec now = {};
  160. assert(ts);
  161. if (!ts)
  162. return BOOL_FALSE;
  163. faux_timespec_now(&now);
  164. if (faux_timespec_cmp(&now, ts) >= 0) // Already happend
  165. return BOOL_TRUE;
  166. return BOOL_FALSE;
  167. }