xtime.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __XTIME__
00023 #define __XTIME__
00024
00025 #ifdef WIN32
00026 #include "CLAM_windows.h"
00027 #undef GetClassName
00028 #else
00029 #include <sys/time.h>
00030 #endif
00031 #include <pthread.h>
00032
00033 namespace CLAM
00034 {
00035
00036 enum
00037 {
00038 TIME_UTC=1,
00039 TIME_TAI,
00040 TIME_MONOTONIC,
00041 TIME_PROCESS,
00042 TIME_THREAD,
00043 TIME_LOCAL,
00044 TIME_SYNC,
00045 TIME_RESOLUTION
00046 };
00047
00048 struct xtime
00049 {
00050 unsigned int sec;
00051 unsigned int nsec;
00052 };
00053
00054 inline int xtime_get( xtime* xtp, int clock_type )
00055 {
00056 if ( clock_type == TIME_UTC )
00057 {
00058 #ifdef WIN32
00059 FILETIME ft;
00060 GetSystemTimeAsFileTime(&ft);
00061 const unsigned __int64 TIMESPEC_TO_FILETIME_OFFSET = ((unsigned __int64)27111902UL << 32) + (unsigned __int64)3577643008UL;
00062 xtp->sec = (int)((*( __int64*)&ft - TIMESPEC_TO_FILETIME_OFFSET) / 10000000);
00063 xtp->nsec = (int)((*( __int64*)&ft - TIMESPEC_TO_FILETIME_OFFSET -
00064 (( __int64)xtp->sec * ( __int64)10000000)) * 100);
00065 return clock_type;
00066 #else
00067 struct timeval tv;
00068 gettimeofday(&tv, 0);
00069 xtp->sec = tv.tv_sec;
00070 xtp->nsec = tv.tv_usec * 1000;
00071 return clock_type;
00072 #endif
00073 }
00074 return clock_type;
00075 }
00076
00077
00078 const int MILLISECONDS_PER_SECOND = 1000;
00079 const int NANOSECONDS_PER_SECOND = 1000000000;
00080 const int NANOSECONDS_PER_MILLISECOND = 1000000;
00081
00082 const int MICROSECONDS_PER_SECOND = 1000000;
00083 const int NANOSECONDS_PER_MICROSECOND = 1000;
00084
00085 inline void to_time(int milliseconds, xtime& xt)
00086 {
00087
00088 xtime_get(&xt, TIME_UTC);
00089
00090 xt.sec += (milliseconds / MILLISECONDS_PER_SECOND);
00091 xt.nsec += ((milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND);
00092
00093 if (xt.nsec > static_cast<const int>(NANOSECONDS_PER_SECOND))
00094 {
00095 ++xt.sec;
00096 xt.nsec -= NANOSECONDS_PER_SECOND;
00097 }
00098
00099
00100
00101 }
00102
00103
00104 inline void to_timespec(const xtime& xt, timespec& ts)
00105 {
00106 ts.tv_sec = static_cast<int>(xt.sec);
00107 ts.tv_nsec = static_cast<int>(xt.nsec);
00108 if(ts.tv_nsec > static_cast<const int>(NANOSECONDS_PER_SECOND))
00109 {
00110 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
00111 ts.tv_nsec %= NANOSECONDS_PER_SECOND;
00112 }
00113 }
00114
00115 inline void to_time(int milliseconds, timespec& ts)
00116 {
00117 xtime xt;
00118 to_time(milliseconds, xt);
00119 to_timespec(xt, ts);
00120
00121
00122
00123 }
00124
00125 inline void to_timespec_duration(const xtime& xt, timespec& ts)
00126 {
00127 xtime cur;
00128
00129 xtime_get(&cur, TIME_UTC);
00130
00131 if (xt.sec < cur.sec || (xt.sec == cur.sec && xt.nsec < cur.nsec))
00132 {
00133 ts.tv_sec = 0;
00134 ts.tv_nsec = 0;
00135 }
00136 else
00137 {
00138 ts.tv_sec = xt.sec - cur.sec;
00139 ts.tv_nsec = xt.nsec - cur.nsec;
00140
00141 if( ts.tv_nsec < 0 )
00142 {
00143 ts.tv_sec -= 1;
00144 ts.tv_nsec += NANOSECONDS_PER_SECOND;
00145 }
00146 if(ts.tv_nsec > static_cast<const int>(NANOSECONDS_PER_SECOND))
00147 {
00148 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
00149 ts.tv_nsec %= NANOSECONDS_PER_SECOND;
00150 }
00151 }
00152 }
00153
00154
00155
00156 }
00157
00158 #endif // XTime.hxx
00159