snapcast/common/timeUtils.h
(no author) feabfee936 xxxx
git-svn-id: svn://elaine/murooma/trunk@272 d8a302eb-03bc-478d-80e4-98257eca68ef
2014-09-17 21:58:38 +00:00

107 lines
1.9 KiB
C++

#ifndef TIME_UTILS_H
#define TIME_UTILS_H
#include <sys/time.h>
#include <chrono>
/*
typedef std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::milliseconds> time_point_ms;
static inline time_point_ms timePoint(const Chunk* chunk)
{
time_point_ms tp;
return tp + std::chrono::seconds(chunk->tv_sec) + std::chrono::milliseconds(chunk->tv_usec / 1000) + std::chrono::milliseconds(chunk->idx / WIRE_CHUNK_MS_SIZE);
}
template<typename T, typename U>
static inline T getAge(const std::chrono::time_point<U>& time_point)
{
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - time_point);
}
template<typename T>
static inline T getAge(const Chunk* chunk)
{
return getAge<T>(timePoint(chunk));
}
inline static long getAge(const Chunk* chunk)
{
return getAge<std::chrono::milliseconds>(chunk).count();
}
inline static long getAge(const time_point_ms& time_point)
{
return getAge<std::chrono::milliseconds>(time_point).count();
}
*/
static void addMs(timeval& tv, int ms)
{
if (ms < 0)
{
timeval t;
t.tv_sec = -ms / 1000;
t.tv_usec = (-ms % 1000) * 1000;
timersub(&tv, &t, &tv);
return;
}
tv.tv_usec += ms*1000;
tv.tv_sec += (tv.tv_usec / 1000000);
tv.tv_usec %= 1000000;
}
static void addUs(timeval& tv, int us)
{
if (us < 0)
{
timeval t;
t.tv_sec = -us / 1000000;
t.tv_usec = (-us % 1000000);
timersub(&tv, &t, &tv);
return;
}
tv.tv_usec += us;
tv.tv_sec += (tv.tv_usec / 1000000);
tv.tv_usec %= 1000000;
}
/*static long diffMs(const timeval& t1, const timeval& t2)
{
return (((t1.tv_sec - t2.tv_sec) * 1000000) +
(t1.tv_usec - t2.tv_usec))/1000;
}*/
static long getTickCount()
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec*1000 + now.tv_nsec / 1000000;
}
static long getuTickCount()
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec*1000000 + now.tv_nsec / 1000;
}
#endif