初めて実行すると、時間とティックカウントが取得されるため、同期されており、測定するものがあります。initセクションはスレッドセーフではなく、深夜に折り返されることはありませんが、以下のキャリーフォームに従って簡単に追加できます。
// this solution is limited to 49+ days of uptime
int timeinitted = 0;
SYSTEMTIME anchortime;
DWORD anchorticks;
void GetAccurateTime(SYSTEMTIME *lt)
{
if (timeinitted == 0)
{ // obviously this init section isn't threadsafe.
// get an anchor time to sync up with system ticks.
GetLocalTime(&anchortime);
anchorticks = timeGetTime();
timeinitted = 1;
}
DWORD now = timeGetTime();
DWORD flyby = now - anchorticks;
// now add flyby to anchortime
memcpy (lt, &anchortime, sizeof(anchortime));
// you can't do the math IN the SYSTEMTIME because everything in it is a WORD (16 bits)
DWORD ms = lt->wMilliseconds + flyby;
DWORD carry = ms / 1000;
lt->wMilliseconds = ms % 1000;
if (carry > 0)
{
DWORD s = lt->wSecond + carry;
carry = s / 60;
lt->wSecond = s % 60;
if (carry > 0)
{
DWORD m = lt->wMinute + carry;
carry = m / 60;
lt->wMinute = m % 60;
if (carry > 0) // won't wrap day correctly.
lt->wHour = (((DWORD)lt->wHour + carry)) % 24;
}
// add day and month and year here if you're so inspired, but remember the 49 day limit
}
}