simon
simon

Reputation: 1240

Convert chrono duration to time_point

How can I convert a chrono duration to a time_point, which is later than clock's epoch with the given duration? I tried to find epoch time in chrono clock without success.

Upvotes: 7

Views: 11274

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 219488

From [time.point.cons]/p2:

template <class Clock, class Duration = typename Clock::duration>
class time_point
{
public:
    constexpr explicit time_point(const duration& d);  // same as time_point() + d

This can be used (for example) like:

using namespace std::chrono;
system_clock::time_point tp{30min}; // Needs C++14 for the literal

Though the system_clock::time_point is not specified in the standard, in practice this creates a time_point referring to 1970-01-01 00:30:00 UTC.

Update for C++20

The epoch of system_clock is now specified to be 1970-01-01 00:00:00 UTC.

Upvotes: 14

Related Questions