timeval.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "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. DWORD milliseconds = GetTickCount();
  33. now.tv_sec = milliseconds / 1000;
  34. now.tv_usec = (milliseconds % 1000) * 1000;
  35. return now;
  36. }
  37. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  38. struct timeval curlx_tvnow(void)
  39. {
  40. /*
  41. ** clock_gettime() is granted to be increased monotonically when the
  42. ** monotonic clock is queried. Time starting point is unspecified, it
  43. ** could be the system start-up time, the Epoch, or something else,
  44. ** in any case the time starting point does not change once that the
  45. ** system has started up.
  46. */
  47. struct timeval now;
  48. struct timespec tsnow;
  49. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  50. now.tv_sec = tsnow.tv_sec;
  51. now.tv_usec = tsnow.tv_nsec / 1000;
  52. }
  53. /*
  54. ** Even when the configure process has truly detected monotonic clock
  55. ** availability, it might happen that it is not actually available at
  56. ** run-time. When this occurs simply fallback to other time source.
  57. */
  58. #ifdef HAVE_GETTIMEOFDAY
  59. else
  60. (void)gettimeofday(&now, NULL);
  61. #else
  62. else {
  63. now.tv_sec = (long)time(NULL);
  64. now.tv_usec = 0;
  65. }
  66. #endif
  67. return now;
  68. }
  69. #elif defined(HAVE_GETTIMEOFDAY)
  70. struct timeval curlx_tvnow(void)
  71. {
  72. /*
  73. ** gettimeofday() is not granted to be increased monotonically, due to
  74. ** clock drifting and external source time synchronization it can jump
  75. ** forward or backward in time.
  76. */
  77. struct timeval now;
  78. (void)gettimeofday(&now, NULL);
  79. return now;
  80. }
  81. #else
  82. struct timeval curlx_tvnow(void)
  83. {
  84. /*
  85. ** time() returns the value of time in seconds since the Epoch.
  86. */
  87. struct timeval now;
  88. now.tv_sec = (long)time(NULL);
  89. now.tv_usec = 0;
  90. return now;
  91. }
  92. #endif
  93. /*
  94. * Make sure that the first argument is the more recent time, as otherwise
  95. * we'll get a weird negative time-diff back...
  96. *
  97. * Returns: the time difference in number of milliseconds.
  98. */
  99. long curlx_tvdiff(struct timeval newer, struct timeval older)
  100. {
  101. return (newer.tv_sec-older.tv_sec)*1000+
  102. (newer.tv_usec-older.tv_usec)/1000;
  103. }
  104. /*
  105. * Same as curlx_tvdiff but with full usec resolution.
  106. *
  107. * Returns: the time difference in seconds with subsecond resolution.
  108. */
  109. double curlx_tvdiff_secs(struct timeval newer, struct timeval older)
  110. {
  111. return (double)(newer.tv_sec-older.tv_sec)+
  112. (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  113. }
  114. /* return the number of seconds in the given input timeval struct */
  115. long Curl_tvlong(struct timeval t1)
  116. {
  117. return t1.tv_sec;
  118. }