c++windowsc++20c++-chronofiletime

How to convert a windows FILETIME to a std::chrono::time_point<std::chrono::file_clock> in C++


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.


Solution

  • 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.