Last active
April 24, 2017 14:13
-
-
Save udaken/c46c4d9c8ede8650fe805dcb281bfd8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <Windows.h> | |
| class HighPrecisionTime | |
| { | |
| LARGE_INTEGER m_freq; | |
| FILETIME m_lastTime; | |
| LARGE_INTEGER m_lastCount; | |
| public: | |
| static const unsigned long ONE_HUNDRED_NANOSECS_PER_SEC = 10 * 1000 * 1000; | |
| HighPrecisionTime() | |
| { | |
| if (!::QueryPerformanceFrequency(&m_freq)) | |
| { | |
| m_freq.QuadPart = 0; | |
| } | |
| } | |
| bool IsAvailabe() const | |
| { | |
| return m_freq.QuadPart != 0; | |
| } | |
| // system tick of 100-nanosecond intervals | |
| LONGLONG GetNanoTime() | |
| { | |
| if (!IsAvailabe()) return 0; | |
| LARGE_INTEGER count; | |
| if (::QueryPerformanceCounter(&count)) | |
| { | |
| return (count.QuadPart * ONE_HUNDRED_NANOSECS_PER_SEC) / m_freq.QuadPart; | |
| } | |
| return 0; | |
| } | |
| bool GetCurrentAsFileTime(LPFILETIME lpSystemTimeAsFiletime) | |
| { | |
| ::GetSystemTimeAsFileTime(lpSystemTimeAsFiletime); | |
| if (memcmp(lpSystemTimeAsFiletime, &m_lastTime, sizeof(FILETIME)) != 0) | |
| { | |
| ::QueryPerformanceCounter(&m_lastCount); | |
| m_lastTime = *lpSystemTimeAsFiletime; | |
| return true; | |
| } | |
| LARGE_INTEGER count; | |
| if (::QueryPerformanceCounter(&count)) | |
| { | |
| LONGLONG countdiff = count.QuadPart - m_lastCount.QuadPart; | |
| LONGLONG nanosecdiff = (countdiff * ONE_HUNDRED_NANOSECS_PER_SEC) / m_freq.QuadPart; | |
| ULARGE_INTEGER tmp = { lpSystemTimeAsFiletime->dwLowDateTime, lpSystemTimeAsFiletime->dwHighDateTime }; | |
| tmp.QuadPart += nanosecdiff; | |
| lpSystemTimeAsFiletime->dwLowDateTime = tmp.LowPart; | |
| lpSystemTimeAsFiletime->dwHighDateTime = tmp.HighPart; | |
| return true; | |
| } | |
| return false; | |
| } | |
| bool GetCurrentLocalTime(LPSYSTEMTIME lpLocalTime, const TIME_ZONE_INFORMATION* lpTimezoneInfo = NULL) | |
| { | |
| SYSTEMTIME sysTime; | |
| return GetCurrentSystemTime(&sysTime) && ::SystemTimeToTzSpecificLocalTime(lpTimezoneInfo, &sysTime, lpLocalTime); | |
| } | |
| bool GetCurrentSystemTime(LPSYSTEMTIME lpSysTime) | |
| { | |
| FILETIME filetime; | |
| return GetCurrentAsFileTime(&filetime) && ::FileTimeToSystemTime(&filetime, lpSysTime) != 0; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment