Thursday, February 20, 2025

time_point

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
NameDescription
typeThe 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 types
NameDescription
clock1st template parameter. Clock, the clock on which this time point is measured
duration2nd template parameter. duration  used to represent the time point since epoch time.
Repduration::rep, an arithmetic type representing the number of ticks of the duration
periodduration::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.
NameDescription
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
NameDescription
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();
This example 4 prints information about the time elapsed.

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.

OperationDescription
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 )

Applies the offset d to pt. Effectively returns CT(pt.time_since_epoch() + d), where CT is the return type.

Example

    //1
    system_clock::time_point pt;
    auto pt2 = pt+5s;
    //prints:[5000000000, <1,1000000000>]
    cout << pt2.time_since_epoch();

    //2
    auto pt3 = 5s+pt;
    //prints:[5000000000, <1,1000000000>]
    cout << pt3.time_since_epoch();
subtraction
pt-d
lhs-rhs
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 C, class D1, class D2 >
common_type_t<D1,D2>
    operator-( const time_point<C,D1>& pt_lhs,const time_point<C,D2>& pt_rhs )

  1. Applies the offset d to pt in negative direction. Effectively returns CT(pt.time_since_epoch() - d), where CT is the return type.
  2. Computes the difference between pt_lhs and pt_rhs. Returns duration.

Example
    //1
    system_clock::time_point pt;
    auto pt2 = pt-5s;
    //prints:[-5000000000, <1,1000000000>]
    cout << pt2.time_since_epoch();

    //2
    auto d = pt-pt2;
    //prints:[5000000000, <1,1000000000>]
    cout << d;
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.

Example
    //prints:0
    cout << (system_clock::time_point() == system_clock::now());
inequality
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 not the same.

Example
    //prints:1
    cout << (system_clock::time_point() != system_clock::now());
less
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 point lhs is less than time point rhs

Example
    //prints:1
    cout << (system_clock::time_point() < system_clock::now());
greater
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 point lhs is greater than time point rhs

Example
    //prints:0
    cout << (system_clock::time_point() > system_clock::now());
lessorequal
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 point lhs is less than or equal to time point rhs

Example
    //prints:1
    cout << (system_clock::time_point() <= system_clock::now());
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 )

Checks if  the time point lhs is greater than time point rhs

Example
    //prints:0
    cout << (system_clock::time_point() >= system_clock::now());

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 );

Examples
    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;

This example 5 defines timer class based on chrono arithmetic and relational operators.




No comments:

Post a Comment