#ifndef TIME_UTILS_H #define TIME_UTILS_H #include #include /* typedef std::chrono::time_point 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 static inline T getAge(const std::chrono::time_point& time_point) { return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - time_point); } template static inline T getAge(const Chunk* chunk) { return getAge(timePoint(chunk)); } inline static long getAge(const Chunk* chunk) { return getAge(chunk).count(); } inline static long getAge(const time_point_ms& time_point) { return getAge(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