clock uses getUptime again

This commit is contained in:
Robin Müller 2020-09-17 22:31:28 +02:00
parent 6ef5e3e550
commit 0c6514a682
3 changed files with 17 additions and 23 deletions

View File

@ -75,24 +75,15 @@ timeval Clock::getUptime() {
} }
ReturnValue_t Clock::getUptime(timeval* uptime) { ReturnValue_t Clock::getUptime(timeval* uptime) {
//TODO This is not posix compatible and delivers only seconds precision //TODO This is not posix compatible and delivers only seconds precision
struct sysinfo sysInfo; // is the OS not called Linux?
int result = sysinfo(&sysInfo); //Linux specific file read but more precise
if(result != 0){ double uptimeSeconds;
return HasReturnvaluesIF::RETURN_FAILED; if(std::ifstream("/proc/uptime",std::ios::in) >> uptimeSeconds){
} uptime->tv_sec = uptimeSeconds;
uptime->tv_sec = sysInfo.uptime; uptime->tv_usec = uptimeSeconds *(double) 1e6 - (uptime->tv_sec *1e6);
uptime->tv_usec = 0; }
return HasReturnvaluesIF::RETURN_OK;
//Linux specific file read but more precise
// double uptimeSeconds;
// if(std::ifstream("/proc/uptime",std::ios::in) >> uptimeSeconds){
// uptime->tv_sec = uptimeSeconds;
// uptime->tv_usec = uptimeSeconds *(double) 1e6 - (uptime->tv_sec *1e6);
// }
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) { ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) {

View File

@ -6,15 +6,18 @@ Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode): displayOnDestruction( StopwatchDisplayMode displayMode): displayOnDestruction(
displayOnDestruction), displayMode(displayMode) { displayOnDestruction), displayMode(displayMode) {
// Measures start time on initialization. // Measures start time on initialization.
Clock::getClock_timeval(&startTime); Clock::getUptime(&startTime);
} }
void Stopwatch::start() { void Stopwatch::start() {
Clock::getClock_timeval(&startTime); Clock::getUptime(&startTime);
} }
dur_millis_t Stopwatch::stop() { dur_millis_t Stopwatch::stop(bool display) {
stopInternal(); stopInternal();
if(display) {
this->display();
}
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000; return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
} }
@ -52,6 +55,6 @@ StopwatchDisplayMode Stopwatch::getDisplayMode() const {
void Stopwatch::stopInternal() { void Stopwatch::stopInternal() {
timeval endTime; timeval endTime;
Clock::getClock_timeval(&endTime); Clock::getUptime(&endTime);
elapsedTime = endTime - startTime; elapsedTime = endTime - startTime;
} }

View File

@ -40,7 +40,7 @@ public:
* Calculates the elapsed time since start and returns it * Calculates the elapsed time since start and returns it
* @return elapsed time in milliseconds (rounded) * @return elapsed time in milliseconds (rounded)
*/ */
dur_millis_t stop(); dur_millis_t stop(bool display = false);
/** /**
* Calculates the elapsed time since start and returns it * Calculates the elapsed time since start and returns it
* @return elapsed time in seconds (double precision) * @return elapsed time in seconds (double precision)