5

How can I convert FILETIME to seconds? I need to compare two FILETIME objects..

I found this, but seems like it doesn't do the trick...

 ULARGE_INTEGER ull;
    ull.LowPart = lastWriteTimeLow1;
    ull.HighPart = lastWriteTimeHigh1;
    time_t lastModified =  ull.QuadPart / 10000000ULL - 11644473600ULL;

    ULARGE_INTEGER xxx;
    xxx.LowPart = currentTimeLow1;
    xxx.HighPart = currentTimeHigh1;
    time_t current =  xxx.QuadPart / 10000000ULL - 11644473600ULL;

    unsigned long SecondsInterval = current - lastModified;

    if (SecondsInterval > RequiredSecondsFromNow)
        return true;

    return false;

I compared to 2 FILETIME and expected diff of 10 seconds and it gave me ~7000... Is that a good way to extract number of seconds?

user1025852
  • 2,684
  • 11
  • 36
  • 58
  • how are you comparing the FILETIME objects? instead of comparing two different variables why don't you try checking your conversion code by outputting the formatted FILETIME variable, then converting that same FILETIME variable to time_t and outputting the (formatted) result. There should be no difference (except for the loss of precision). – gordonk Oct 31 '13 at 14:53
  • I'm not with C++ background I wish I knew how to do it..I'll add the full code and you'll decide.. – user1025852 Oct 31 '13 at 15:04

3 Answers3

5

The code you give seems correct, it converts a FILETIME to a UNIX timestamp (obviously losing precision, as FILETIME has a theoretical resolution of 100 nanoseconds). Are you sure that the FILETIMEs you compare indeed have only 10 seconds of difference?

I actually use a very similar code in some software:

double time_d()
{
  FILETIME ft;
  GetSystemTimeAsFileTime(&ft);
  __int64* val = (__int64*) &ft;
  return static_cast<double>(*val) / 10000000.0 - 11644473600.0;   // epoch is Jan. 1, 1601: 134774 days to Jan. 1, 1970
}

This returns a UNIX-like timestamp (in seconds since 1970) with sub-second resolution.

Ale
  • 1,727
  • 14
  • 26
  • I thought maybe it doesn't include my TimeZone (+02:00) when I get NOW date? – user1025852 Oct 31 '13 at 15:08
  • yes my code for time_d() is also running only on Windows... about the other comment, indeed it might be a timezone problem. do you obtain your timestamps with the same function? – Ale Nov 01 '13 at 15:40
  • 1
    It seems likely that you don't have the times you think you do. As a debugging measure, try converting both FILETIME structures to SYSTEMTIME structures, and see how far apart the times actually are. – Harry Johnston Nov 07 '13 at 22:26
  • Actually, how do you get the local time in your code? Make sure you obtain it as UTC, not as local time (e.g., if you're using GetLocalTime(), it should be GetSystemTime()), or there will be a difference due to the timezones (file times are in UTC). – Ale Nov 08 '13 at 09:40
0

For the sake of comparisom

double toSeconds(const FILETIME& t)
{
    return LARGE_INTEGER{t.dwLowDateTime, (long)t.dwHighDateTime}.QuadPart * 1e-7;
}

is the simplest.

Bohdan
  • 468
  • 5
  • 6
0

You can use this macro to get time in UNIX epochs:

#define windows_time_to_unix_epoch(x) ((x) - 116444736000000000LL) / 10000000LL

Tapan Pandey
  • 1
  • 2
  • 24
  • 89