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