timeval.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2000, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  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. SYSTEMTIME st;
  33. time_t tt;
  34. struct tm tmtm;
  35. /* mktime converts local to UTC */
  36. GetLocalTime (&st);
  37. tmtm.tm_sec = st.wSecond;
  38. tmtm.tm_min = st.wMinute;
  39. tmtm.tm_hour = st.wHour;
  40. tmtm.tm_mday = st.wDay;
  41. tmtm.tm_mon = st.wMonth - 1;
  42. tmtm.tm_year = st.wYear - 1900;
  43. tmtm.tm_isdst = -1;
  44. tt = mktime (&tmtm);
  45. tp->tv_sec = tt;
  46. tp->tv_usec = st.wMilliseconds * 1000;
  47. return 1;
  48. }
  49. #define HAVE_GETTIMEOFDAY
  50. #endif
  51. #endif
  52. struct timeval Curl_tvnow (void)
  53. {
  54. struct timeval now;
  55. #ifdef HAVE_GETTIMEOFDAY
  56. gettimeofday (&now, NULL);
  57. #else
  58. now.tv_sec = (long) time(NULL);
  59. now.tv_usec = 0;
  60. #endif
  61. return now;
  62. }
  63. /*
  64. * Make sure that the first argument is the more recent time, as otherwise
  65. * we'll get a weird negative time-diff back...
  66. */
  67. long Curl_tvdiff (struct timeval newer, struct timeval older)
  68. {
  69. return (newer.tv_sec-older.tv_sec)*1000+
  70. (499+newer.tv_usec-older.tv_usec)/1000;
  71. }
  72. long Curl_tvlong (struct timeval t1)
  73. {
  74. return t1.tv_sec;
  75. }
  76. /*
  77. * local variables:
  78. * eval: (load-file "../curl-mode.el")
  79. * end:
  80. * vim600: fdm=marker
  81. * vim: et sw=2 ts=2 sts=2 tw=78
  82. */