Answer the question
In order to leave comments, you need to log in
How to convert FILETIME to time_t in C++?
In one place, the modified date is determined as
std::filesystem::last_write_time(filepath);
and with the help of the code
// Преобразовать дату/время файла в time_t
template <typename TP> std::time_t to_time_t(TP tp)
{
using namespace std::chrono;
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now()
+ system_clock::now());
return system_clock::to_time_t(sctp);
}
static std::time_t to_time_t(FILETIME ft)
{
std::time_t ret = 0;
// Преобразовать в time_t
ULARGE_INTEGER ull;
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
ret = (ull.QuadPart / 10000000ULL - 11644473600ULL);
return ret;
}
Answer the question
In order to leave comments, you need to log in
Here is the code. It gives the same result. At least for test cases
// Преобразовать в time_t
static std::time_t to_time_t(const ULARGE_INTEGER& ull)
{
// Преобразовать в time_t
std::time_t ret = (ull.QuadPart / 10000000ULL - 11644473600ULL);
// Правильное оуркгление
ULONGLONG QuadPartMod = ull.QuadPart % 10000000ULL;
if (QuadPartMod >= 5000000ULL) {
ret++;
}
//
return ret;
}
static std::time_t to_time_t(const FILETIME& ft)
{
std::time_t ret = 0;
// Преобразовать в ULARGE_INTEGER
ULARGE_INTEGER ull;
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
// Преобразовать в time_t
return to_time_t(ull);
}
template <typename TP> std::time_t to_time_t(TP tp)
{
// Преобразовать в ULARGE_INTEGER
ULARGE_INTEGER ull;
ull.QuadPart = tp.time_since_epoch().count();
// Преобразовать в time_t
return to_time_t(ull);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question