Advertisement
Rochet2

Untitled

Jun 26th, 2015
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. // Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
  2. // windows and linux.
  3.  
  4. uint64 GetTimeMs64()
  5. {
  6. #ifdef _WIN32
  7. // Windows
  8. FILETIME ft;
  9. LARGE_INTEGER li;
  10.  
  11. // Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
  12. // to a LARGE_INTEGER structure.
  13. GetSystemTimeAsFileTime(&ft);
  14. li.LowPart = ft.dwLowDateTime;
  15. li.HighPart = ft.dwHighDateTime;
  16.  
  17. uint64 ret = li.QuadPart;
  18. ret -= 116444736000000000LL; // Convert from file time to UNIX epoch time.
  19. ret /= 10000; // From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals
  20.  
  21. return ret;
  22. #else
  23. // Linux
  24. struct timeval tv;
  25.  
  26. gettimeofday(&tv, NULL);
  27.  
  28. uint64 ret = tv.tv_usec;
  29. // Convert from micro seconds (10^-6) to milliseconds (10^-3)
  30. ret /= 1000;
  31.  
  32. // Adds the seconds (10^0) after converting them to milliseconds (10^-3)
  33. ret += (tv.tv_sec * 1000);
  34.  
  35. return ret;
  36. #endif
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement