timeUtilities.cpp

Go to the documentation of this file.
00001 
00015 #include <dlrPortability/timeUtilities.h>
00016 
00017 /* ===================== Common includes ===================== */
00018 
00019 
00020 /* ===================== End common includes ===================== */
00021 
00022 #ifdef _WIN32
00023 
00024 /* ===================== Windows includes ===================== */
00025 
00026 #include <time.h>
00027 #include <windows.h>
00028 #include <sys/timeb.h>
00029 
00030 /* ===================== End Windows includes ===================== */
00031 
00032 #else /* #ifdef _WIN32 */
00033 
00034 /* ===================== Linux includes ===================== */
00035 
00036 #include <time.h>
00037 #include <sys/time.h>
00038 #include <sys/select.h>
00039 
00040 /* ===================== End Linux includes ===================== */
00041 
00042 #endif /* #ifdef _WIN32 */
00043 
00044 
00045 /* ===================== Common code ===================== */
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   } // namespace portability
00057 
00058 } // namespace dlr
00059 
00060 /* ===================== End common code ===================== */
00061 
00062 
00063 #ifdef _WIN32
00064 
00065 /* ===================== Windows code ===================== */
00066 
00067 // Anonymous namespace for stuff local to this file.
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   } // namespace portability
00089   
00090 } // namespace dlr
00091 
00092 /* ===================== End Windows code ===================== */
00093 
00094 #else /* #ifdef _WIN32 */
00095 
00096 /* ===================== Linux code ===================== */
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   } // namespace portability
00130 
00131 } // namespace dlr
00132 
00133 /* ===================== End Linux code ===================== */
00134 
00135 #endif /* #ifdef _WIN32 */

Generated on Mon Jul 9 20:34:03 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2