How can I get the current time, in C++, without C libraries?
C has <ctime>. I am trying to avoid C libraries, for no good reason.
How can I get the current time, in C++, without C libraries?
C has <ctime>. I am trying to avoid C libraries, for no good reason.
You can use std::chrono::system_clock::now() from chrono library. It returns a time point representing the current point in time.
system_clock::now() returns a time_point object representing the current time.
The time point object can be converted to time_t with system_clock::to_time_t(), which can be printed and manipulated further.
#include <iostream>
#include <chrono>
int main() {
    std::chrono::time_point t = std::chrono::system_clock::now();
    time_t t_time_t = std::chrono::system_clock::to_time_t(t);
    std::cout << t_time_t << '\n';
}
Here time_t is imported from <chrono>, even though it is a C type.
You can use time.h to get the time in seconds as below,
include <time.h>
time_t timeSec;
time(&timeSec);
println("Current time in seconds : \t%lld\n", (long long)timeSec);