How would it be to convert a SYSTEMTIME structure returned by GetLocalTime to time stamp ( seconds since 1970, ULONG )?
Asked
Active
Viewed 121 times
1 Answers
2
One way is to convert both the reference time and the target time to FILETIME
structures, which then lets you perform simple arithmetic upon their values. These are measured in 100ns units and so you need to divide the final result by 10000000 to get number of seconds.
// reference time, convert to FILETIME
SYSTEMTIME stRef{};
stRef.wYear = 1970;
stRef.wMonth = 1;
stRef.wDay = 1;
FILETIME ftRef;
SystemTimeToFileTime(&stRef, &ftRef);
// target time, convert to FILETIME
SYSTEMTIME stTarget;
GetLocalTime(&stTarget);
FILETIME ftTarget;
SystemTimeToFileTime(&stTarget, &ftTarget);
// convert both to ULARGE_INTEGER
ULARGE_INTEGER ulRef, ulTarget;
ulRef.HighPart = ftRef.dwHighDateTime;
ulRef.LowPart = ftRef.dwLowDateTime;
ulTarget.HighPart = ftTarget.dwHighDateTime;
ulTarget.LowPart = ftTarget.dwLowDateTime;
// subtract reference time from target time
ulTarget.QuadPart -= ulRef.QuadPart;
// convert to seconds (divide by 10000000)
DWORD dwSecondsSinceRefTime = ulTarget.QuadPart / 10000000i64;

Jonathan Potter
- 36,172
- 4
- 64
- 79
-
Shouldn't it be `GetSystemTime` instead of `GetLocalTime`? We are supposed to see the same time-stamp right now even if we are at different time zones. – Barmak Shemirani Oct 16 '15 at 03:33
-
@BarmakShemirani That's what the OP asked for, you could use any timestamp there of course. It's the method that's important. – Jonathan Potter Oct 16 '15 at 03:44
-
I don't know how I missed `GetLocalTime` in OP's question. Early stages of dementia perhaps. – Barmak Shemirani Oct 16 '15 at 04:19