Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ \ Convert FILETIME to seconds

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?

like image 848
user1025852 Avatar asked Oct 31 '13 14:10

user1025852


Video Answer


1 Answers

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.

like image 97
Ale Avatar answered Sep 24 '22 13:09

Ale