timeval.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 curlx_tvnow(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 curlx_tvnow(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_GETTIMEOFDAY)
  81. struct curltime curlx_tvnow(void)
  82. {
  83. /*
  84. ** gettimeofday() is not granted to be increased monotonically, due to
  85. ** clock drifting and external source time synchronization it can jump
  86. ** forward or backward in time.
  87. */
  88. struct timeval now;
  89. struct curltime ret;
  90. (void)gettimeofday(&now, NULL);
  91. ret.tv_sec = now.tv_sec;
  92. ret.tv_usec = now.tv_usec;
  93. return ret;
  94. }
  95. #else
  96. struct curltime curlx_tvnow(void)
  97. {
  98. /*
  99. ** time() returns the value of time in seconds since the Epoch.
  100. */
  101. struct curltime now;
  102. now.tv_sec = time(NULL);
  103. now.tv_usec = 0;
  104. return now;
  105. }
  106. #endif
  107. /*
  108. * Make sure that the first argument is the more recent time, as otherwise
  109. * we'll get a weird negative time-diff back...
  110. *
  111. * Returns: the time difference in number of milliseconds. For large diffs it
  112. * returns 0x7fffffff on 32bit time_t systems.
  113. *
  114. * @unittest: 1323
  115. */
  116. time_t curlx_tvdiff(struct curltime newer, struct curltime older)
  117. {
  118. #if SIZEOF_TIME_T < 8
  119. /* for 32bit time_t systems, add a precaution to avoid overflow for really
  120. big time differences */
  121. time_t diff = newer.tv_sec-older.tv_sec;
  122. if(diff >= (0x7fffffff/1000))
  123. return 0x7fffffff;
  124. #endif
  125. return (newer.tv_sec-older.tv_sec)*1000+
  126. (int)(newer.tv_usec-older.tv_usec)/1000;
  127. }
  128. /*
  129. * Make sure that the first argument is the more recent time, as otherwise
  130. * we'll get a weird negative time-diff back...
  131. *
  132. * Returns: the time difference in number of microseconds. For too large diffs
  133. * it returns max value.
  134. */
  135. time_t Curl_tvdiff_us(struct curltime newer, struct curltime older)
  136. {
  137. time_t diff = newer.tv_sec-older.tv_sec;
  138. #if SIZEOF_TIME_T < 8
  139. /* for 32bit time_t systems */
  140. if(diff >= (0x7fffffff/1000000))
  141. return 0x7fffffff;
  142. #else
  143. /* for 64bit time_t systems */
  144. if(diff >= (0x7fffffffffffffffLL/1000000))
  145. return 0x7fffffffffffffffLL;
  146. #endif
  147. return (newer.tv_sec-older.tv_sec)*1000000+
  148. (time_t)(newer.tv_usec-older.tv_usec);
  149. }