a system clock from simgear, using std::chrono libs

this is to use some kind of utc time for timestamps in mp protocol,
to improve the lag correction system for mp planes, using a
"real time" mode when possible.
This commit is contained in:
jean pellotier
2018-01-06 10:07:21 +01:00
parent 8a128a57cd
commit 396a7d7f09
2 changed files with 23 additions and 0 deletions

View File

@@ -29,6 +29,7 @@
#include <ctime>
#include <cerrno>
#include <chrono>
#ifdef HAVE_UNISTD_H
# include <unistd.h> // for gettimeofday() and the _POSIX_TIMERS define
@@ -98,6 +99,21 @@ void SGTimeStamp::stamp()
#endif
}
void SGTimeStamp::systemClockHoursAndMinutes()
{
using namespace std;
using namespace std::chrono;
typedef duration<int, ratio_multiply<hours::period, ratio<24> >::type> days;
system_clock::time_point now = system_clock::now();
system_clock::duration tp = now.time_since_epoch();
tp -= duration_cast<days>(tp);
_sec = duration_cast<seconds>(tp).count();
_nsec = duration_cast<nanoseconds>(tp - seconds(_sec)).count();
}
// sleep based timing loop.
//
// Calling sleep, even usleep() on linux is less accurate than

View File

@@ -78,6 +78,13 @@ public:
/** Update stored time to current time (seconds and nanoseconds) */
void stamp();
/** Update stored time to current system clock (seconds and nanoseconds)
* non monotonic, keeping only hours, minutes, seconds and decimals,
* so restart at 0 at midnight.
* using the std::chrono libs
*/
void systemClockHoursAndMinutes();
/** Set the time from a double value */
void setTime(const double& seconds)
{