gettimeofday.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifdef _WIN32
  2. #include "gettimeofday.h"
  3. int gettimeofday(struct timeval* tp, struct timezone* tzp)
  4. {
  5. static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
  6. SYSTEMTIME system_time;
  7. FILETIME file_time;
  8. uint64_t time;
  9. GetSystemTime(&system_time);
  10. SystemTimeToFileTime(&system_time, &file_time);
  11. time = ((uint64_t)file_time.dwLowDateTime);
  12. time += ((uint64_t)file_time.dwHighDateTime) << 32;
  13. /*converting file time to unix epoch*/
  14. tp->tv_sec = (long)((time - EPOCH) / 10000000L);
  15. tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
  16. return 0;
  17. }
  18. int clock_gettime(int dummy, struct timespec* ct)
  19. {
  20. LARGE_INTEGER count;
  21. if (g_first_time) {
  22. g_first_time = 0;
  23. if (0 == QueryPerformanceFrequency(&g_counts_per_sec)) {
  24. g_counts_per_sec.QuadPart = 0;
  25. }
  26. }
  27. if ((NULL == ct) || (g_counts_per_sec.QuadPart <= 0) || (0 == QueryPerformanceCounter(&count))) {
  28. return -1;
  29. }
  30. ct->tv_sec = count.QuadPart / g_counts_per_sec.QuadPart;
  31. ct->tv_nsec = ((count.QuadPart % g_counts_per_sec.QuadPart) * BILLION) / g_counts_per_sec.QuadPart;
  32. return 0;
  33. }
  34. #endif