testutil.c 3.9 KB

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