timeval.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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. struct timeval Curl_tvnow (void)
  76. {
  77. struct timeval now;
  78. (void)gettimeofday(&now, NULL);
  79. return now;
  80. }
  81. /*
  82. * Make sure that the first argument is the more recent time, as otherwise
  83. * we'll get a weird negative time-diff back...
  84. */
  85. long Curl_tvdiff (struct timeval newer, struct timeval older)
  86. {
  87. return (newer.tv_sec-older.tv_sec)*1000+
  88. (499+newer.tv_usec-older.tv_usec)/1000;
  89. }
  90. long Curl_tvlong (struct timeval t1)
  91. {
  92. return t1.tv_sec;
  93. }