Overview
time_point is associated with a clock . It represents the duration since epoch time of the clock.Details
common_type class
This is a helper class.
When two duration objects of different types are involved, the one with the longest period is converted to common type before the operation.
common_type is a specialization of the standard traits class common_type. It defines the most precise duration type between its two template arguments. It defines the common type of two time_point types that use the same clock type using the common type of their two durations.
Syntax
template< class Clock, class Duration1, class Duration2 > struct common_type<std::chrono::time_point<Clock, Duration1>, std::chrono::time_point<Clock, Duration2>>
Clock
When two duration objects of different types are involved, the one with the longest period is converted
The clock used by both time_point types.
Duration1,Duration2
When two duration objects of different types are involved, the one with the longest period is converted
The duration types of the time_point types.
Member Types
Name Description type The time_point common type The common type of two time_point types is a time_point with the same clock as the two types and the common_type of their durations.
time_point class
time_point template class is defined as below.
Syntax
template <class Clock, class Duration = typename Clock::duration>class time_point;
member typesName Description clock 1st template parameter. Clock, the clock on which this time point is measured duration 2nd template parameter. duration used to represent the time point since epoch time. Rep duration::rep, an arithmetic type representing the number of ticks of the duration period duration::period, a ratio representing the tick period of the duration
constructors
The default constructor and copy constructors are automatically defined by the define keyword. The following additional constructors are also available.Name Description time_point() Default constructor. Default constructor, creates a time_point representing the Clock's epoch.
Example
//prints:[0, <1,1000000000>]
cout << system_clock::time_point().time_since_epoch();
time_point
(const duration& d) Constructs an object representing a time point where a duration of d has elapsed since the epoch.
Example
//prints:[1000000000, <1,1000000000>]
cout << system_clock::time_point(1s).time_since_epoch();
template< class Duration2 >
time_point
(const time_point<Clock, Duration2>& t) Constructs a duration by converting d to an appropriate period and tick count, as if by duration_cast<duration>(d).count().
Example
//prints:[1740146546277738489, <1,1000000000>]
cout << system_clock::time_point(system_clock::now()).time_since_epoch();
methods
Name | Description |
---|---|
duration time_since_epoch() | returns the time point as duration since the start of its clock. Example //prints:[1740146546277738489, <1,1000000000>] cout << system_clock::time_point(system_clock::now()).time_since_epoch(); |
time_point::min() | returns the time point corresponding to the smallest duration Example //prints:[-9223372036854775808, <1,1000000000>] cout << system_clock::time_point::min().time_since_epoch(); |
time_point::max() | returns the time point corresponding to the largest duration. Example //prints:[9223372036854775808, <1,1000000000>] cout << system_clock::time_point::max().time_since_epoch(); |
time_point& operator+= (const duration& d ) | Applies the offset d to pt. Effectively, d is added to the internally stored duration d_ as d_ += d. Example system_clock::time_point pt; pt+=5s; //prints:[5000000000, <1,1000000000>] cout << pt.time_since_epoch(); |
time_point& operator-= (const duration& d ) | Applies the offset d to pt in negative direction. Effectively, d is subtracted from internally stored duration d_ as d_ -= d. Example system_clock::time_point pt; pt-=5s; //prints:[-5000000000, <1,1000000000>] cout << pt.time_since_epoch(); |
arithmetic and relational methods
The following arithmetic operations are supported. These are implemented as non member methods. When two duration objects of different types are involved, the one with the longest period is converted to common type before the operation.
The Rep type and Period can be different for rhs and lhs. However storing the result of an arithmetic operation of higher period and lower period into higher period is not allowed.
For example, secs += millisecs.
Operation | Description |
---|---|
addition pt+d d+pt | 1. template< class C, class D1, class R2, class P2 > time_point<C, common_type_t<D1, duration<R2,P2>>> operator+( const time_point<C,D1>& pt, const duration<R2,P2>& d ) 2. template< class R1, class P1, class C, class D2 > time_point<C, common_type_t<duration<R1,P1>,D2>> operator+( const duration<R1,P1>& d,const time_point<C,D2>& pt ) |
subtraction pt-d lhs-rhs |
|
equality lhs==rhs | template< class Clock, class Dur1, class Dur2 > bool operator==( const time_point<Clock,Dur1>& lhs,const time_point<Clock,Dur2>& rhs ) Checks if the time points are the same.
|
inequality lhs!=rhs | template< class Clock, class Dur1, class Dur2 > bool operator!=( const time_point<Clock,Dur1>& lhs,const time_point<Clock,Dur2>& rhs ) |
less lhs < rhs | template< class Clock, class Dur1, class Dur2 > bool operator!=( const time_point<Clock,Dur1>& lhs,const time_point<Clock,Dur2>& rhs ) |
greater lhs > rhs | template< class Clock, class Dur1, class Dur2 > bool operator!=( const time_point<Clock,Dur1>& lhs,const time_point<Clock,Dur2>& rhs ) |
lessorequal lhs <=rhs | template< class Clock, class Dur1, class Dur2 > bool operator!=( const time_point<Clock,Dur1>& lhs,const time_point<Clock,Dur2>& rhs ) |
greater or equal lhs>=rhs | template< class Clock, class Dur1, class Dur2 > bool operator!=( const time_point<Clock,Dur1>& lhs,const time_point<Clock,Dur2>& rhs ) |
time_cast()
Converts a time_point from one duration to another. Converts the value of tp into a time_point type with a different duration internal object, taking into account differences in their durations' periods.
The function uses duration_cast to convert the internal duration objects.
template< class ToDuration, class Clock, class Duration > constexpr std::chrono::time_point<Clock, ToDuration> time_point_cast( const std::chrono::time_point<Clock, Duration> &tp );
typedef duration<int,ratio<60*60*24>> days_type; time_point<system_clock,days_type> today = time_point_cast<days_type>(system_clock::now()); //prints:[20141, <86400,1>] cout << today.time_since_epoch() << endl;