| 1 | #include <stdint.h> |
| 2 | #include <stdlib.h> |
| 3 |
|
| 4 | #if defined(__unix__) || defined(unix) || defined(__unix) || defined(__CYGWIN__) |
| 5 | # include <sys/time.h> |
| 6 | uint64_t time_us(void) { |
| 7 | uint64_t us; |
| 8 | struct timeval lnm_current_time; |
| 9 | gettimeofday(&lnm_current_time, NULL); |
| 10 | us = (lnm_current_time.tv_sec * 1000000ULL + lnm_current_time.tv_usec); |
| 11 | return us; |
| 12 | } |
| 13 | #elif defined(_WIN32) || defined(__WINDOWS__) |
| 14 | # include <sysinfoapi.h> |
| 15 | # include <windows.h> |
| 16 | uint64_t time_us(void) { |
| 17 | uint64_t us; |
| 18 | // get system time in ticks |
| 19 | FILETIME lnm_win32_filetime; |
| 20 | GetSystemTimeAsFileTime(&lnm_win32_filetime); |
| 21 | // load time from two 32-bit words into one 64-bit integer |
| 22 | us = lnm_win32_filetime.dwHighDateTime; |
| 23 | us = us << 32; |
| 24 | us |= lnm_win32_filetime.dwLowDateTime; |
| 25 | // convert to microseconds |
| 26 | us /= 10ULL; // is there a better way to do this? |
| 27 | // convert from time since Windows NT epoch to time since Unix epoch |
| 28 | us -= 11644473600000000ULL; |
| 29 | return us; |
| 30 | } |
| 31 | #endif |
| 32 |
|