ares__timeval.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2008 by Daniel Stenberg et al
  2. *
  3. * Permission to use, copy, modify, and distribute this software and its
  4. * documentation for any purpose and without fee is hereby granted, provided
  5. * that the above copyright notice appear in all copies and that both that
  6. * copyright notice and this permission notice appear in supporting
  7. * documentation, and that the name of M.I.T. not be used in advertising or
  8. * publicity pertaining to distribution of the software without specific,
  9. * written prior permission. M.I.T. makes no representations about the
  10. * suitability of this software for any purpose. It is provided "as is"
  11. * without express or implied warranty.
  12. */
  13. #include "ares_setup.h"
  14. #include "ares.h"
  15. #include "ares_private.h"
  16. #if defined(WIN32) && !defined(MSDOS)
  17. struct timeval ares__tvnow(void)
  18. {
  19. /*
  20. ** GetTickCount() is available on _all_ Windows versions from W95 up
  21. ** to nowadays. Returns milliseconds elapsed since last system boot,
  22. ** increases monotonically and wraps once 49.7 days have elapsed.
  23. */
  24. struct timeval now;
  25. DWORD milliseconds = GetTickCount();
  26. now.tv_sec = milliseconds / 1000;
  27. now.tv_usec = (milliseconds % 1000) * 1000;
  28. return now;
  29. }
  30. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  31. struct timeval ares__tvnow(void)
  32. {
  33. /*
  34. ** clock_gettime() is granted to be increased monotonically when the
  35. ** monotonic clock is queried. Time starting point is unspecified, it
  36. ** could be the system start-up time, the Epoch, or something else,
  37. ** in any case the time starting point does not change once that the
  38. ** system has started up.
  39. */
  40. struct timeval now;
  41. struct timespec tsnow;
  42. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  43. now.tv_sec = tsnow.tv_sec;
  44. now.tv_usec = tsnow.tv_nsec / 1000;
  45. }
  46. /*
  47. ** Even when the configure process has truly detected monotonic clock
  48. ** availability, it might happen that it is not actually available at
  49. ** run-time. When this occurs simply fallback to other time source.
  50. */
  51. #ifdef HAVE_GETTIMEOFDAY
  52. else
  53. (void)gettimeofday(&now, NULL); /* LCOV_EXCL_LINE */
  54. #else
  55. else {
  56. now.tv_sec = (long)time(NULL);
  57. now.tv_usec = 0;
  58. }
  59. #endif
  60. return now;
  61. }
  62. #elif defined(HAVE_GETTIMEOFDAY)
  63. struct timeval ares__tvnow(void)
  64. {
  65. /*
  66. ** gettimeofday() is not granted to be increased monotonically, due to
  67. ** clock drifting and external source time synchronization it can jump
  68. ** forward or backward in time.
  69. */
  70. struct timeval now;
  71. (void)gettimeofday(&now, NULL);
  72. return now;
  73. }
  74. #else
  75. struct timeval ares__tvnow(void)
  76. {
  77. /*
  78. ** time() returns the value of time in seconds since the Epoch.
  79. */
  80. struct timeval now;
  81. now.tv_sec = (long)time(NULL);
  82. now.tv_usec = 0;
  83. return now;
  84. }
  85. #endif
  86. #if 0 /* Not used */
  87. /*
  88. * Make sure that the first argument is the more recent time, as otherwise
  89. * we'll get a weird negative time-diff back...
  90. *
  91. * Returns: the time difference in number of milliseconds.
  92. */
  93. long ares__tvdiff(struct timeval newer, struct timeval older)
  94. {
  95. return (newer.tv_sec-older.tv_sec)*1000+
  96. (newer.tv_usec-older.tv_usec)/1000;
  97. }
  98. #endif