timeval.c 3.7 KB

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