Namespaces
Variants
Views
Actions

std::chrono::time_point::time_point

From cppreference.com
< cpp‎ | chrono‎ | time point
Revision as of 15:34, 23 August 2013 by Nate (Talk | contribs)

constexpr time_point();
(1) (since C++11)
constexpr explicit time_point( const duration& d );
(2) (since C++11)
template< class Duration2 >
constexpr time_point( const time_point<Clock,Duration2>& t );
(3) (since C++11)

Constructs a new time_point from one of several optional data sources.

1) Default constructor, creates a time_point with a value of Clock's epoch.
2) Constructs a time_point at Clock's epoch plus d.
3) Constructs a time_point by converting t to duration. This constructor only participates in overload resolution if Duration2 is implicitly convertible to duration.

Contents

Parameters

d - a duration to copy from
t - a time_point to convert from

Notes

These constructors were not constexpr in C++11, this was corrected in C++14.

Example

#include <chrono>
#include <iostream> // std::cout
 
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::time_point<Clock> TimePoint;
 
inline void print_ms(const TimePoint& point) {
  typedef std::chrono::milliseconds Ms;
  const Clock::duration since_epoch = point.time_since_epoch();
  std::cout << std::chrono::duration_cast<Ms>(since_epoch).count() << " ms\n";
}
 
int main() {
  const TimePoint default_value = TimePoint(); // (1)
  print_ms(default_value); // 0 ms
 
  const Clock::duration duration_4_seconds = std::chrono::seconds(4);
  const TimePoint time_point_4_seconds(duration_4_seconds); // (2)
      // 4 seconds from start of epoch
  print_ms(time_point_4_seconds); // 4000 ms
 
  const TimePoint time_point_now = Clock::now(); // (3)
  print_ms(time_point_now); // 43098276 ms
}

Output:

0 ms
4000 ms
43098276 ms

See also

constructs new duration
(public member function of std::chrono::duration) [edit]
converts a duration to another, with a different tick interval
(function template) [edit]