Rather than using WMI to obtain the last boot time, I wanted to calculate it using ::GetSystemTime()
and ::GetTickCount64
. But once I get to milliseconds, I don't know how to get back to a FILETIME object.
I tried this:
static ULONGLONG FileTimeToMillis(const FILETIME &ft)
{
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime; // could use memcpy here!
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart/10000;
}
static void MillisToSystemTime(ULONGLONG millis, SYSTEMTIME *st)
{
UINT64 t = static_cast<UINT64>(-10000) * static_cast<UINT64>(millis);
FILETIME ft;
ft.dwLowDateTime = static_cast<DWORD(t & 0xFFFFFFFF);
ft.dwHighDateTime = static_cast<DWORD>(t >> 32);
::FileTimeToSystemTime(&ft, st);
}
void main()
{
SYSTEMTIME st, lt, st2, lt2;
::GetSystemTime(&st);
::GetLocalTime(<);
cout << "The system time is: " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << endl;
cout << "The local time is: " << lt.wHour << ":" << lt.wMinute << ":" << lt.wSecond << endl;
FILETIME sft, lft;
::SystemTimeToFileTime(&st, &sft);
::SystemTimeToFileTime(<, &lft);
cout << "The system time in millis is: " << FileTimeToMillis(sft) << endl;
cout << "The local time in millis is: " << FileTimeToMillis(lft) << endl;
MillisToSystemTime(FileTimeToMillis(sft), &st2);
MillisToSystemTime(FileTimeToMillis(sft), <2);
cout << "The system time (post conversion) is: " << st2.wHour << ":" << st2.wMinute << ":" << st2.wSecond << endl;
cout << "The local time (post conversion) is: " << lt2.wHour << ":" << lt2.wMinute << ":" << lt2.wSecond << endl;
}
But I certainly don't get what's expected. Instead, I get:
The system time is: 15:5:2
The local time is: 10:5:2
The system time in millis is: 12984678302935
The local time in millis is: 12984660302935
The system time (post conversion) is: 52428:52428:52428
The local time (post conversion) is: 52428:52428:52428
Any ideas? Please don't tell me to use Boost or because it's not available to me.