23 #ifndef __TIMESPEC_UTIL_H__ 24 #define __TIMESPEC_UTIL_H__ 36 inline void timestamp( timespec* timestampOut ) {
if(!timestampOut)
return; timestampOut->tv_sec=0; timestampOut->tv_nsec=0; clock_gettime(CLOCK_REALTIME, timestampOut); }
50 inline timespec
timeZero() { timespec t; t.tv_sec=0; t.tv_nsec=0;
return t; }
57 inline timespec
timeNew( time_t seconds,
long int nanoseconds ) { timespec t; t.tv_sec=seconds; t.tv_nsec=nanoseconds;
return t; }
64 inline timespec
timeNew(
long int nanoseconds ) {
const time_t sec=nanoseconds/1e-9;
return timeNew(sec, nanoseconds-sec*1e-9); }
71 inline timespec
timeAdd(
const timespec& a,
const timespec& b ) { timespec t; t.tv_sec=a.tv_sec+b.tv_sec; t.tv_nsec=a.tv_nsec+b.tv_nsec;
const time_t sec=t.tv_nsec/1e-9; t.tv_sec+=sec; t.tv_nsec-=sec*1e-9;
return t; }
78 inline void timeDiff(
const timespec& start,
const timespec& end, timespec* result )
80 if ((end.tv_nsec-start.tv_nsec)<0) {
81 result->tv_sec = end.tv_sec-start.tv_sec-1;
82 result->tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
84 result->tv_sec = end.tv_sec-start.tv_sec;
85 result->tv_nsec = end.tv_nsec-start.tv_nsec;
94 inline timespec
timeDiff(
const timespec& start,
const timespec& end )
111 inline int timeCmp(
const timespec& a,
const timespec& b )
113 if( a.tv_sec < b.tv_sec )
115 else if( a.tv_sec > b.tv_sec )
119 if( a.tv_nsec < b.tv_nsec )
121 else if( a.tv_nsec > b.tv_nsec )
132 inline float timeFloat(
const timespec& a ) {
return a.tv_sec * 1000.0f + a.tv_nsec * 0.000001f; }
139 inline float timeDouble(
const timespec& a ) {
return a.tv_sec * 1000.0 + a.tv_nsec * 0.000001; }
146 inline char*
timeStr(
const timespec&
timestamp,
char* strOut ) { sprintf(strOut,
"%lu s %lu ns", (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec);
return strOut; }
153 inline void timePrint(
const timespec&
timestamp,
const char* text=NULL ) { printf(
"%s %lus %010luns\n", text, (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec); }
160 inline void sleepTime(
const timespec& duration ) { nanosleep(&duration, NULL); }
float timeFloat(const timespec &a)
Convert to 32-bit float (in milliseconds).
Definition: timespec.h:132
void sleepUs(uint64_t microseconds)
Put the current thread to sleep for a specified number of microseconds.
Definition: timespec.h:181
float timeDouble(const timespec &a)
Convert to 64-bit double (in milliseconds).
Definition: timespec.h:139
void timePrint(const timespec ×tamp, const char *text=NULL)
Print the time to stdout.
Definition: timespec.h:153
void timestamp(timespec *timestampOut)
Retrieve a timestamp of the current system time.
Definition: timespec.h:36
void sleepMs(uint64_t milliseconds)
Put the current thread to sleep for a specified number of milliseconds.
Definition: timespec.h:174
void timeDiff(const timespec &start, const timespec &end, timespec *result)
Find the difference between two timestamps.
Definition: timespec.h:78
void sleepNs(uint64_t nanoseconds)
Put the current thread to sleep for a specified number of nanoseconds.
Definition: timespec.h:188
timespec timeAdd(const timespec &a, const timespec &b)
Add two times together.
Definition: timespec.h:71
timespec timeNew(time_t seconds, long int nanoseconds)
Return an initialized timespec
Definition: timespec.h:57
void sleepTime(const timespec &duration)
Put the current thread to sleep for a specified time.
Definition: timespec.h:160
int timeCmp(const timespec &a, const timespec &b)
Compare two timestamps.
Definition: timespec.h:111
timespec timeZero()
Return a blank timespec that's been zero'd.
Definition: timespec.h:50
char * timeStr(const timespec ×tamp, char *strOut)
Produce a text representation of the timestamp.
Definition: timespec.h:146