Standard filesystem time types

Hi all,

https://en.cppreference.com/w/cpp/filesystem/file_time_type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <chrono>
#include <iomanip>
#include <fstream>
#include <filesystem>
 
using namespace std::chrono_literals;
int main()
{
    auto p = std::filesystem::temp_directory_path() / "example.bin";
    std::ofstream(p.c_str()).put('a'); // create file
 
    auto print_last_write_time = [](std::filesystem::file_time_type const& ftime) {
        std::time_t cftime = std::chrono::system_clock::to_time_t(
            std::chrono::file_clock::to_sys(ftime));
        std::cout << "File write time is " << std::asctime(std::localtime(&cftime));
    };
 
    auto ftime = std::filesystem::last_write_time(p);
    print_last_write_time(ftime);
 
    std::filesystem::last_write_time(p, ftime + 1h); // move file write time 1 hour to the future
    ftime = std::filesystem::last_write_time(p); // read back from the filesystem
    print_last_write_time(ftime);
 
    std::filesystem::remove(p);
}

So I'm trying to make sense of file_time_type between C++17 and C++20.

Clicking on the "Run this code" takes me to the coliru compiler tester.
Compiling with GCC11.1(C++20) mode, all is well.

But using GCC11.1(C++17), I get
1
2
3
4
5
6
7
main.cpp: In lambda function:

main.cpp:15:26: error: 'std::chrono::file_clock' has not been declared

   15 |             std::chrono::file_clock::to_sys(ftime));

      |                          ^~~~~~~~~~

Which seems fair enough, file_clock is new in C++20.

However, I'm baffled as to what to use in it's place to make it valid for C++17.

Thanks.
Last edited on
For C++17 you need to supply a TrivialClock* for the time point instead of std::chrono ::file_clock.

*<chrono> has several that fit the bill of TrivialClock:
https://en.cppreference.com/w/cpp/named_req/TrivialClock
Topic archived. No new replies allowed.