timeval.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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 timeval 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 timeval 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 = (long) (milliseconds / 1000);
  40. now.tv_usec = (long) (milliseconds % 1000) * 1000;
  41. #endif
  42. return now;
  43. }
  44. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  45. struct timeval 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 timespec tsnow;
  56. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  57. now.tv_sec = tsnow.tv_sec;
  58. now.tv_usec = tsnow.tv_nsec / 1000;
  59. }
  60. /*
  61. ** Even when the configure process has truly detected monotonic clock
  62. ** availability, it might happen that it is not actually available at
  63. ** run-time. When this occurs simply fallback to other time source.
  64. */
  65. #ifdef HAVE_GETTIMEOFDAY
  66. else
  67. (void)gettimeofday(&now, NULL);
  68. #else
  69. else {
  70. now.tv_sec = (long)time(NULL);
  71. now.tv_usec = 0;
  72. }
  73. #endif
  74. return now;
  75. }
  76. #elif defined(HAVE_GETTIMEOFDAY)
  77. struct timeval curlx_tvnow(void)
  78. {
  79. /*
  80. ** gettimeofday() is not granted to be increased monotonically, due to
  81. ** clock drifting and external source time synchronization it can jump
  82. ** forward or backward in time.
  83. */
  84. struct timeval now;
  85. (void)gettimeofday(&now, NULL);
  86. return now;
  87. }
  88. #else
  89. struct timeval curlx_tvnow(void)
  90. {
  91. /*
  92. ** time() returns the value of time in seconds since the Epoch.
  93. */
  94. struct timeval now;
  95. now.tv_sec = (long)time(NULL);
  96. now.tv_usec = 0;
  97. return now;
  98. }
  99. #endif
  100. /*
  101. * Make sure that the first argument is the more recent time, as otherwise
  102. * we'll get a weird negative time-diff back...
  103. *
  104. * Returns: the time difference in number of milliseconds. For large diffs it
  105. * returns 0x7fffffff on 32bit time_t systems.
  106. */
  107. time_t curlx_tvdiff(struct timeval newer, struct timeval older)
  108. {
  109. #if SIZEOF_TIME_T < 8
  110. /* for 32bit time_t systems, add a precaution to avoid overflow for really
  111. big time differences */
  112. time_t diff = newer.tv_sec-older.tv_sec;
  113. if(diff >= (0x7fffffff/1000))
  114. return 0x7fffffff;
  115. #endif
  116. return (newer.tv_sec-older.tv_sec)*1000+
  117. (time_t)(newer.tv_usec-older.tv_usec)/1000;
  118. }
  119. /*
  120. * Same as curlx_tvdiff but with full usec resolution.
  121. *
  122. * Returns: the time difference in seconds with subsecond resolution.
  123. */
  124. double curlx_tvdiff_secs(struct timeval newer, struct timeval older)
  125. {
  126. if(newer.tv_sec != older.tv_sec)
  127. return (double)(newer.tv_sec-older.tv_sec)+
  128. (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  129. else
  130. return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  131. }
  132. /* return the number of seconds in the given input timeval struct */
  133. time_t Curl_tvlong(struct timeval t1)
  134. {
  135. return t1.tv_sec;
  136. }