timeval.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "timeval.h"
  23. #if defined(WIN32) && !defined(MSDOS)
  24. struct curltime Curl_now(void)
  25. {
  26. /*
  27. ** GetTickCount() is available on _all_ Windows versions from W95 up
  28. ** to nowadays. Returns milliseconds elapsed since last system boot,
  29. ** increases monotonically and wraps once 49.7 days have elapsed.
  30. */
  31. struct curltime now;
  32. #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
  33. (_WIN32_WINNT < _WIN32_WINNT_VISTA) || \
  34. (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  35. DWORD milliseconds = GetTickCount();
  36. now.tv_sec = milliseconds / 1000;
  37. now.tv_usec = (milliseconds % 1000) * 1000;
  38. #else
  39. ULONGLONG milliseconds = GetTickCount64();
  40. now.tv_sec = (time_t) (milliseconds / 1000);
  41. now.tv_usec = (unsigned int) (milliseconds % 1000) * 1000;
  42. #endif
  43. return now;
  44. }
  45. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  46. struct curltime Curl_now(void)
  47. {
  48. /*
  49. ** clock_gettime() is granted to be increased monotonically when the
  50. ** monotonic clock is queried. Time starting point is unspecified, it
  51. ** could be the system start-up time, the Epoch, or something else,
  52. ** in any case the time starting point does not change once that the
  53. ** system has started up.
  54. */
  55. struct timeval now;
  56. struct curltime cnow;
  57. struct timespec tsnow;
  58. /*
  59. ** clock_gettime() may be defined by Apple's SDK as weak symbol thus
  60. ** code compiles but fails during run-time if clock_gettime() is
  61. ** called on unsupported OS version.
  62. */
  63. #if defined(__APPLE__) && (HAVE_BUILTIN_AVAILABLE == 1)
  64. bool have_clock_gettime = FALSE;
  65. if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *))
  66. have_clock_gettime = TRUE;
  67. #endif
  68. if(
  69. #if defined(__APPLE__) && (HAVE_BUILTIN_AVAILABLE == 1)
  70. have_clock_gettime &&
  71. #endif
  72. (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow))) {
  73. cnow.tv_sec = tsnow.tv_sec;
  74. cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
  75. }
  76. /*
  77. ** Even when the configure process has truly detected monotonic clock
  78. ** availability, it might happen that it is not actually available at
  79. ** run-time. When this occurs simply fallback to other time source.
  80. */
  81. #ifdef HAVE_GETTIMEOFDAY
  82. else {
  83. (void)gettimeofday(&now, NULL);
  84. cnow.tv_sec = now.tv_sec;
  85. cnow.tv_usec = (unsigned int)now.tv_usec;
  86. }
  87. #else
  88. else {
  89. cnow.tv_sec = time(NULL);
  90. cnow.tv_usec = 0;
  91. }
  92. #endif
  93. return cnow;
  94. }
  95. #elif defined(HAVE_MACH_ABSOLUTE_TIME)
  96. #include <stdint.h>
  97. #include <mach/mach_time.h>
  98. struct curltime Curl_now(void)
  99. {
  100. /*
  101. ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
  102. ** returns time in Mach "absolute time units," which are platform-dependent.
  103. ** To convert to nanoseconds, one must use conversion factors specified by
  104. ** mach_timebase_info().
  105. */
  106. static mach_timebase_info_data_t timebase;
  107. struct curltime cnow;
  108. uint64_t usecs;
  109. if(0 == timebase.denom)
  110. (void) mach_timebase_info(&timebase);
  111. usecs = mach_absolute_time();
  112. usecs *= timebase.numer;
  113. usecs /= timebase.denom;
  114. usecs /= 1000;
  115. cnow.tv_sec = usecs / 1000000;
  116. cnow.tv_usec = (int)(usecs % 1000000);
  117. return cnow;
  118. }
  119. #elif defined(HAVE_GETTIMEOFDAY)
  120. struct curltime Curl_now(void)
  121. {
  122. /*
  123. ** gettimeofday() is not granted to be increased monotonically, due to
  124. ** clock drifting and external source time synchronization it can jump
  125. ** forward or backward in time.
  126. */
  127. struct timeval now;
  128. struct curltime ret;
  129. (void)gettimeofday(&now, NULL);
  130. ret.tv_sec = now.tv_sec;
  131. ret.tv_usec = (int)now.tv_usec;
  132. return ret;
  133. }
  134. #else
  135. struct curltime Curl_now(void)
  136. {
  137. /*
  138. ** time() returns the value of time in seconds since the Epoch.
  139. */
  140. struct curltime now;
  141. now.tv_sec = time(NULL);
  142. now.tv_usec = 0;
  143. return now;
  144. }
  145. #endif
  146. #if SIZEOF_TIME_T < 8
  147. #define TIME_MAX INT_MAX
  148. #define TIME_MIN INT_MIN
  149. #else
  150. #define TIME_MAX 9223372036854775807LL
  151. #define TIME_MIN -9223372036854775807LL
  152. #endif
  153. /*
  154. * Returns: time difference in number of milliseconds. For too large diffs it
  155. * returns max value.
  156. *
  157. * @unittest: 1323
  158. */
  159. timediff_t Curl_timediff(struct curltime newer, struct curltime older)
  160. {
  161. timediff_t diff = newer.tv_sec-older.tv_sec;
  162. if(diff >= (TIME_MAX/1000))
  163. return TIME_MAX;
  164. else if(diff <= (TIME_MIN/1000))
  165. return TIME_MIN;
  166. return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
  167. }
  168. /*
  169. * Returns: time difference in number of microseconds. For too large diffs it
  170. * returns max value.
  171. */
  172. timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
  173. {
  174. timediff_t diff = newer.tv_sec-older.tv_sec;
  175. if(diff >= (TIME_MAX/1000000))
  176. return TIME_MAX;
  177. else if(diff <= (TIME_MIN/1000000))
  178. return TIME_MIN;
  179. return diff * 1000000 + newer.tv_usec-older.tv_usec;
  180. }