-
std::time()
: It returns the current time (seconds since 00:00:00 GMT, Jan 1, 1970) as an object of typetime_t
.time_t
is defined asint64
under the hood. -
gmtime (time_t *timer )
: converts given time since epoch into calendar time, expressed in Coordinated Universal Time (UTC) in thestruct tm
format. The result is stored in static storage and a pointer to that static storage is returned. -
char* asctime (struct tm * timeptr)
: It convert the contents of thetm
structure into a C-string in the following format:www Mmm dd hh:mm:ss yyyy
#include <ctime>
#include <time.h>
int main()
{
time_t epoch = std::time(NULL);
std::cout << "seconds since the epoch began: " << epoch << std::endl;
tm *t = gmtime(&epoch);
std::cout << "current date and time: " << 1900 + t->tm_year << "/"
<< 1 + t->tm_mon << "/" << t->tm_mday << std::endl;
std::cout << "current date and time: " << asctime(gmtime(&epoch))
<< std::endl;
}
template <class Clock, class Duration> class time_point;
: This class expresses a point in time relative to a clock's epoch.
system_clock
: is a system-wide realtime clock.steady_clock
: is specifically designed to calculate time intervalshigh_resolution_clock
: is the clock with the shortest tick period. It may be a synonym forsystem_clock
orsteady_clock
.
template <class Rep, class Period = ratio<1> >class duration;
A duration object expresses a time span by means of a count (stored as an object of member type rep
) and a period.
template<
class Rep,
class Period = std::ratio<1>
> class duration;
Consists of the type of the tick Rep
and the length of a tick Period
.
- Rep: an arithmetic type representing the number of ticks
- Period: a
std::ratio
representing the tick period (number of second's fractions per tick)
std::ratio<1,1>
stands for a second.std::ratio<1,60>
is a minute.std::ratio<1,1000>
is a millisecond.
Predefined durations:
std::chrono::nanoseconds = typedef duration<signed int, std::nano> nanoseconds;
std::chrono::microseconds = typedef duration<signed int, std::micro> microseconds;
std::chrono::hours = typedef duration<signed int, ratio<3600>> hours;
Examples:
std::chrono::system_clock::now()
returns std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>
Different durations are not implicitly convertible to one another, You can have to convert time points with different durations using std::chrono::time_point_cast
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> timePoint =
std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now());
const auto now = std::chrono::system_clock::now();
time_t time = std::chrono::system_clock::to_time_t(now);
std::cout << time << "\n";
std::cout << std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() << "\n";
std::cout << std::chrono::system_clock::from_time_t(time).time_since_epoch().count() << "\n";
std::cout << std::chrono::system_clock::time_point(std::chrono::seconds(time)).time_since_epoch().count() << "\n";