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
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <vlCore/Time.hpp>
00033 #include <ctime>
00034 #include <cmath>
00035
00036
00037 #if defined(_WIN32)
00038 #include <windows.h>
00039
00040 #elif defined(__GNUG__)
00041 #include <sys/time.h>
00042 #include <unistd.h>
00043 #endif
00044
00045 using namespace vl;
00046
00047 namespace
00048 {
00049 double init_start_time()
00050 {
00051 #if defined(_WIN32)
00052
00053 LARGE_INTEGER Frequency;
00054 LARGE_INTEGER PerformanceCount;
00055 BOOL has_timer = QueryPerformanceFrequency( &Frequency );
00056 if (has_timer)
00057 {
00058 QueryPerformanceCounter( &PerformanceCount );
00059 return (double)PerformanceCount.QuadPart/Frequency.QuadPart;
00060 }
00061 else
00062 {
00063 return GetTickCount() / 1000.0;
00064 }
00065 #elif defined(__GNUG__)
00066 struct timeval tv;
00067 gettimeofday( &tv, NULL );
00068 return tv.tv_sec + tv.tv_usec * 0.000001;
00069 #endif
00070 }
00071 double mStartTime = init_start_time();
00072 }
00073
00074
00075
00076
00077 Time::Time()
00078 {
00079 VL_DEBUG_SET_OBJECT_NAME()
00080
00081 for(int i=0; i<VL_MAX_TIMERS; ++i)
00082 mStart[i] = -1;
00083
00084 #if defined(_WIN32)
00085 SYSTEMTIME local_time;
00086 GetLocalTime(&local_time);
00087 mYear = local_time.wYear;
00088 mMonth = local_time.wMonth;
00089 mDayOfWeek = local_time.wDayOfWeek;
00090 mDayOfMonth = local_time.wDay;
00091 mHour = local_time.wHour;
00092 mMinute = local_time.wMinute;
00093 mSecond = local_time.wSecond;
00094 mMicrosecond = local_time.wMilliseconds * 1000;
00095 #elif defined(__GNUG__)
00096 time_t secs;
00097 ::time(&secs);
00098 tm* date = localtime( &secs );
00099
00100 mYear = date->tm_year + 1900;
00101 mMonth = date->tm_mon;
00102 mDayOfWeek = date->tm_wday;
00103 mDayOfMonth = date->tm_mday;
00104 mHour = date->tm_hour;
00105 mMinute = date->tm_min;
00106 mSecond = date->tm_sec;
00107
00108 struct timeval tv;
00109 gettimeofday( &tv, NULL );
00110 mMicrosecond = tv.tv_usec;
00111 #endif
00112 }
00113
00116 Real Time::currentTime()
00117 {
00118 #if defined(_WIN32)
00119
00120 LARGE_INTEGER Frequency;
00121 LARGE_INTEGER PerformanceCount;
00122 BOOL has_timer = QueryPerformanceFrequency( &Frequency );
00123 if (has_timer)
00124 {
00125 QueryPerformanceCounter( &PerformanceCount );
00126 return (Real)PerformanceCount.QuadPart/Frequency.QuadPart - mStartTime;
00127 }
00128 else
00129 {
00130 return (Real)(GetTickCount() / 1000.0 - mStartTime);
00131 }
00132 #elif defined(__GNUG__)
00133 struct timeval tv;
00134 gettimeofday( &tv, NULL );
00135 return (Real)(tv.tv_sec + tv.tv_usec * 0.000001 - mStartTime);
00136 #endif
00137 }
00138
00139 void Time::sleep(unsigned int milliseconds)
00140 {
00141 #if defined(_WIN32)
00142 Sleep(milliseconds);
00143 #elif defined(__GNUG__)
00144 usleep(milliseconds*1000);
00145 #endif
00146 }
00147