Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert chrono duration to time_point

Tags:

c++11

chrono

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.

like image 442
simon Avatar asked Jun 10 '15 12:06

simon


1 Answers

From the synopsis in 20.12.6 [time.point]:

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.

like image 88
Howard Hinnant Avatar answered Sep 29 '22 17:09

Howard Hinnant