< cpp‎ | chrono
Revision as of 17:54, 4 December 2012 by Jyasskin (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <chrono>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >

    class Clock,
    class Duration = typename Clock::duration

> class time_point;

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Class template std::chrono::time_point represents a point in time. It is implemented as if it stores a value of type Duration indicating the time interval from the start of the Clock's epoch.


Member types

Member type Definition
clock Clock, the clock on which this time point is measured
duration Duration, a std::chrono::duration type used to measure the time since epoch
rep Rep, an arithmetic type representing the number of ticks of the duration
period Period, a std::ratio type representing the tick period of the duration

Member functions

Template:cpp/chrono/time point/dcl list constructorTemplate:cpp/chrono/time poind/dcl list time since epochTemplate:cpp/chrono/time point/dcl list operator arithTemplate:cpp/chrono/time point/dcl list minTemplate:cpp/chrono/time point/dcl list max

Non-member functions

Template:cpp/chrono/time point/dcl list common typeTemplate:cpp/chrono/time point/dcl list operator arith2Template:cpp/chrono/time point/dcl list operator cmpTemplate:cpp/chrono/time point/dcl list time point cast


This example prints the current time minus 24 hours:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
using std::chrono::system_clock;
int main()
    system_clock::time_point now = system_clock::now();
    std::time_t now_c = system_clock::to_time_t(
                            now - std::chrono::hours(24));
    std::cout << "One day ago, the time was "
              << std::put_time(std::localtime(&now_c), "%F %T") << '\n';

Possible output:

One day ago, the time was 2011-10-25 12:00:08

This example prints the time it takes to print "Hello World":

#include <iostream>
#include <chrono>
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::steady_clock;
int main()
    steady_clock::time_point start = steady_clock::now();
    std::cout << "Hello World\n";
    steady_clock::time_point end = steady_clock::now();
    std::cout << "Printing took "
              // duration_cast is required to avoid accidentally losing precision.
              << duration_cast<microseconds>(end - start).count()
              << "us.\n";

Possible output:

Hello World
Printing took 84us.

See also

a time interval
(class template)