In: Computer Science
c++ CHRONO CLOCK wrong format....
So right now, the code im using is using std::chrono::system_clock; std::time_t tt = system_clock::to_time_t (system_clock::now()); struct std::tm * ptm = std::localtime(&tt); std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n'; std::this_thread::sleep_for (std::chrono::seconds(7)); It is simple in that this is in a loop, and chrono sleep_for delays the system for however many seconds. The problem is that it is in the HH:MM:SS format when I really need seconds.milliseconds to show the system clock transaction time. How would I do this? I really just need someone to explain the code, why is it making a struct? And what should I do to change the format? Thanks!
/**********************main.cpp*****************/
#include <iostream>
#include <chrono>
#include <unistd.h>
using namespace std;
int main()
{
//get start time
auto start_time = chrono::steady_clock::now();
sleep(7);//do something
//get end time of transaction
auto end_time = chrono::steady_clock::now();
cout << "Transaction time in milliseconds : " << chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count()<< " ms" << endl;
cout << "Transaction time in seconds : " << chrono::duration_cast<chrono::seconds>(end_time - start_time).count()<< " sec";
return 0;
}
/****************output***************************/
Please let me know if you have any doubt or modify the answer, Thanks :)