I want to measure elapsed time in seconds. With std::chrono::steady_clock I do it. However it suffers from system time changes.
Wasn't steady_clock supposed to not being affected by changes in system time?
How can I do that?
Here is the code:
#include <iostream>
#include <chrono>
#include <time.h>
std::chrono::steady_clock::time_point t = std::chrono::steady_clock::now();
/* Change system time */
std::time_t tnow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
tnow -= 20;
std::cout << "stime: " << stime(&tnow) << std::endl;
/********************************************************/
sleep(5);
std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
std::cout << "ELAPSED: " << std::chrono::duration_cast<std::chrono::seconds>(t2-t).count() << std::endl;
This results:
stime: 0
ELAPSED: -15
What I wanted to get was:
ELAPSED: 5
Edit:
I have added C tag, because it seems that it is a kernel (or buildroot of board) bug. So, how could I achieve this without chrono? I mean, in a straight way (without having to watch system time changes).
How was the people living before chrono?