snapcast/client/timeProvider.h
(no author) 0a20924e66 code cleanup
git-svn-id: svn://elaine/murooma/trunk@337 d8a302eb-03bc-478d-80e4-98257eca68ef
2015-01-01 18:15:20 +00:00

66 lines
1.4 KiB
C++

#ifndef TIME_PROVIDER_H
#define TIME_PROVIDER_H
#include <atomic>
#include <chrono>
#include "doubleBuffer.h"
#include "message/message.h"
#include "common/timeDefs.h"
class TimeProvider
{
public:
static TimeProvider& getInstance()
{
static TimeProvider instance;
return instance;
}
void setDiffToServer(double ms);
template<typename T>
inline T getDiffToServer() const
{
return std::chrono::duration_cast<T>(chronos::usec(diffToServer_));
}
/* chronos::usec::rep getDiffToServer();
chronos::usec::rep getPercentileDiffToServer(size_t percentile);
long getDiffToServerMs();
*/
template<typename T>
static T sinceEpoche(const chronos::time_point_hrc& point)
{
return std::chrono::duration_cast<T>(point.time_since_epoch());
}
static chronos::time_point_hrc toTimePoint(const tv& timeval)
{
return chronos::time_point_hrc(chronos::usec(timeval.usec) + chronos::sec(timeval.sec));
}
inline static chronos::time_point_hrc now()
{
return chronos::hrc::now();
}
inline static chronos::time_point_hrc serverNow()
{
return chronos::hrc::now() + TimeProvider::getInstance().getDiffToServer<chronos::usec>();
}
private:
TimeProvider();
TimeProvider(TimeProvider const&); // Don't Implement
void operator=(TimeProvider const&); // Don't implement
DoubleBuffer<chronos::usec::rep> diffBuffer_;
std::atomic<chronos::usec::rep> diffToServer_;
};
#endif