Template:cpp/filesystem/time example
From cppreference.com
Run this code
#include <iostream> #include <chrono> #include <iomanip> #include <fstream> #include <filesystem> namespace fs = std::filesystem; using namespace std::chrono_literals; int main() { fs::path p = fs::current_path() / "example.bin"; std::ofstream(p.c_str()).put('a'); // create file auto ftime = fs::last_write_time(p); // assuming system_clock for this demo // note: not true on MSVC; C++20 will allow portable output std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime); std::cout << "File write time is " << std::asctime(std::localtime(&cftime)) << '\n'; fs::last_write_time(p, ftime + 1h); // move file write time 1 hour to the future ftime = fs::last_write_time(p); // read back from the filesystem cftime = decltype(ftime)::clock::to_time_t(ftime); std::cout << "File write time is " << std::asctime(std::localtime(&cftime)) << '\n'; fs::remove(p); }
Possible output:
File write time is Tue Mar 31 19:47:04 2015 File write time is Tue Mar 31 20:47:04 2015