00001
00015 #include <dlrPortability/timeUtilities.h>
00016
00017
00018
00019
00020
00021
00022 #ifdef _WIN32
00023
00024
00025
00026 #include <time.h>
00027 #include <windows.h>
00028 #include <sys/timeb.h>
00029
00030
00031
00032 #else
00033
00034
00035
00036 #include <time.h>
00037 #include <sys/time.h>
00038 #include <sys/select.h>
00039
00040
00041
00042 #endif
00043
00044
00045
00046
00047 namespace dlr {
00048
00049 namespace portability {
00050
00051 void preciseSleep(int milliseconds)
00052 {
00053 portableSleep(static_cast<double>(milliseconds) / 1000.0);
00054 }
00055
00056 }
00057
00058 }
00059
00060
00061
00062
00063 #ifdef _WIN32
00064
00065
00066
00067
00068 namespace dlr {
00069
00070 namespace portability {
00071
00072 double
00073 getCurrentTime() {
00074 struct __timeb64 tp;
00075 _ftime64(&tp);
00076 return tp.time + (tp.millitm * 0.001);
00077 }
00078
00079
00080 void
00081 portableSleep(double seconds) {
00082 if(seconds > 0.0) {
00083 int milliseconds = static_cast<int>(1000.0 * seconds + 0.5);
00084 Sleep(milliseconds);
00085 }
00086 }
00087
00088 }
00089
00090 }
00091
00092
00093
00094 #else
00095
00096
00097
00098 #include <cmath>
00099
00100 namespace dlr {
00101
00102 namespace portability {
00103
00104 double
00105 getCurrentTime() {
00106 struct timeval tp;
00107 gettimeofday(&tp, 0);
00108 return tp.tv_sec + (tp.tv_usec * 0.000001);
00109 }
00110
00111
00112 void
00113 portableSleep(double seconds) {
00114 if(seconds > 0.0) {
00115 double integerPart;
00116 double fractionalPart = std::modf(seconds, &integerPart);
00117 struct timeval timeout;
00118 timeout.tv_sec = static_cast<int>(integerPart);
00119 timeout.tv_usec =
00120 static_cast<int>(1000000.0 * fractionalPart + 0.5);
00121 if(timeout.tv_usec == 1000000) {
00122 ++timeout.tv_sec;
00123 timeout.tv_usec = 0;
00124 }
00125 select(0, NULL, NULL, NULL, &timeout);
00126 }
00127 }
00128
00129 }
00130
00131 }
00132
00133
00134
00135 #endif