timeval.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, Daniel Stenberg, <[email protected]>, 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. #ifdef WIN32
  24. #include <windows.h>
  25. #endif
  26. #include "timeval.h"
  27. #ifndef HAVE_GETTIMEOFDAY
  28. #ifdef WIN32
  29. int
  30. gettimeofday (struct timeval *tp, void *nothing)
  31. {
  32. #ifdef WITHOUT_MM_LIB
  33. SYSTEMTIME st;
  34. time_t tt;
  35. struct tm tmtm;
  36. /* mktime converts local to UTC */
  37. GetLocalTime (&st);
  38. tmtm.tm_sec = st.wSecond;
  39. tmtm.tm_min = st.wMinute;
  40. tmtm.tm_hour = st.wHour;
  41. tmtm.tm_mday = st.wDay;
  42. tmtm.tm_mon = st.wMonth - 1;
  43. tmtm.tm_year = st.wYear - 1900;
  44. tmtm.tm_isdst = -1;
  45. tt = mktime (&tmtm);
  46. tp->tv_sec = tt;
  47. tp->tv_usec = st.wMilliseconds * 1000;
  48. #else
  49. /**
  50. ** The earlier time calculations using GetLocalTime
  51. ** had a time resolution of 10ms.The timeGetTime, part
  52. ** of multimedia apis offer a better time resolution
  53. ** of 1ms.Need to link against winmm.lib for this
  54. **/
  55. unsigned long Ticks = 0;
  56. unsigned long Sec =0;
  57. unsigned long Usec = 0;
  58. Ticks = timeGetTime();
  59. Sec = Ticks/1000;
  60. Usec = (Ticks - (Sec*1000))*1000;
  61. tp->tv_sec = Sec;
  62. tp->tv_usec = Usec;
  63. #endif
  64. return 1;
  65. }
  66. #define HAVE_GETTIMEOFDAY
  67. #endif
  68. #endif
  69. struct timeval Curl_tvnow (void)
  70. {
  71. struct timeval now;
  72. #ifdef HAVE_GETTIMEOFDAY
  73. gettimeofday (&now, NULL);
  74. #else
  75. now.tv_sec = (long) time(NULL);
  76. now.tv_usec = 0;
  77. #endif
  78. return now;
  79. }
  80. /*
  81. * Make sure that the first argument is the more recent time, as otherwise
  82. * we'll get a weird negative time-diff back...
  83. */
  84. long Curl_tvdiff (struct timeval newer, struct timeval older)
  85. {
  86. return (newer.tv_sec-older.tv_sec)*1000+
  87. (499+newer.tv_usec-older.tv_usec)/1000;
  88. }
  89. long Curl_tvlong (struct timeval t1)
  90. {
  91. return t1.tv_sec;
  92. }
  93. /*
  94. * local variables:
  95. * eval: (load-file "../curl-mode.el")
  96. * end:
  97. * vim600: fdm=marker
  98. * vim: et sw=2 ts=2 sts=2 tw=78
  99. */