How to convert std::filesystem::file_time_type to time_t?

后端 未结 2 1356
清歌不尽
清歌不尽 2020-12-18 06:24

I wrote a solution for windows using MSVC2015 where the follow code converts the std::filesystem::last_write_time result time_t:

time_t ftime = std::file_tim         


        
2条回答
  •  余生分开走
    2020-12-18 07:12

    Before C++20? There is no portable way to do that. It worked in that version of Visual Studio because that version of VS's filesystem implementation just so happens to use system_clock for the filesystem clock type. This is not required by the C++ standard, but it is allowed. So your code just so happened to work.

    Pre-C++20, there was no mechanism to align two clocks together, so that time from one could be converted into the time of another. So if the filesystem clock isn't system_clock, you're out of luck. You would have to write implementation-specific code using knowledge of how that implementation implemented their filesystem clock (basically knowing what epoch it uses) so that you could manually convert it to a system_clock::time_point.

    C++20 gives system_clock a fixed epoch to be used across all implementations (UNIX-time), and requires that file_clock be able to convert its time points into system_clock time points.

提交回复
热议问题