4

I'm looking for a way to convert a Windows FILETIME structure to a std::chrono::time_point< std::chrono::file_clock> so that I can build a difference between two file times and express that duration f.e. in std::chrono::microseconds.

EDIT: Here is a complete godbolt example of Howard's Answer.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ridilculous
  • 624
  • 3
  • 16

1 Answers1

4

Disclaimer: I don't have a Windows system to test on.

I believe that one just needs to take the high and low words of the FILETIME and interpret that 64 bit integral value as the number of tenths of a microsecond since the std::chrono::file_clock epoch:

FILETIME ft = ...
file_clock::duration d{(static_cast<int64_t>(ft.dwHighDateTime) << 32)
                                           | ft.dwLowDateTime};
file_clock::time_point tp{d};

Both the units and the epoch of FILETIME and file_clock (on Windows) are identical.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • I am using C++11 and have no `std::chrono::file_clock` available yet. Just `std::chrono::system_clock`. Don't know about the first one, but for the `std::chrono::system_clock` you need to subtract 116444736000000000 after combining upper and lower `DWORD` of the `FILETIME`. Otherwise, the time_point is off. – Simon Rozman Jul 20 '23 at 15:41
  • Right. The epoch of both `FILETIME` and `std::chrono::file_clock` is 1601-01-01 (on Windows). And the epoch of `std::chrono::system_clock` is 1970-01-01. Windows ticks are deci-microseconds and there are 116444736000000000 of those between these two epochs. – Howard Hinnant Jul 20 '23 at 15:54