Reputation: 4745
In my C++ windows app I am converting a SYSTEMTIME to a formatted string like so:
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
char pSystemTime[50];
sprintf_s(pSystemTime, 50, "%04d%02d%02d%02d%02d%02d%03u\0", systemTime.wYear, systemTime.wMonth,
systemTime.wDay, systemTime.wHour,
systemTime.wMinute, systemTime.wSecond,
systemTime.wMilliseconds);
Now I have a time in milliseconds since UNIX epoch.
long time = 1442524186; //for example
How do I convert this long epoch time to a SYSTEMTIME so that I can also format it to a string?
Upvotes: 1
Views: 4849
Reputation: 91
In MillisToSystemTime
the line should be this way:
UINT64 t = multiplier * millis + 116444736000000000
Upvotes: 0
Reputation: 5002
static UINT64 FileTimeToMillis(const FILETIME &ft)
{
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime; // could use memcpy here!
uli.HighPart = ft.dwHighDateTime;
return static_cast<UINT64>(uli.QuadPart/10000);
}
static void MillisToSystemTime(UINT64 millis, SYSTEMTIME *st)
{
UINT64 multiplier = 10000;
UINT64 t = multiplier * millis;
ULARGE_INTEGER li;
li.QuadPart = t;
// NOTE, DON'T have to do this any longer because we're putting
// in the 64bit UINT directly
//li.LowPart = static_cast<DWORD>(t & 0xFFFFFFFF);
//li.HighPart = static_cast<DWORD>(t >> 32);
FILETIME ft;
ft.dwLowDateTime = li.LowPart;
ft.dwHighDateTime = li.HighPart;
::FileTimeToSystemTime(&ft, st);
}
Source: https://stackoverflow.com/a/11123106/2385309
Edit: Also see: https://stackoverflow.com/a/26486180/2385309 You might need to add/subtract 11644473600000 for Epoch time milliseconds
Upvotes: 5