1 year ago

#251841

test-img

Markus Donath

C++ std::filesystem::last_write_time operator< sometimes not correct?

I want to compare the file age of two files. I tried this:

auto ft1 = std::filesystem::last_write_time("file1");
auto ft2 = std::filesystem::last_write_time("file2");
if (ft1 < ft2)
  std::cout << "file1 is older than file2.";

Unfortunately sometimes the condition is true even if both file ages are equal. Converting to time_t helps:

auto ft1 = std::chrono::system_clock::to_time_t(std::filesystem::last_write_time("file1"));
auto ft2 = std::chrono::system_clock::to_time_t(std::filesystem::last_write_time("file2"));
if (ft1 < ft2)
  std::cout << "file1 is older than file2.";

This always works fine. Any ideas what's the problem with my first version?

c++

c++17

std-filesystem

0 Answers

Your Answer

Accepted video resources