Richard
Richard

Reputation: 8758

How to retrieve the clock type of std::filesystem::file_time_type in C++17

Since std::filesystem::file_time_type uses a trivial clock in C++17, is there a way to retrieve the actual clock type of file_time_type with C++17**?

The goal is to convert the time to std::chrono::system_clock to use it e.g. in a stream.


** In C++20, file_time_type will use std::chrono::file_clock, which has operator<< and can be casted to other clocks using std::chrono::clock_cast.

Upvotes: 4

Views: 1000

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473232

You can retrieve the clock easily enough via file_time_type::clock. But converting from one clock to another won't really be possible until the changes from C++20 are available.

The reason the clock even matters is that different clocks define a different "epoch" start date, the 0 time point for that clock. But in C++11-17, all clocks have an implementation-defined "epoch", and each clock can use a different one. The file_time_type::clock is no different. Since every implementation of every clock potentially uses a different epoch, and no clock epochs are defined relative to each other, there's no way to convert one clock's time_point into another clock's time_point. Not without implementation-specific knowledge.

C++20 changes things in exactly one way: system_clock has a well-defined epoch: it uses UNIX-time. Because there is one clock with a well-defined epoch, that clock can act as a universal intermediate clock for clock-to-clock conversions. That is, every implementation-defined clock's epoch can be converted relative to UNIX-time, and vice-versa. Hence: clock_cast.

Without that definition, and the time_conversion machinery, there's not much you can do... portably, at least.

Upvotes: 5

Related Questions