timeval.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, 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. DWORD milliseconds = GetTickCount();
  35. now.tv_sec = milliseconds / 1000;
  36. now.tv_usec = (milliseconds % 1000) * 1000;
  37. #else
  38. ULONGLONG milliseconds = GetTickCount64();
  39. now.tv_sec = (time_t) (milliseconds / 1000);
  40. now.tv_usec = (unsigned int) (milliseconds % 1000) * 1000;
  41. #endif
  42. return now;
  43. }
  44. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  45. struct curltime Curl_now(void)
  46. {
  47. /*
  48. ** clock_gettime() is granted to be increased monotonically when the
  49. ** monotonic clock is queried. Time starting point is unspecified, it
  50. ** could be the system start-up time, the Epoch, or something else,
  51. ** in any case the time starting point does not change once that the
  52. ** system has started up.
  53. */
  54. struct timeval now;
  55. struct curltime cnow;
  56. struct timespec tsnow;
  57. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  58. cnow.tv_sec = tsnow.tv_sec;
  59. cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
  60. }
  61. /*
  62. ** Even when the configure process has truly detected monotonic clock
  63. ** availability, it might happen that it is not actually available at
  64. ** run-time. When this occurs simply fallback to other time source.
  65. */
  66. #ifdef HAVE_GETTIMEOFDAY
  67. else {
  68. (void)gettimeofday(&now, NULL);
  69. cnow.tv_sec = now.tv_sec;
  70. cnow.tv_usec = (unsigned int)now.tv_usec;
  71. }
  72. #else
  73. else {
  74. cnow.tv_sec = time(NULL);
  75. cnow.tv_usec = 0;
  76. }
  77. #endif
  78. return cnow;
  79. }
  80. #elif defined(HAVE_MACH_ABSOLUTE_TIME)
  81. #include <stdint.h>
  82. #include <mach/mach_time.h>
  83. struct curltime Curl_now(void)
  84. {
  85. /*
  86. ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
  87. ** returns time in Mach "absolute time units," which are platform-dependent.
  88. ** To convert to nanoseconds, one must use conversion factors specified by
  89. ** mach_timebase_info().
  90. */
  91. static mach_timebase_info_data_t timebase;
  92. struct curltime cnow;
  93. uint64_t usecs;
  94. if(0 == timebase.denom)
  95. (void) mach_timebase_info(&timebase);
  96. usecs = mach_absolute_time();
  97. usecs *= timebase.numer;
  98. usecs /= timebase.denom;
  99. usecs /= 1000;
  100. cnow.tv_sec = usecs / 1000000;
  101. cnow.tv_usec = (int)(usecs % 1000000);
  102. return cnow;
  103. }
  104. #elif defined(HAVE_GETTIMEOFDAY)
  105. struct curltime Curl_now(void)
  106. {
  107. /*
  108. ** gettimeofday() is not granted to be increased monotonically, due to
  109. ** clock drifting and external source time synchronization it can jump
  110. ** forward or backward in time.
  111. */
  112. struct timeval now;
  113. struct curltime ret;
  114. (void)gettimeofday(&now, NULL);
  115. ret.tv_sec = now.tv_sec;
  116. ret.tv_usec = (int)now.tv_usec;
  117. return ret;
  118. }
  119. #else
  120. struct curltime Curl_now(void)
  121. {
  122. /*
  123. ** time() returns the value of time in seconds since the Epoch.
  124. */
  125. struct curltime now;
  126. now.tv_sec = time(NULL);
  127. now.tv_usec = 0;
  128. return now;
  129. }
  130. #endif
  131. #if SIZEOF_TIME_T < 8
  132. #define TIME_MAX INT_MAX
  133. #define TIME_MIN INT_MIN
  134. #else
  135. #define TIME_MAX 9223372036854775807LL
  136. #define TIME_MIN -9223372036854775807LL
  137. #endif
  138. /*
  139. * Returns: time difference in number of milliseconds. For too large diffs it
  140. * returns max value.
  141. *
  142. * @unittest: 1323
  143. */
  144. timediff_t Curl_timediff(struct curltime newer, struct curltime older)
  145. {
  146. timediff_t diff = newer.tv_sec-older.tv_sec;
  147. if(diff >= (TIME_MAX/1000))
  148. return TIME_MAX;
  149. else if(diff <= (TIME_MIN/1000))
  150. return TIME_MIN;
  151. return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
  152. }
  153. /*
  154. * Returns: time difference in number of microseconds. For too large diffs it
  155. * returns max value.
  156. */
  157. timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
  158. {
  159. timediff_t diff = newer.tv_sec-older.tv_sec;
  160. if(diff >= (TIME_MAX/1000000))
  161. return TIME_MAX;
  162. else if(diff <= (TIME_MIN/1000000))
  163. return TIME_MIN;
  164. return diff * 1000000 + newer.tv_usec-older.tv_usec;
  165. }