Thursday, February 20, 2025

clocks

Overview
clock is a device that has a starting time or epoch time and a resolution or period. The epoch time can vary based on the clock type. For a wall clock, epoch time starts from  1970.1.1 12:00 GMT, Chrono library defines three clocks as defined below. 

Details


system_clock 
system_clock represents the system-wide real time wall clock. Its attributes are:
  • It is intended to represent the real time, and thus it can be translated in some way to and from calendar representations. 
  • Its time_point values can refer to times before the epoch (with negative values).
  • All processes running on the system shall retrieve the same time_point values by using this clock.
member types
NameDescription
repsigned arithmetic type representing the number of ticks in the clock's duration
periodratio type representing the tick period of the clock, in seconds.
durationduration<rep, period>, capable of representing negative durations.
time_pointtime_point<system_clock>

member constants
NameDescription
 bool is_steadyA bool value specifying whether the clock always advances.
true if the time between ticks is always constant and increasing.
Example
/* prints
    Monotonic	:false
    Resolution	:ratio<1,1000000000>
    Now		:1740224214 seconds
*/
    
    cout << "Monotonic\t:" << boolalpha << system_clock::is_steady << endl; 
    cout << "Resolution\t:" << "ratio<" << system_clock::period::num << "," << system_clock::period::den << ">" << endl;
    cout << "Now\t\t:" << duration_cast<seconds>(system_clock::now().time_since_epoch()).count()  << " seconds" << endl;

member functions
NameDescription
time_point system_clock::now()Returns a time_point representing the current point in time.
Example
    //prints:[1740146546277738489, <1,1000000000>]
    cout << system_clock::time_point(system_clock::now()).time_since_epoch();
time_t to_time_t
(const time_point& tp) 
Converts tp into its equivalent of type time_t
system_clock::time_point
system_clock::from_time_t
(time_t t)
Converts t to a time point type, using the coarser precision of the two types.
Example
    time_t today = system_clock::to_time_t(system_clock::now());

    //prints:[1740223822000000000, <1,1000000000>]
    cout << system_clock::from_time_t(today).time_since_epoch() << endl;

steady_clock 
steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward and the time between ticks of this clock is constant. This clock is not related to wall clock time (for example, it can be time since last reboot), and is most suitable for measuring intervals.
It's monotonic as its now() never returns a lower value than in a previous call.

member types
NameDescription
repsigned arithmetic type representing the number of ticks in the clock's duration
periodratio type representing the tick period of the clock, in seconds.
durationduration<rep, period>, capable of representing negative durations.
time_pointtime_point<steady_clock>

member constants
NameDescription
 bool is_steadyA bool value specifying whether the clock always advances.
true if the time between ticks is always constant and increasing.
Example
/* prints
   Monotonic	:true
    Resolution	:ratio<1,1000000000>
    Now		:6025161 seconds
*/
    
    cout << "Monotonic\t:" << boolalpha << steady_clock::is_steady << endl; 
    cout << "Resolution\t:" << "ratio<" << steady_clock::period::num << "," << steady_clock::period::den << ">" << endl;
    cout << "Now\t\t:" << duration_cast<seconds>(steady_clock::now().time_since_epoch()).count()  << " seconds" << endl;

member functions
NameDescription
time_point steady_clock::now()Returns a time_point representing the current point in time.
Example
    //prints:[6025308175775127, <1,1000000000>]
    cout << steady_clock::time_point(steady_clock::now()).time_since_epoch();

high_resolution_clock 
high_resolution_clock is the clock with the shortest tick period. It may be a synonym for system_clock or steady_clock.
The clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of system_clock or steady_clock, or a third, independent clock.
The members of clock classes provide access to the current time_point.

member types
NameDescription
repsigned arithmetic type representing the number of ticks in the clock's duration
periodratio type representing the tick period of the clock, in seconds.
durationduration<rep, period>, capable of representing negative durations.
time_pointtime_point<high_resolution_clock >

member constants
NameDescription
 bool is_steadyA bool value specifying whether the clock always advances.
true if the time between ticks is always constant and increasing.
Example
/* prints
Monotonic	:false
Resolution	:ratio<1,1000000000>
Now		:1740247378 seconds
*/
    
    cout << "Monotonic\t:" << boolalpha << high_resolution_clock ::is_steady << endl; 
    cout << "Resolution\t:" << "ratio<" << high_resolution_clock ::period::num << "," << high_resolution_clock ::period::den << ">" << endl;
    cout << "Now\t\t:" << duration_cast<seconds>(high_resolution_clock ::now().time_since_epoch()).count()  << " seconds" << endl;
member functions
NameDescription
time_point steady_clock::now()Returns a time_point representing the current point in time.
Example
 //prints:[6025308175775127, <1,1000000000>]
 cout << high_resolution_clock ::time_point(high_resolution_clock ::now()).time_since_epoch();

No comments:

Post a Comment