ares__timeval.c 3.0 KB

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