testutil.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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.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 "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "testutil.h"
  25. #include "memdebug.h"
  26. #if defined(WIN32) && !defined(MSDOS)
  27. struct timeval tutil_tvnow(void)
  28. {
  29. /*
  30. ** GetTickCount() is available on _all_ Windows versions from W95 up
  31. ** to nowadays. Returns milliseconds elapsed since last system boot,
  32. ** increases monotonically and wraps once 49.7 days have elapsed.
  33. */
  34. struct timeval now;
  35. DWORD milliseconds = GetTickCount();
  36. now.tv_sec = milliseconds / 1000;
  37. now.tv_usec = (milliseconds % 1000) * 1000;
  38. return now;
  39. }
  40. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  41. struct timeval tutil_tvnow(void)
  42. {
  43. /*
  44. ** clock_gettime() is granted to be increased monotonically when the
  45. ** monotonic clock is queried. Time starting point is unspecified, it
  46. ** could be the system start-up time, the Epoch, or something else,
  47. ** in any case the time starting point does not change once that the
  48. ** system has started up.
  49. */
  50. struct timeval now;
  51. struct timespec tsnow;
  52. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  53. now.tv_sec = tsnow.tv_sec;
  54. now.tv_usec = (int)(tsnow.tv_nsec / 1000);
  55. }
  56. /*
  57. ** Even when the configure process has truly detected monotonic clock
  58. ** availability, it might happen that it is not actually available at
  59. ** run-time. When this occurs simply fallback to other time source.
  60. */
  61. #ifdef HAVE_GETTIMEOFDAY
  62. else
  63. (void)gettimeofday(&now, NULL);
  64. #else
  65. else {
  66. now.tv_sec = time(NULL);
  67. now.tv_usec = 0;
  68. }
  69. #endif
  70. return now;
  71. }
  72. #elif defined(HAVE_GETTIMEOFDAY)
  73. struct timeval tutil_tvnow(void)
  74. {
  75. /*
  76. ** gettimeofday() is not granted to be increased monotonically, due to
  77. ** clock drifting and external source time synchronization it can jump
  78. ** forward or backward in time.
  79. */
  80. struct timeval now;
  81. (void)gettimeofday(&now, NULL);
  82. return now;
  83. }
  84. #else
  85. struct timeval tutil_tvnow(void)
  86. {
  87. /*
  88. ** time() returns the value of time in seconds since the Epoch.
  89. */
  90. struct timeval now;
  91. now.tv_sec = time(NULL);
  92. now.tv_usec = 0;
  93. return now;
  94. }
  95. #endif
  96. /*
  97. * Make sure that the first argument is the more recent time, as otherwise
  98. * we'll get a weird negative time-diff back...
  99. *
  100. * Returns: the time difference in number of milliseconds.
  101. */
  102. long tutil_tvdiff(struct timeval newer, struct timeval older)
  103. {
  104. return (long)(newer.tv_sec-older.tv_sec)*1000+
  105. (long)(newer.tv_usec-older.tv_usec)/1000;
  106. }
  107. /*
  108. * Same as tutil_tvdiff but with full usec resolution.
  109. *
  110. * Returns: the time difference in seconds with subsecond resolution.
  111. */
  112. double tutil_tvdiff_secs(struct timeval newer, struct timeval older)
  113. {
  114. if(newer.tv_sec != older.tv_sec)
  115. return (double)(newer.tv_sec-older.tv_sec)+
  116. (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  117. return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  118. }