I tried to print the current time in the usual format of hh:mm:ss however I get the full date format instead. I need the answer to be in string or int, so it is easier to deal with. I was thinking of adding it to a log file so that I can make it easier to track my program.
#include <iostream>
#include <chrono>
#include <ctime>
int main()
{
auto curr_time = std::chrono::system_clock::now();
std::time_t pcurr_time = std::chrono::system_clock::to_time_t(curr_time);
std::cout << "current time" << std::ctime(&pcurr_time)<<"\n";
}
I do want a little tip to help me do so.
>Solution :
The following example displays the time in the "HH:MM:SS" format (requires at least c++11 – tested with clang):
#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>
int main()
{
std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm ltime;
localtime_r(&t, <ime);
std::cout << std::put_time(<ime, "%H:%M:%S") << std::endl;
}
Compiling and running:
$ clang++ -std=c++11 so.cpp
$ ./a.out
13:44:23
$