std::time_t to std::filesystem::file_time_t in C++ 17 [duplicate]
Until C++20's full date/time stuff comes along, there isn't much you can do with a file_time_type
besides store it and compare it to another file_time_type
. Your to_time_t
trick is not portable, because file_time_type
is not required to be a system_clock
time.
If you want to store a string representing an integer representation of a file_time_type
and convert it back, then you should just get the time_since_epoch().count()
of the time directly, rather than using to_time_t
gymnastics. You can convert the integer back to a file_time_type
by doing this:
unsigned long long int_val = ...;
using ft = std::filesystem::file_time_type;
auto the_time = ft(ft::duration(int_val));
Of course, this only works if the source and destination implementations use compatible filesystem clocks. And even if they use the same clocks with the same epochs, their file_time_type::duration
needs to be the same too.