1

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Harry Boy
  • 4,159
  • 17
  • 71
  • 122
  • 1
    This proposal appears to be semantically _wrong_, to me. Values of type `SYSTEMTIME` are produced only by `GetSystemTime`, which represents the current system time. This type is not intended for general use. Have you investigated ways to instead format the UNIX timestamp itself? That's what I'd do, for sure. – Lightness Races in Orbit Sep 17 '15 at 17:38

2 Answers2

5
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

Community
  • 1
  • 1
72DFBF5B A0DF5BE9
  • 4,954
  • 3
  • 21
  • 24
  • When I pass in a time of e.g. 1442535162 to the website http://www.epochconverter.com/ it gives a time GMT: Fri, 18 Sep 2015 00:12:42 GMT which is correct but when I pass it into MillisToSystemTime function the SYSTEMTIME value is incorrect as its says wYear=1601, wMonth=1, wDayOfWEeek=3... – Harry Boy Sep 17 '15 at 18:49
  • You need to multiply that number to 1000 for milliseconds, so try with 1442524186000 – 72DFBF5B A0DF5BE9 Sep 17 '15 at 18:55
  • but its already getting multiplied by 10000, and how does multiplying 1442535162 by 1000 get 1442524186000?? – Harry Boy Sep 18 '15 at 09:18
0

In MillisToSystemTime the line should be this way:

UINT64 t = multiplier * millis + 116444736000000000
Anna
  • 2,988
  • 3
  • 15
  • 29
sreeR
  • 59
  • 6