timeval.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2005, 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. #ifndef HAVE_GETTIMEOFDAY
  25. #ifdef WIN32
  26. #include <mmsystem.h>
  27. static int gettimeofday(struct timeval *tp, void *nothing)
  28. {
  29. #ifdef WITHOUT_MM_LIB
  30. SYSTEMTIME st;
  31. time_t tt;
  32. struct tm tmtm;
  33. /* mktime converts local to UTC */
  34. GetLocalTime (&st);
  35. tmtm.tm_sec = st.wSecond;
  36. tmtm.tm_min = st.wMinute;
  37. tmtm.tm_hour = st.wHour;
  38. tmtm.tm_mday = st.wDay;
  39. tmtm.tm_mon = st.wMonth - 1;
  40. tmtm.tm_year = st.wYear - 1900;
  41. tmtm.tm_isdst = -1;
  42. tt = mktime (&tmtm);
  43. tp->tv_sec = tt;
  44. tp->tv_usec = st.wMilliseconds * 1000;
  45. #else
  46. /**
  47. ** The earlier time calculations using GetLocalTime
  48. ** had a time resolution of 10ms.The timeGetTime, part
  49. ** of multimedia apis offer a better time resolution
  50. ** of 1ms.Need to link against winmm.lib for this
  51. **/
  52. unsigned long Ticks = 0;
  53. unsigned long Sec =0;
  54. unsigned long Usec = 0;
  55. Ticks = timeGetTime();
  56. Sec = Ticks/1000;
  57. Usec = (Ticks - (Sec*1000))*1000;
  58. tp->tv_sec = Sec;
  59. tp->tv_usec = Usec;
  60. #endif /* WITHOUT_MM_LIB */
  61. (void)nothing;
  62. return 0;
  63. }
  64. #else /* WIN32 */
  65. /* non-win32 version of Curl_gettimeofday() */
  66. static int gettimeofday(struct timeval *tp, void *nothing)
  67. {
  68. (void)nothing; /* we don't support specific time-zones */
  69. tp->tv_sec = (long)time(NULL);
  70. tp->tv_usec = 0;
  71. return 0;
  72. }
  73. #endif /* WIN32 */
  74. #endif /* HAVE_GETTIMEOFDAY */
  75. /* Return the current time in a timeval struct */
  76. struct timeval curlx_tvnow(void)
  77. {
  78. struct timeval now;
  79. (void)gettimeofday(&now, NULL);
  80. return now;
  81. }
  82. /*
  83. * Make sure that the first argument is the more recent time, as otherwise
  84. * we'll get a weird negative time-diff back...
  85. *
  86. * Returns: the time difference in number of milliseconds.
  87. */
  88. long curlx_tvdiff(struct timeval newer, struct timeval older)
  89. {
  90. return (newer.tv_sec-older.tv_sec)*1000+
  91. (newer.tv_usec-older.tv_usec)/1000;
  92. }
  93. /*
  94. * Same as curlx_tvdiff but with full usec resolution.
  95. *
  96. * Returns: the time difference in seconds with subsecond resolution.
  97. */
  98. double curlx_tvdiff_secs(struct timeval newer, struct timeval older)
  99. {
  100. return (double)(newer.tv_sec-older.tv_sec)+
  101. (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  102. }
  103. /* return the number of seconds in the given input timeval struct */
  104. long Curl_tvlong(struct timeval t1)
  105. {
  106. return t1.tv_sec;
  107. }