timeUtilities.cpp

Go to the documentation of this file.
00001 
00015 #include <dlrCommon/exception.h>
00016 #include <dlrPortability/timeUtilities.h>
00017 
00018 /* ===================== Common includes ===================== */
00019 
00020 
00021 /* ===================== End common includes ===================== */
00022 
00023 #ifdef _WIN32
00024 
00025 /* ===================== Windows includes ===================== */
00026 
00027 #include <time.h>
00028 #include <windows.h>
00029 #include <sys/timeb.h>
00030 
00031 /* ===================== End Windows includes ===================== */
00032 
00033 #else /* #ifdef _WIN32 */
00034 
00035 /* ===================== Linux includes ===================== */
00036 
00037 #include <time.h>
00038 #include <sys/time.h>
00039 #include <sys/select.h>
00040 
00041 /* ===================== End Linux includes ===================== */
00042 
00043 #endif /* #ifdef _WIN32 */
00044 
00045 
00046 /* ===================== Common code ===================== */
00047 
00048 namespace dlr {
00049 
00050   namespace portability {
00051 
00052     void preciseSleep(int milliseconds)
00053     {
00054       portableSleep(static_cast<double>(milliseconds) / 1000.0);
00055     }
00056     
00057   } // namespace portability
00058 
00059 } // namespace dlr
00060 
00061 /* ===================== End common code ===================== */
00062 
00063 
00064 #ifdef _WIN32
00065 
00066 /* ===================== Windows code ===================== */
00067 
00068 // Anonymous namespace for stuff local to this file.
00069 namespace dlr {
00070 
00071   namespace portability {
00072 
00073     double
00074     getCurrentTime() {
00075       struct __timeb64 tp;
00076       _ftime64(&tp);
00077       return tp.time + (tp.millitm * 0.001);
00078     }
00079     
00080     
00081     void
00082     portableSleep(double seconds) {
00083       if(seconds > 0.0) {
00084         int milliseconds = static_cast<int>(1000.0 * seconds + 0.5);
00085         Sleep(milliseconds);
00086       }
00087     }
00088     
00089   } // namespace portability
00090   
00091 } // namespace dlr
00092 
00093 /* ===================== End Windows code ===================== */
00094 
00095 #else /* #ifdef _WIN32 */
00096 
00097 /* ===================== Linux code ===================== */
00098 
00099 #include <cmath>
00100 
00101 namespace dlr {
00102 
00103   namespace portability {
00104 
00105     double
00106     getCurrentTime() {
00107       struct timeval tp;
00108       gettimeofday(&tp, 0);
00109       return tp.tv_sec + (tp.tv_usec * 0.000001);
00110     }
00111 
00112 
00113     std::string
00114     getISOTimeString(TimeZone timeZone) {
00115       // 20 characters is enough for "YYYY-MM-DD hh:mm:ss\0".
00116       const int bufferSize = 20;
00117       char buffer[bufferSize]; 
00118       time_t timeSinceEpoch;
00119       time_t returnValue = time(&timeSinceEpoch);
00120       if(returnValue == -1) {
00121         DLR_THROW(RunTimeException, "getISOTimeString()",
00122                   "Call to time() failed.");
00123       }
00124       struct tm brokenDownTime;
00125       switch(timeZone) {
00126       case DLR_TZ_GMT:
00127         gmtime_r(&timeSinceEpoch, &brokenDownTime);
00128         break;
00129       case DLR_TZ_LOCAL:
00130         tzset();
00131         localtime_r(&timeSinceEpoch, &brokenDownTime);
00132         break;
00133       default:
00134         DLR_THROW(LogicException, "getISOTimeString()",
00135                   "Unexpected value in switch() argument.");
00136         break;
00137       }
00138 
00139       strftime(buffer, bufferSize, "%Y-%m-%d %H:%M:%S", &brokenDownTime);
00140       return std::string(buffer);
00141     }
00142     
00143 
00144     void
00145     portableSleep(double seconds) {
00146       if(seconds > 0.0) {
00147         double integerPart;
00148         double fractionalPart = std::modf(seconds, &integerPart);
00149         struct timeval timeout;
00150         timeout.tv_sec = static_cast<int>(integerPart);
00151         timeout.tv_usec =
00152           static_cast<int>(1000000.0 * fractionalPart + 0.5);
00153         if(timeout.tv_usec == 1000000) {
00154           ++timeout.tv_sec;
00155           timeout.tv_usec = 0;
00156         }
00157         select(0, NULL, NULL, NULL, &timeout);
00158       }
00159     }
00160 
00161   } // namespace portability
00162 
00163 } // namespace dlr
00164 
00165 /* ===================== End Linux code ===================== */
00166 
00167 #endif /* #ifdef _WIN32 */

Generated on Wed Nov 25 00:03:49 2009 for dlrPortability Utility Library by  doxygen 1.5.8